BaseController.php 2.4 KB

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