request->get(); $account = isset($get['account']) ? $get['account'] : ''; $wantLogin = isset($get['wantLogin']) ? $get['wantLogin'] : 'no'; $merchant = xhMerchantService::getByAccount($account); if (empty($merchant)) { util::stop('店铺不存在'); } $session = Yii::$app->session; if (isset($session['visitingMerchantAccount']) && $session['visitingMerchantAccount'] != $account) { xhUserService::logout(); } $this->error = error::instance(); $this->isWeixin = util::isWeixin(); $this->isLogin = xhUserService::isLogin(); $merchantId = $merchant['id']; $this->merchantId = $merchantId; //全局变量 Yii::$app->params['merchantId'] = $merchantId; Yii::$app->params['merchant'] = $merchant; $this->merchant = $merchant; $this->merchantName = $merchant['merchantName']; $this->appId = $merchant['wxAppId']; $this->appSecret = $merchant['wxAppSecret']; if ($this->isWeixin) { $jssdk = new jsSDK();//微信分享 $this->signPackage = $jssdk->GetSignPackage($merchant); } $noNeedLogin = false; if (in_array($action->id, $this->withoutLogin)) {//游客可以访问的方法 $noNeedLogin = true; } if ($wantLogin == 'yes') { $noNeedLogin = false; } $userId = xhUserService::getId(); Yii::$app->params['isLogin'] = false; if ($this->isLogin && !empty($userId)) { $this->userId = $userId; $user = xhUserService::getById($userId); //帐户已经因合并等原因注销,退出登陆并返回首页 if (empty($user['merchantId']) || $user['status'] == 2) { xhUserService::logout(); $this->redirect(Yii::$app->params['mobileUrl'] . '/mobile/index?account=' . $account); } $this->user = $user; $this->userName = $user['userName']; $noNeedLogin = true; Yii::$app->params['userId'] = $userId; Yii::$app->params['user'] = $user; Yii::$app->params['isLogin'] = true; } else { /**如果没有登陆,同时授权参数带了snsapi_userinfo要获取完全用户信息,则必须登陆。否则不应该带此参数**/ if (isset($get['authScope']) && $get['authScope'] == 'snsapi_userinfo') { $noNeedLogin = false; } } /**更新用户访问时间**/ if (!empty($userId)) { UserService::updateVisitTime(); } if ($this->isWeixin == false) {//顺序很重要 if ($action->controller->id == 'pay' && $action->id == 'index') { //非微信访问(其它浏览器访问)付款页不需要登陆 $noNeedLogin = true; } } //后台默认的输出方式是网页 Yii::$app->params['errorExportStyle'] = 'json'; Yii::$app->params['appStyle'] = 'mobile'; if ($this->isWeixin) {//如果是微信,走微信授权取用户信息 if ($noNeedLogin == false) { /** * authScope 参数的说明 * 1.用户通过公众号菜单打开网页,因为已经关注过了公众号,已经生成过用户信息,所以只要使用snsapi_base静默方式拿到openid就可以 * 2.为了保证付款页的访问速度,暂时只要通过snsapi_base静默方式拿到openid先生成可以用的用户信息即可 * 3.其它情况需要获取用户完整信息的,使用snsapi_userinfo让用户授权获取信息即可 * 4.snsapi_base静默方式使用的场景更多,所以参数authScope默认使用snsapi_base */ $authScope = isset($get['authScope']) ? $get['authScope'] : 'snsapi_base'; $params = empty($_SERVER['REQUEST_URI']) ? '' : base64_encode($_SERVER['REQUEST_URI']);//取主域名后面的参数 $url = 'http://' . $_SERVER['HTTP_HOST'] . '/auth/get-user-info?params=' . $params . '&authScope=' . $authScope; weixinUtil::getToken($url, $merchant, $authScope); util::end(); } } else { if ($noNeedLogin == false) { $this->redirect(['mobile/login', 'account' => $account]); util::stop(); } } $this->cateogryList = CategoryRelateService::getShowList(4); $extend = xhMerchantExtendService::getByMerchantId($merchantId); $this->merchantExtend = $extend; if (!empty($extend)) { $this->webTitle = $extend['webTitle']; $this->webDescription = $extend['webDescription']; $this->webKeyword = $extend['webKeyword']; } VisitByDayService::gatherIP($merchantId);//统计每天访问人数 return parent::beforeAction($action); } /** * 商户数据权限检测 * @param $data * @return bool */ protected function merchantCheck($data, $account, $msg = '') { if ($data['merchantId'] != $this->merchantId) { $url = \yii\helpers\Url::to(['error/error-page']) . "?account=" . $account . "&statusCode=illegalMerchantId"; $this->redirect($url); Yii::$app->end(); } return true; } /** * 异常错误提示(提高用户体验,不暴露系统敏感数据) * @param $account */ protected function error($account, $errorType = 'illegalData', $title = '', $msg = '') { $url = \yii\helpers\Url::to(['error/error-page']) . "?account=" . $account . "&statusCode=" . $errorType . "&title=" . $title . "&msg=" . $msg; $this->redirect($url); Yii::$app->end(); } //方法里错误输出设置为json shish 2019.8.25 public function errorExportWeb() { Yii::$app->params['errorExportStyle'] = 'web'; } }