| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace mobile\controllers;
- use biz\goods\services\CategoryRelateService;
- use biz\stat\services\VisitByDayService;
- use biz\user\services\UserService;
- use common\components\error;
- use Yii;
- use common\services\xhMerchantExtendService;
- use common\services\xhUserService;
- use common\components\jsSDK;
- use common\components\weixinUtil;
- use common\components\util;
- use common\services\xhMerchantService;
- /**
- * 次级基类,次于PublicController
- */
- class MobileendController extends PublicController
- {
-
- public $merchantId, $merchant, $merchantExtend, $merchantName, $merchantShowName;
- public $cateogryList;//菜单分类
- public $isWeixin = false;
- public $appId, $appSecret, $signPackage;
- public $user, $userName, $userId = 0, $isLogin = false, $version;
- public $withoutLogin = [];//不需要登陆的方法名
- public $error;
-
- public function beforeAction($action)
- {
- $get = Yii::$app->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';
- }
-
- }
|