MobileendController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace mobile\controllers;
  3. use biz\goods\services\CategoryRelateService;
  4. use biz\stat\services\VisitByDayService;
  5. use biz\user\services\UserService;
  6. use common\components\error;
  7. use Yii;
  8. use common\services\xhMerchantExtendService;
  9. use common\services\xhUserService;
  10. use common\components\jsSDK;
  11. use common\components\weixinUtil;
  12. use common\components\util;
  13. use common\services\xhMerchantService;
  14. /**
  15. * 次级基类,次于PublicController
  16. */
  17. class MobileendController extends PublicController
  18. {
  19. public $merchantId, $merchant, $merchantExtend, $merchantName, $merchantShowName;
  20. public $cateogryList;//菜单分类
  21. public $isWeixin = false;
  22. public $appId, $appSecret, $signPackage;
  23. public $user, $userName, $userId = 0, $isLogin = false, $version;
  24. public $withoutLogin = [];//不需要登陆的方法名
  25. public $error;
  26. public function beforeAction($action)
  27. {
  28. $get = Yii::$app->request->get();
  29. $account = isset($get['account']) ? $get['account'] : '';
  30. $wantLogin = isset($get['wantLogin']) ? $get['wantLogin'] : 'no';
  31. $merchant = xhMerchantService::getByAccount($account);
  32. if (empty($merchant)) {
  33. util::stop('店铺不存在');
  34. }
  35. $session = Yii::$app->session;
  36. if (isset($session['visitingMerchantAccount']) && $session['visitingMerchantAccount'] != $account) {
  37. xhUserService::logout();
  38. }
  39. $this->error = error::instance();
  40. $this->isWeixin = util::isWeixin();
  41. $this->isLogin = xhUserService::isLogin();
  42. $merchantId = $merchant['id'];
  43. $this->merchantId = $merchantId;
  44. //全局变量
  45. Yii::$app->params['merchantId'] = $merchantId;
  46. Yii::$app->params['merchant'] = $merchant;
  47. $this->merchant = $merchant;
  48. $this->merchantName = $merchant['merchantName'];
  49. $this->appId = $merchant['wxAppId'];
  50. $this->appSecret = $merchant['wxAppSecret'];
  51. if ($this->isWeixin) {
  52. $jssdk = new jsSDK();//微信分享
  53. $this->signPackage = $jssdk->GetSignPackage($merchant);
  54. }
  55. $noNeedLogin = false;
  56. if (in_array($action->id, $this->withoutLogin)) {//游客可以访问的方法
  57. $noNeedLogin = true;
  58. }
  59. if ($wantLogin == 'yes') {
  60. $noNeedLogin = false;
  61. }
  62. $userId = xhUserService::getId();
  63. Yii::$app->params['isLogin'] = false;
  64. if ($this->isLogin && !empty($userId)) {
  65. $this->userId = $userId;
  66. $user = xhUserService::getById($userId);
  67. //帐户已经因合并等原因注销,退出登陆并返回首页
  68. if (empty($user['merchantId']) || $user['status'] == 2) {
  69. xhUserService::logout();
  70. $this->redirect(Yii::$app->params['mobileUrl'] . '/mobile/index?account=' . $account);
  71. }
  72. $this->user = $user;
  73. $this->userName = $user['userName'];
  74. $noNeedLogin = true;
  75. Yii::$app->params['userId'] = $userId;
  76. Yii::$app->params['user'] = $user;
  77. Yii::$app->params['isLogin'] = true;
  78. } else {
  79. /**如果没有登陆,同时授权参数带了snsapi_userinfo要获取完全用户信息,则必须登陆。否则不应该带此参数**/
  80. if (isset($get['authScope']) && $get['authScope'] == 'snsapi_userinfo') {
  81. $noNeedLogin = false;
  82. }
  83. }
  84. /**更新用户访问时间**/
  85. if (!empty($userId)) {
  86. UserService::updateVisitTime();
  87. }
  88. if ($this->isWeixin == false) {//顺序很重要
  89. if ($action->controller->id == 'pay' && $action->id == 'index') {
  90. //非微信访问(其它浏览器访问)付款页不需要登陆
  91. $noNeedLogin = true;
  92. }
  93. }
  94. //后台默认的输出方式是网页
  95. Yii::$app->params['errorExportStyle'] = 'json';
  96. Yii::$app->params['appStyle'] = 'mobile';
  97. if ($this->isWeixin) {//如果是微信,走微信授权取用户信息
  98. if ($noNeedLogin == false) {
  99. /**
  100. * authScope 参数的说明
  101. * 1.用户通过公众号菜单打开网页,因为已经关注过了公众号,已经生成过用户信息,所以只要使用snsapi_base静默方式拿到openid就可以
  102. * 2.为了保证付款页的访问速度,暂时只要通过snsapi_base静默方式拿到openid先生成可以用的用户信息即可
  103. * 3.其它情况需要获取用户完整信息的,使用snsapi_userinfo让用户授权获取信息即可
  104. * 4.snsapi_base静默方式使用的场景更多,所以参数authScope默认使用snsapi_base
  105. */
  106. $authScope = isset($get['authScope']) ? $get['authScope'] : 'snsapi_base';
  107. $params = empty($_SERVER['REQUEST_URI']) ? '' : base64_encode($_SERVER['REQUEST_URI']);//取主域名后面的参数
  108. $url = 'http://' . $_SERVER['HTTP_HOST'] . '/auth/get-user-info?params=' . $params . '&authScope=' . $authScope;
  109. weixinUtil::getToken($url, $merchant, $authScope);
  110. util::end();
  111. }
  112. } else {
  113. if ($noNeedLogin == false) {
  114. $this->redirect(['mobile/login', 'account' => $account]);
  115. util::stop();
  116. }
  117. }
  118. $this->cateogryList = CategoryRelateService::getShowList(4);
  119. $extend = xhMerchantExtendService::getByMerchantId($merchantId);
  120. $this->merchantExtend = $extend;
  121. if (!empty($extend)) {
  122. $this->webTitle = $extend['webTitle'];
  123. $this->webDescription = $extend['webDescription'];
  124. $this->webKeyword = $extend['webKeyword'];
  125. }
  126. VisitByDayService::gatherIP($merchantId);//统计每天访问人数
  127. return parent::beforeAction($action);
  128. }
  129. /**
  130. * 商户数据权限检测
  131. * @param $data
  132. * @return bool
  133. */
  134. protected function merchantCheck($data, $account, $msg = '')
  135. {
  136. if ($data['merchantId'] != $this->merchantId) {
  137. $url = \yii\helpers\Url::to(['error/error-page']) . "?account=" . $account . "&statusCode=illegalMerchantId";
  138. $this->redirect($url);
  139. Yii::$app->end();
  140. }
  141. return true;
  142. }
  143. /**
  144. * 异常错误提示(提高用户体验,不暴露系统敏感数据)
  145. * @param $account
  146. */
  147. protected function error($account, $errorType = 'illegalData', $title = '', $msg = '')
  148. {
  149. $url = \yii\helpers\Url::to(['error/error-page']) . "?account=" . $account . "&statusCode=" . $errorType . "&title=" . $title . "&msg=" . $msg;
  150. $this->redirect($url);
  151. Yii::$app->end();
  152. }
  153. //方法里错误输出设置为json shish 2019.8.25
  154. public function errorExportWeb()
  155. {
  156. Yii::$app->params['errorExportStyle'] = 'web';
  157. }
  158. }