123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- <?php
- namespace app\common\controllers;
- use app\modules\cms\models\CategoryPriv;
- use Yii;
- use yii\web\Controller;
- use yii\helpers\Url;
- use app\common\helpers\Cookie;
- /**
- * 基类
- */
- class CController extends Controller
- {
- public $imgcodeUrl;
- public $moduleName;
- public $controllerName;
- public $actionName;
- public $adminHomeUrl;
- public $waterMarkPos;//水印位置
- public $mediaTimeTypes;//多媒体时长类型
- public $transferPageTypes;//转换页数类型
- public $loginWays;//登录方式
- public $breadcrumbs;//面包屑
- public function init()
- {
- parent::init();
- error_reporting(0);
- $this->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 '<div class="alert alert-'.$class.'" >'.$msg.'</div>';
- }
- /**
- * 页面提示信息
- * @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 "<script>";
- echo "window.location.href='".$url."'";
- echo "</script>";
- }
- 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 = "<?php".PHP_EOL;
- $configStr .= "return array(".PHP_EOL;
- $configStr .= "\t'params'=>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 = '<ul class="file-tree">';
- //遍历刚才得到的目录树
- foreach($arr as $key=>$val) {
- //如果是个数组,也就代表它是个目录,那么就在它的子文件中加入-|来表示是下一级吧
- if(is_array($arr[$key])) {
- $valInfo = explode('/',strval($key));
- $str.="<li data-jstree='{\"opened\":true,\"icon\":\"iconfont icon-wenjianjia1\"}'> ".$valInfo[count($valInfo)-1]."<ul>";
- $this->beautifulTree($arr[$key], $preview, $l);
- $str.= "</ul>";
- }else {
- $valInfo = explode('/',strval($val));
- $ext = in_array(fileext($val),$fileTypeList)?fileext($val):'file';
- if($preview>=1)
- {
- $str.="<li data-jstree='{\"opened\":true,\"icon\":\"iconfont icon-".$ext."\"}' data-md5='".md5($val)."' class='previewpackfile".$preview."' > ".$l.$valInfo[count($valInfo)-1]."<span style='margin-left:15px;'>--点击预览</span></li>";
- }
- else
- {
- $str.="<li data-jstree='{\"opened\":true,\"icon\":\"iconfont icon-".$ext."\"}'> ".$l.$valInfo[count($valInfo)-1]."</li>";
- }
- }
- }
- $l = '';
- return $str;
- }
- //压缩包文件数量
- function packFileNum($arr)
- {
- static $num = 0;
- //遍历刚才得到的目录树
- if(is_array($arr))foreach($arr as $key=>$val) {
- //如果是个数组,也就代表它是个目录,那么就在它的子文件中加入-|来表示是下一级吧
- if(is_array($arr[$key])) {
- $this->packFileNum($arr[$key]);
- }else {
- $num++;
- }
- }
- return $num;
- }
- //头像
- public function makeAvatar($imagePath,$width,$height)
- {
- $hash = $imagePath?md5($imagePath):'noavatar';
- $img = \app\modules\admin\models\Attachment::find()->where("hash='".$hash."'")->limit(1)->one();
- if(!$img&&is_net_file($imagePath))
- {
- return $imagePath;
- }
- else
- {
- return APP_URL.'avt/'.$width.'/'.$height.'/'.$hash.'.jpg';
- }
- }
- //缩略图
- public function makeThumb($imagePath,$width,$height)
- {
- if(Yii::$app->params['oss']['OPEN_OSS']&&Yii::$app->params['oss']['OSS_THUMB'])
- {
- $fileUrl = getFileUrl($imagePath);
- if(Yii::$app->params['oss']['OSS_TYPE']=='tencent')
- {
- return $fileUrl.'?imageMogr2/thumbnail/'.$width.'x'.$height;
- }
- if(Yii::$app->params['oss']['OSS_TYPE']=='ali')
- {
- return $fileUrl.'?x-oss-process=image/resize,m_fill,h_'.$height.',w_'.$width;
- }
- if(Yii::$app->params['oss']['OSS_TYPE']=='baidu')
- {
- return $fileUrl.'?x-bce-process=image/resize,m_fill,w_'.$width.',h_'.$height;
- }
- else
- {
- return $fileUrl;
- }
- }
- else
- {
- $hash = $imagePath?md5($imagePath):'noimage';
- return APP_URL.'p/'.$width.'/'.$height.'/'.$hash.'.jpg';
- }
- }
- //当前用户是否有权限访问和操作所请求的资源
- public function checkRights($user_id,$model,$fild_name='user_id')
- {
- if ($model->$fild_name != $user_id){
- if (Yii::$app->request->isAjax) {
- $msgdata = ['error' => 1,'msg' => '没有操作权限'];
- echo_json($msgdata);
- }
- else
- {
- throw new \yii\web\ForbiddenHttpException('你只能操作你自己创作的数据');
- }
- }
- else
- {
- return true;
- }
- }
- //解密价值平台字串
- public function getIdentityInfoByToken($str){
- if ($str == null) {
- return false;
- }
- //Cookie::setCookie('data_str',$str);
- $str = str_replace("\\\\n", "", $str);
- $str = str_replace("/add/", "+", $str);
- // 解密
- $decrypted = json_decode(openssl_decrypt(base64_decode($str),"AES-128-ECB", '2na$$PdV9AW8b#CS',OPENSSL_RAW_DATA),true);
- //var_dump($decrypted);
- if(!empty($decrypted) && is_array($decrypted) && isset($decrypted['LOGIN_ID']) && isset($decrypted['APP_ID']) && isset($decrypted['TIME_STAMP']) && isset($decrypted['REQUEST_URL'])){
- //if(time()-5*60 > strtotime($decrypted['TIME_STAMP']) || time() + 5*60 < strtotime($decrypted['TIME_STAMP'])) return 'token';
- $identityInfo = array();
- $identityInfo['admin_id']=1;
- $identityInfo['user_name']=$decrypted['LOGIN_ID'];
- $identityInfo['role_id']=1;
- $identityInfo['real_name']='管理员';
- $identityInfo['role_name']='超级管理员';
- $identityInfo['cookieHash'] = sys_auth($decrypted['LOGIN_ID']);
- $identityInfo['login_type'] = 'token';
- $info = sys_auth(array2string($identityInfo));
- Yii::$app->session['adminInfo']=$info;
- Cookie::setCookie(Yii::$app->params['adminCookieName'],$info);//使用COOKIE记录用户身份信息
- //为锁屏功能设置的一个开关变量
- Cookie::setCookie('lockscreen',0);
- return $identityInfo;
- }
- return 'token';
- }
- }
|