BaseController.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 $guestAccess = [];
  16. public $userId = 0, $user, $shopId,$shop;
  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. $sjId = 0;
  25. if (in_array($action->id, $this->guestAccess) == false){
  26. $header = httpUtil::getHeader();
  27. $shopId = isset($header['account']) ? $header['account'] : 0;
  28. if (empty($shopId)) {
  29. util::fail('没有找到门店');
  30. }
  31. $shop = ShopClass::getById($shopId);
  32. if (empty($shop)) {
  33. util::fail('没有找到门店哦');
  34. }
  35. $sjId = $shop['merchantId'] ?? 0;
  36. if (empty($sjId)) {
  37. util::fail('没有找到商家哦');
  38. }
  39. $sj = MerchantService::getMerchantById($sjId);
  40. if (empty($sj)) {
  41. util::fail('没有找到商家');
  42. }
  43. $this->sjId = $sjId;
  44. $this->sj = $sj;
  45. $this->shopId = $shopId;
  46. $this->shop = $shop;
  47. $this->sjExtend = MerchantExtendService::getByMerchantId($sjId);
  48. }
  49. $this->isWx = util::isWx();
  50. if (empty($userId)) {
  51. $this->validate = false;
  52. } else {
  53. $this->validate = true;
  54. $this->userId = $userId;
  55. $userInfo = UserService::getUserInfo($userId);
  56. if (empty($userInfo)) {
  57. util::fail('没有用户信息');
  58. }
  59. $this->user = $userInfo;
  60. if (in_array($action->id, $this->guestAccess) == false) {
  61. $custom = CustomClass::replaceCustom($sjId, $userInfo);
  62. $this->custom = $custom;
  63. $customId = $custom['id'] ?? 0;
  64. $this->customId = $customId;
  65. }
  66. }
  67. //游客可以访问的方法
  68. if (in_array($action->id, $this->guestAccess)) {
  69. $this->validate = true;
  70. }
  71. if ($this->validate == false) {
  72. util::notLogin();
  73. }
  74. return parent::beforeAction($action);
  75. }
  76. }