BaseController.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace mall\controllers;
  3. use biz\shop\classes\ShopClass;
  4. use bizHd\custom\classes\CustomClass;
  5. use bizMall\merchant\services\MerchantExtendService;
  6. use bizMall\merchant\services\MerchantService;
  7. use bizMall\user\services\UserService;
  8. use common\components\httpUtil;
  9. use common\components\jwt;
  10. use common\components\util;
  11. class BaseController extends PublicController
  12. {
  13. public $validate = true;
  14. public $guestAccessAction = [];
  15. public $userId = 0, $user, $shopId;
  16. public $sjId, $sj, $sjExtend, $customId = 0;
  17. public $isWx;
  18. public function beforeAction($action)
  19. {
  20. $userId = jwt::getLoginId();
  21. $header = httpUtil::getHeader();
  22. $shopId = isset($header['account']) ? $header['account'] : 0;
  23. if (empty($shopId)) {
  24. util::fail('没有找到门店');
  25. }
  26. $shop = ShopClass::getById($shopId);
  27. if (empty($shop)) {
  28. util::fail('没有找到门店哦');
  29. }
  30. $sjId = $shop['merchantId'] ?? 0;
  31. if (empty($sjId)) {
  32. util::fail('没有找到商家哦');
  33. }
  34. $sj = MerchantService::getMerchantById($sjId);
  35. if (empty($sj)) {
  36. util::fail('没有找到商家');
  37. }
  38. $this->sjId = $sjId;
  39. $this->sj = $sj;
  40. $this->shopId = $shopId;
  41. $this->sjExtend = MerchantExtendService::getByMerchantId($sjId);
  42. $this->isWx = util::isWx();
  43. if (empty($userId)) {
  44. $this->validate = false;
  45. } else {
  46. $this->validate = true;
  47. $this->userId = $userId;
  48. $userInfo = UserService::getUserInfo($userId);
  49. if (empty($userInfo)) {
  50. util::fail('没有用户信息');
  51. }
  52. $this->user = $userInfo;
  53. $customId = CustomClass::getCurrentCustomId($sjId, $userInfo);
  54. $this->customId = $customId;
  55. }
  56. //游客可以访问的方法
  57. if (in_array($action->id, $this->guestAccessAction)) {
  58. $this->validate = true;
  59. }
  60. if ($this->validate == false) {
  61. util::notLogin();
  62. }
  63. return parent::beforeAction($action);
  64. }
  65. }