BaseController.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace mall\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. $merchantId = isset($header['account']) ? $header['account'] : 0;
  30. $shopId = isset($header['shopId']) ? $header['shopId'] : 0;
  31. if (empty($merchantId)) {
  32. util::fail('商家无效');
  33. }
  34. $merchant = MerchantService::getMerchantById($merchantId);
  35. if (empty($merchant)) {
  36. util::fail('商家信息无效');
  37. }
  38. $this->merchantId = $merchantId;
  39. $this->merchant = $merchant;
  40. $this->shopId = $shopId;
  41. $this->merchantExtend = MerchantExtendService::getByMerchantId($merchantId);
  42. $this->isWx = util::isWx();
  43. //收集访问量
  44. StatVisitService::gatherVisit($merchantId);
  45. //本地环境没有token时模拟一个可以用的帐号
  46. if (getenv('YII_ENV') == 'local') {
  47. $one = UserService::getByCondition(['merchantId'=>$merchantId]);
  48. if (!empty($one)) {
  49. $userId = $one['id'];
  50. }
  51. }
  52. if (empty($userId)) {
  53. $this->validate = false;
  54. } else {
  55. $this->validate = true;
  56. $this->userId = $userId;
  57. $userInfo = UserService::getUserInfo($userId);
  58. $this->user = $userInfo;
  59. //更新用户访问时间
  60. UserService::updateVisitTime($merchantId, $userId);
  61. //客户短时间访问二家商家的商城,登陆信息一致性问题
  62. if ($userInfo['merchantId'] != $merchantId) {
  63. util::logout();
  64. }
  65. }
  66. //游客可以访问的方法
  67. if (in_array($action->id, $this->withoutLogin)) {
  68. $this->validate = true;
  69. }
  70. if ($this->validate == false) {
  71. util::notLogin();
  72. }
  73. return parent::beforeAction($action);
  74. }
  75. }