* @date 2015/07/01 */ class BaseController extends AppController { /** * CSR验证,关闭后可以在不提交CSR验证码的情况下通过POST方式提交数据 * @var bool */ public $enableCsrfValidation = false; /** @var **/ protected $wx; /** @var **/ protected $site; /** * 初始化应用控制器,确保 mall id 的存在,有效启动程序,否则抛出异常。 * @throws yii\web\BadRequestHttpException * @throws yii\web\BadRequestHttpException */ public function init() { $this->initTempFilePaths(); //AppLog::DEBUG("BaseController: init()" ); parent::init(); $wxArray = $this->getWxArray(); if (empty($wxArray)) { throw new BadRequestHttpException(404); } //重置应用的名称 Yii::$app->name = $wxArray['name']; /** * 微信服务号配置 */ $this->wx = new stdClass(); $this->site = new stdClass(); $this->formatWx($wxArray); $this->formatSite($wxArray); $view = Yii::$app->view; $view->params['site']= $this->site; } /** * @return array|bool */ private function getWxArray() { $wxQuery = new Query(); $wxQuery->select(['mp_setting.*']); $wxQuery->from('mp_setting'); //调用 Active Query 得到一个数组的结果 $wxArray = $wxQuery->one(); return $wxArray; } /** * @param $wxArray */ private function formatWx($wxArray) { /** * 工程师微信服务号信息 */ $this->wx->name = $wxArray['name'];//名称 $this->wx->intro = $wxArray['intro'];//简介 $this->wx->logo_url = $wxArray['logo_path'];//路径 $this->wx->subscribe_title = $wxArray['subscribe_title']; // 公众号关注图文标题 $this->wx->subscribe_desc = $wxArray['subscribe_desc']; // 公众号关注图文描述 $this->wx->subscribe_img = $wxArray['subscribe_img']; // 公众号关注图文消息图片 $this->wx->subscribe_url = $wxArray['subscribe_url']; // 公众号关注图文url $this->wx->appid = $wxArray['appid'];// APPID $this->wx->appsecret = $wxArray['appsecret'];//APPSECRET $this->wx->token = $wxArray['token'];//微信TOKEN } /** * 获取 ID * @return int|string */ public function getUserId() { if (isset(Yii::$app->user)) { return Yii::$app->user->id; } else { return 0; } } /** * @param $wxArray */ private function formatSite($wxArray) { /** * 站点信息 */ $appUser = Yii::$app->getUser(); $url = Yii::$app->request->getHostInfo() . Yii::$app->request->url; $this->site->url = $url; if (YII_ENV_DEV) { $this->site->base_url = Yii::$app->request->getHostInfo() . Yii::$app->request->baseUrl; } else { $this->site->base_url = Yii::$app->request->getHostInfo(); } $am = Yii::$app->view->getAssetManager(); $assets = $am->getBundle('app\wx\assets\AppAsset'); $this->site->is_android = (int)$this->isAndroid(); $this->site->assets_url = $assets->baseUrl; $this->site->is_login = !$appUser->isGuest; $this->site->title = $wxArray['name']; $search = array("\t", "\n", "\r"); $this->site->desc = str_replace($search, '
', $wxArray['intro']); $this->site->icon = ImageManager::getUrl($wxArray['logo_path']); $sign = WxHelper::getWxJSSDK()->getSignContext(); $this->site->appid = $sign->appId; $this->site->timestamp = $sign->timestamp; $this->site->noncestr = $sign->nonceStr; $this->site->signature = $sign->signature; } /** * @return yii\web\Response */ protected function handleMallAccessCtrl() { //AppLog::DEBUG("BaseController: handleMallAccessCtrl()" ); $appUser = Yii::$app->getUser(); if(YII_ENV_DEV && $appUser->isGuest){ $uid = Yii::$app->params['testUID']; // 请在各自的config/params-local.php里配置 $appUser->login(UserRepository::findOne($uid)); $this->goHome(); return ; } //test和 prod 环境,进行微信登陆 if ($appUser->isGuest){ $this->handleOAuthRedirect(); } else { $path = $this->request->get('jumppath'); $model= $this->request->get('jumpmodel'); $paramOne= $this->request->get('paramOne'); if (!empty($path) && !empty($model)) { $locationUrl = Yii::$app->params["baseUrl"]."/".$model."#".$path; if (!empty($paramOne)) { $locationUrl .= "/".urldecode($paramOne); } header("Location: $locationUrl");exit; } } } /** * @return yii\web\Response */ private function handleOAuthRedirect() { $redirectUri = WxHelper::getDomain(true) . Url::toRoute(['/wechat/login-proccess', 'dest' => $this->site->url]); AppLog::DEBUG("BaseController::handleOAuthRedirect() --> 开始重定向跳转 redirectUri={$redirectUri}"); $wechat = WxHelper::getWxPHPSDK(); $OAuthUrl = $wechat->getOAuthRedirect($redirectUri, 'smart', 'snsapi_base'); AppLog::DEBUG("BaseController::handleOAuthRedirect() --> 开始重定向跳转 OAuthUrl={$OAuthUrl}"); Yii::$app->response->redirect($OAuthUrl)->send(); Yii::$app->end(); } /** * 是否是IOS端 * 是否通过微信客户端来访问 * LOG 记录时发现, * 在微信公众号里面第一次访问: $_SERVER ['HTTP_USER_AGENT'] 为 Mozilla/4.0 * 第二次访问: $_SERVER ['HTTP_USER_AGENT'] 为 * 苹果: * Mozilla/5.0 (iPhone; CPU iPhone OS 9_3_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13E238 MicroMessenger/6.3.16 NetType/WIFI Language/zh_CN * 安卓(小米NOTE): * Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9b4) Gecko/2008030317 Firefox/3.0b4 * @return bool */ protected function isAndroid() { // if(false !== strpos($_SERVER ['HTTP_USER_AGENT'], 'Android') || !isset($_SERVER ['HTTP_USER_AGENT'])){ // return true; // } else { // return false; // } return true; } /** 用来验证签名 * @param $sign * @param $revFromOpenId * @param $location * @param $tt * @return bool */ /* public function checkSign($sign,$revFromOpenId,$location,$tt){ $mySign = md5('openid='.$revFromOpenId.'&location_x='.$location['x'].'&time='.$tt.'&key='.$this->vKey); if($sign==$mySign){return true;}else{return false;} } */ /** * @param $revFromOpenId * @param $location * @return string */ /* public function genSign($revFromOpenId,$location){ $tt = time(); $mySign = md5('openid='.$revFromOpenId.'&location_x='.$location['x'].'&time='.$tt.'&key='.$this->vKey); return $mySign; } */ /** * 重定向页面 * @param bool $return */ private function redirectPage($return = false , $pageUrl = "") { $redirect = $this->site->base_url; $redirect = $redirect . $pageUrl; if ($return == true) { return array('success' => true,'redirect_url' => $redirect); } else { $this->redirect($redirect); } } /** 判断用户是否被封号或者禁用 * @param $userStatus * @return array */ public function checkEngineerBlockOrDisable($userStatus, $redirect = false) { if(EngineerStatus::STATUS_BLOCK == $userStatus){ //封号直接跳转 if (true == $redirect) { $blockUrl = $this->site->base_url.'/site/block'; $this->redirect($blockUrl); } else { $disableUrl = $this->site->base_url.'/site/block'; return array('success' => 0 - $userStatus,'msg' => '您的账号已被封号','redirect_url' => $disableUrl); } } elseif (EngineerStatus::STATUS_DISABLE == $userStatus) { //禁用表示还可以进入,但是要做其他处理 return array('success' => 0-$userStatus,'msg'=>' 您的账号已被禁用!'); } elseif (EngineerStatus::STATUS_SKILL_FAIL != $userStatus) { //技能审核失败 } else { } return array('success'=>0,'msg'=>'null'); } /* 下面这个作废 */ public function checkUserStatus($userStatus, $redirect = false) { if(EngineerStatus::STATUS_BLOCK == $userStatus){ //封号直接跳转 if (true == $redirect) { $blockUrl = $this->site->base_url.'/site/block'; $this->redirect($blockUrl); } else { $disableUrl = $this->site->base_url.'/site/block'; return array('success' => 0 - $userStatus,'msg' => '您的账号已被封号','redirect_url' => $disableUrl); } } elseif (EngineerStatus::STATUS_DISABLE == $userStatus) { //禁用表示还可以进入,但是要做其他处理 return array('success' => 0-$userStatus,'msg'=>' 您的账号已被禁用!'); } elseif (EngineerStatus::STATUS_SKILL_FAIL != $userStatus) { //技能审核失败 } else { } return array('success'=>0,'msg'=>'null'); } /* public function authTable($userStatus) { $controllerID = Yii::$app->controller->id; $actionID = Yii::$app->controller->action->id; $moduleID=$this->module->id; //echo 'moduleId:'.$moduleID.'_controllerID:'.$controllerID.'_actionID:'.$actionID; if($userStatus == EngineerStatus::STATUS_NOT_AUTH){ //toDo } } */ // public function setPhoneCode($phone, $code) { Yii::$app->session['v_'.$phone] = array('create_time' => time(),'vCode' => md5($phone.'_'.$code)); } public function getPhoneCode($phone) { return Yii::$app->session['v_'.$phone]; } // 默认是300秒 public function validatePhoneCode($phone, $code, $vaTime = 300) { $pCode = $this->getPhoneCode($phone); $cTime = time(); if (empty($pCode)) { return -3; } if ($cTime > ($pCode['create_time'] + $vaTime)) { $this->unsetPhoneCode($phone); return -1; } if($pCode['vCode'] == md5($phone.'_'.$code)){ return 0; }else{ return -2; } } public function unsetPhoneCode($phone) { unset(Yii::$app->session['v_'.$phone]); } public function checkClientStatus() { $stop = SysSetting::checkClientSiteStop(); if ($stop) { $blockUrl = $this->site->base_url.'/error/stop'; //$this->redirect($blockUrl); header('Location:'.$blockUrl); exit; } return true; } /** * 判断是否是BD成员 */ public function isBdRole($engineerModel) { $bdRoles = EngineerRole::getBdVisitedRoles(); $engineerStatus = EngineerStatus::getBdVisitedStatus(); if (in_array($engineerModel->role, $bdRoles) && in_array($engineerModel->status, $engineerStatus)) { return true; } else { return false; } } public function checkIsBdRoleAndWarning() { $appUser = Yii::$app->getUser(); $userId = $appUser->id; if (empty($userId)) { $redirectUrl = $this->site->base_url.'/site/no-oauth'; $this->redirect($redirectUrl); } $engineerModel = Engineer::findOne($userId); if (empty($engineerModel)) { $redirectUrl = $this->site->base_url.'/site/no-oauth'; $this->redirect($redirectUrl); } //AppLog::DEBUG('== checkIsBdRoleAndWarning =='.$engineerModel->id); if (!$this->isBdRole($engineerModel)) { $redirectUrl = $this->site->base_url.'/site/no-oauth'; $this->redirect($redirectUrl); } } /** * 判断是否是工程师成员 */ public function isEngineerRole($engineerModel) { $roles = EngineerRole::getVisitedRoles(); $engineerStatus = EngineerStatus::getVisitedStatus(); if (in_array($engineerModel->role, $roles) && in_array($engineerModel->status, $engineerStatus)) { return true; } else { return false; } } public function checkIsEngineerRoleAndWarning() { $appUser = Yii::$app->getUser(); $userId = $appUser->id; if (empty($userId)) { $redirectUrl = $this->site->base_url.'/site/no-oauth'; $this->redirect($redirectUrl); } $engineerModel = Engineer::findOne($userId); if (empty($engineerModel)) { $redirectUrl = $this->site->base_url.'/site/no-oauth'; $this->redirect($redirectUrl); } if (!$this->isEngineerRole($engineerModel)) { $redirectUrl = $this->site->base_url.'/site/no-oauth'; $this->redirect($redirectUrl); } } /** 判断用户是否被封号或者禁用 * @param $userStatus * @return array */ public function checkEngineerSubscribe($subscribe) { if(EngineerStatus::NOSUBSCRIBE== $subscribe) { //未关注直接跳转 $subscribeUrl = $this->site->base_url.'/site/subscribe'; $this->redirect($subscribeUrl); } } /** * 初始化web目录文件夹, 完成自动创建 */ private function initTempFilePaths() { // 临时文件 $webRoot = Yii::getAlias('@webroot'); $tmpFilePath = $webRoot . '/tmp'; if (!is_dir($tmpFilePath)) { mkdir($tmpFilePath, 0777, true); } } /**检测用户去到哪个状态,根据状态跳转页面 * @param string $view * @param bool|false $return 标明要直接跳转还是要返回数组 * @return array */ public function checkUserlevel($view = '', $return = false, $needPower = "default") { // getIdentity 可能要定时更新session 才行,不然审核通过后无法更新这个数据 $user = Yii::$app->getUser()->getIdentity(); switch ($needPower) { case "none": // 无需验证 if ($return == true) { return array('success'=> false, 'redirect_url'=>''); } break; default: break; } } }