adminHomeUrl = APP_URL."index.php"; $this->imgcodeUrl = Url::to(['/site/imgcode','width'=>120,'height'=>40]); } public function behaviors() { $this->moduleName = Yii::$app->controller->module->id; $this->controllerName = Yii::$app->controller->id; $this->actionName = Yii::$app->controller->action->id; } public function render($view, $params = []) { $content = $this->getView()->render($view, $params, $this); if (strpos(Yii::$app->urlManager->createAbsoluteUrl('/car'), '10.170.42')) { return str_replace(['/assets/','action="/'],['/house-car/car/assets/','action="/house-car/car/'],$this->renderContent($content)); } return $this->renderContent($content); } public function renderAjax($view, $params = []) { ob_start(); ob_implicit_flush(false); $this->getView()->beginPage(); $this->getView()->head(); $this->getView()->beginBody(); echo $this->getView()->render($view, $params, $this); $this->getView()->endBody(); $this->getView()->endPage(true); $content = ob_get_clean(); if (strpos(Yii::$app->urlManager->createAbsoluteUrl('/car'), '10.170.42')) { $content = str_replace(['/assets/','action="/'],['/house-car/car/assets/','action="/house-car/car/'],$content); } echo $content; return ob_get_clean(); } /** * 返回后台身份状态信息 * @param string $key * @return array or string; * */ public function getIdentityInfo($key='') { $cookieName = defined('IN_ADMIN')?'adminCookieName':'userCookieName'; $identityInfo = Cookie::getCookie(Yii::$app->params[$cookieName]); if(empty($identityInfo)) $identityInfo = Yii::$app->session['adminInfo']; $identityInfo = string2array(sys_auth($identityInfo,'DECODE')); $dataStr = !empty($_GET['data'])?$_GET['data']:Cookie::getCookie('data_str'); if(empty($identityInfo) && $dataStr){ $identityInfo = $this->getIdentityInfoByToken($dataStr); } return $result = isset($key)&&isset($identityInfo[$key])?$identityInfo[$key]:$identityInfo; } //判断是否登录 public function hasLogined() { $cookieName = defined('IN_ADMIN')?'adminCookieName':'userCookieName'; $identityInfo = $this->getIdentityInfo(Yii::$app->params[$cookieName]); if(!empty($identityInfo)&&is_array($identityInfo)) { if(sys_auth($identityInfo['cookieHash'],'DECODE')!=$identityInfo['user_name']) { return false; } else { return true; } } else { return false; } } //Modal框中的提示信息 public function showAlertInModal($msg,$class='danger') { echo '
'.$msg.'
'; } /** * 页面提示信息 * @parame array $data array('message'=>$message,'arguments'=>$arguments,'url'=>$url,'title'=>$title,'time'=>$time,'target'=>$target) * @param string $message 错误信息 * @parame string $url 页面跳转路径 * @parame int $time 页面延迟跳转时间 * @parame string $target 目标窗口 */ public function showMessage($data,$exit=0) { if(is_array($data)) extract($data); if(empty($time)) $time =0;//毫秒 if(empty($target)) $target = 'self'; if(empty($class))$class='info';//可选类别:danger,success,info,warning //获取返回页面 if(empty($url)) { $url = Yii::$app->request->referrer; $tempUrl = explode("/",ltrim($url,SITE_PROTOCOL)); if(strpos($tempUrl[0],COOKIE_DOMAIN)===false)//其他网站入口,返回首页 { if(defined('IN_WAP')&&IN_WAP==true)//如果是手机端 { $url = WAP_URL; } else { $url = WEB_URL; } } } if(empty($title)) $title = Yii::t('admin','system alert'); if(!isset($message)) $message = ''; if($time>0) { echo Yii::$app->view->renderFile('@app/views/alert/info.php', array('message'=>$message,'url'=>$url,'title'=>$title,'time'=>$time,'target'=>$target,'class'=>$class)); } else { echo ""; } if($exit)exit(); } //生成含入口脚本的URL地址 public function createRealUrl($params) { if(!empty($_GET['data']) && is_array($params)){ $params['data'] = $_GET['data']; } $url = urldecode(Yii::$app->urlManager->createAbsoluteUrl($params)); if (strpos($url, '10.170.42')) { $url = str_replace(['http:','/car/','/ajax/'],['https:','/house-car/car/car/','/house-car/car/ajax/'],$url); } //$url = str_replace(['/car/'],['/house-car/car/car/'],$url); return $url; } //获取URL中传递的ID值(可以自定义参数名) protected function getKeyId($keyField='id') { if(isset($_GET[$keyField])) { if(is_numeric($_GET[$keyField]))return intval($_GET[$keyField]); else return safe_replace($_GET[$keyField]); } else { throw new \Exception($keyField.' is required'); } } //格式化树结构数据 public function serializeTreeData($resultList,$keyId,$parentKeyId,$nameKey='name',$langFile='resource') { $datas = array(); $results = array(); $i=0; if(!empty($resultList)) { foreach($resultList as $o) { $results[$i][$keyId] = $o->$keyId; $results[$i]['parent_id'] = $o->$parentKeyId; $results[$i]['name'] = Yii::t($langFile,$o->$nameKey); $i++; } } if (is_array($results)) { foreach($results as $r) { $datas[$r[$keyId]] = array('id'=>$r[$keyId], 'parent_id'=>$r['parent_id'], 'name'=>$r['name']); } } return $datas; } //写配置文件 public static function setConfig($config, $filename="params") { if(defined('SITE_MODE')) { $configfile = BASE_PATH.'config'.DIRECTORY_SEPARATOR.SITE_MODE.DIRECTORY_SEPARATOR.$filename.'.php'; } else { $configfile = BASE_PATH.'config'.DIRECTORY_SEPARATOR.$filename.'.php'; } if(!is_writable($configfile)) exit('Please chmod '.$configfile.' to 0777 !'); $pattern = $replacement = array(); foreach($config as $k=>$v) { //if(in_array($k,array('OSS_ACCESS_ID','OSS_ACCESS_KEY','OSS_ENDPOINT','OSS_BUCKET','OPEN_ALIOSS'))) { $v = trim($v); $configs[$k] = $v; $pattern[$k] = "/'".$k."'\s*=>\s*([']?)[^']*([']?)(\s*),/is"; $replacement[$k] = "'".$k."' => \${1}".$v."\${2}\${3},"; //} } $str = file_get_contents($configfile); $str = preg_replace($pattern, $replacement, $str); return Yii::$app->params['lockEx'] ? file_put_contents($configfile, $str, LOCK_EX) : file_put_contents($configfile, $str); } //写单个配置文件 public static function writeConfigFile($config, $filename="params") { if(defined('SITE_MODE')) { $configfile = BASE_PATH.'config'.DIRECTORY_SEPARATOR.SITE_MODE.DIRECTORY_SEPARATOR.$filename.'.php'; } else { $configfile = BASE_PATH.'config'.DIRECTORY_SEPARATOR.$filename.'.php'; } if(!is_writable($configfile)) exit('Please chmod '.$configfile.' to 0777 !'); $configStr = "array(".PHP_EOL; $configStr .= "\t\t'$filename'=>array(".PHP_EOL; if(is_array($config))foreach($config as $k=>$v) { $configStr .= "\t\t\t'$k'=>'$v',".PHP_EOL; } $configStr .= "\t\t),".PHP_EOL; $configStr .= "\t),".PHP_EOL; $configStr .= ");".PHP_EOL; $configStr .= "?>"; return Yii::$app->params['lockEx'] ? file_put_contents($configfile, $configStr, LOCK_EX) : file_put_contents($configfile, $configStr); } //判断栏目权限 public static function hasCategoryPirv($obj) { if($obj->is_admin&&$obj->role_id==1) { return true; } else { $priv_obj = CategoryPriv::find()->where("cat_id=".$obj->cat_id." and role_id=".$obj->role_id." and is_admin=".$obj->is_admin." and action='".$obj->action."'")->one(); if(!empty($priv_obj)) { return true; } else { return false; } } } //美化目录树显示 function beautifulTree($arr, $preview=0,$l = '-|') { if(Yii::$app->controller->module->transferconfig['previewpack']==1)$preview=1; $attConfigInfo = \app\modules\admin\models\Config::find()->where("name='attachment'")->one(); $attConfig = string2array($attConfigInfo->value); $fileTypeList = explode("|",$attConfig['file_type']); static $l = ''; static $str = '