BaseController.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace client\controllers;
  3. use biz\stat\services\StatVisitService;
  4. use Yii;
  5. use biz\merchant\services\MerchantExtendService;
  6. use biz\merchant\services\MerchantService;
  7. use biz\user\services\UserService;
  8. use common\components\httpUtil;
  9. use common\components\jwt;
  10. use common\components\util;
  11. /**
  12. * Created by PhpStorm.
  13. * User: shish <shish@zhhinc.com>
  14. * Date: 2019/11/18 0018
  15. * Time: 14:58
  16. */
  17. class BaseController extends PublicController
  18. {
  19. public $validate = true;
  20. public $withoutLogin = [];//不需要登陆可以访问的方法
  21. public $userId = 0, $user, $shopId;
  22. public $merchantId, $merchant, $merchantExtend;
  23. public $isWx;
  24. public function beforeAction($action)
  25. {
  26. //token验证
  27. $userId = jwt::getLoginId();
  28. $header = httpUtil::getHeader();
  29. $account = isset($header['account']) ? $header['account'] : 0;
  30. $shopId = isset($header['shopId']) ? $header['shopId'] : 0;
  31. if (empty($account)) {
  32. util::fail('商家无效');
  33. }
  34. $merchant = MerchantService::getMerchantById($account);
  35. if (empty($merchant)) {
  36. util::fail('商家信息无效');
  37. }
  38. $this->merchantId = $account;
  39. $this->merchant = $merchant;
  40. $this->shopId = $shopId;
  41. $this->merchantExtend = MerchantExtendService::getByMerchantId($account);
  42. $this->isWx = util::isWx();
  43. //收集访问量
  44. StatVisitService::gatherVisit($account);
  45. if (empty($userId)) {
  46. $this->validate = false;
  47. } else {
  48. $this->validate = true;
  49. $this->userId = $userId;
  50. $userInfo = UserService::getUserInfo($userId);
  51. $this->user = $userInfo;
  52. //客户短时间访问二家商家的商城,登陆信息一致性问题
  53. if ($userInfo['merchantId'] != $account) {
  54. util::logout();
  55. }
  56. }
  57. //游客可以访问的方法
  58. if (in_array($action->id, $this->withoutLogin)) {
  59. $this->validate = true;
  60. }
  61. if ($this->validate == false) {
  62. util::notLogin();
  63. }
  64. return parent::beforeAction($action);
  65. }
  66. }