BaseController.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace mall\controllers;
  3. use bizMall\merchant\services\MerchantExtendService;
  4. use bizMall\merchant\services\MerchantService;
  5. use bizMall\user\services\UserService;
  6. use common\components\httpUtil;
  7. use common\components\jwt;
  8. use common\components\util;
  9. class BaseController extends PublicController
  10. {
  11. public $validate = true;
  12. public $withoutLogin = [];//不需要登陆可以访问的方法
  13. public $userId = 0, $user, $shopId;
  14. public $merchantId, $merchant, $merchantExtend;
  15. public $isWx;
  16. public function beforeAction($action)
  17. {
  18. //token验证
  19. $userId = jwt::getLoginId();
  20. $header = httpUtil::getHeader();
  21. $merchantId = isset($header['account']) ? $header['account'] : 0;
  22. $shopId = isset($header['shopId']) ? $header['shopId'] : 0;
  23. if (empty($merchantId)) {
  24. util::fail('商家无效');
  25. }
  26. $merchant = MerchantService::getMerchantById($merchantId);
  27. if (empty($merchant)) {
  28. util::fail('商家信息无效');
  29. }
  30. $this->merchantId = $merchantId;
  31. $this->merchant = $merchant;
  32. $this->shopId = $shopId;
  33. $this->merchantExtend = MerchantExtendService::getByMerchantId($merchantId);
  34. $this->isWx = util::isWx();
  35. if (empty($userId)) {
  36. $this->validate = false;
  37. } else {
  38. $this->validate = true;
  39. $this->userId = $userId;
  40. $userInfo = UserService::getUserInfo($userId);
  41. $this->user = $userInfo;
  42. }
  43. //游客可以访问的方法
  44. if (in_array($action->id, $this->withoutLogin)) {
  45. $this->validate = true;
  46. }
  47. if ($this->validate == false) {
  48. util::notLogin();
  49. }
  50. return parent::beforeAction($action);
  51. }
  52. }