BaseController.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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, $custom = [];
  17. public $isWx;
  18. public function beforeAction($action)
  19. {
  20. //定义平台类型 1花店 2供货商 3商城
  21. Yii::$app->params['ptStyle'] = 3;
  22. $userId = jwt::getLoginId();
  23. $header = httpUtil::getHeader();
  24. $shopId = isset($header['account']) ? $header['account'] : 0;
  25. if (empty($shopId)) {
  26. util::fail('没有找到门店');
  27. }
  28. $shop = ShopClass::getById($shopId);
  29. if (empty($shop)) {
  30. util::fail('没有找到门店哦');
  31. }
  32. $sjId = $shop['merchantId'] ?? 0;
  33. if (empty($sjId)) {
  34. util::fail('没有找到商家哦');
  35. }
  36. $sj = MerchantService::getMerchantById($sjId);
  37. if (empty($sj)) {
  38. util::fail('没有找到商家');
  39. }
  40. $this->sjId = $sjId;
  41. $this->sj = $sj;
  42. $this->shopId = $shopId;
  43. $this->sjExtend = MerchantExtendService::getByMerchantId($sjId);
  44. $this->isWx = util::isWx();
  45. if (empty($userId)) {
  46. $this->validate = false;
  47. } else {
  48. $this->validate = true;
  49. $this->userId = $userId;
  50. $userInfo = UserService::getUserInfo($userId);
  51. if (empty($userInfo)) {
  52. util::fail('没有用户信息');
  53. }
  54. $this->user = $userInfo;
  55. $custom = CustomClass::getCurrentCustom($sjId, $userInfo);
  56. if (empty($custom)) {
  57. util::fail('没有找到客户');
  58. }
  59. $this->custom = $custom;
  60. $customId = $custom['id'] ?? 0;
  61. $this->customId = $customId;
  62. }
  63. //游客可以访问的方法
  64. if (in_array($action->id, $this->guestAccessAction)) {
  65. $this->validate = true;
  66. }
  67. if ($this->validate == false) {
  68. util::notLogin();
  69. }
  70. return parent::beforeAction($action);
  71. }
  72. }