BaseController.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace mall\controllers;
  3. use biz\shop\classes\ShopClass;
  4. use biz\sj\classes\SjClass;
  5. use bizHd\custom\classes\CustomClass;
  6. use bizMall\merchant\services\MerchantExtendService;
  7. use bizMall\merchant\services\MerchantService;
  8. use bizMall\user\classes\UserClass;
  9. use bizMall\user\services\UserService;
  10. use common\components\dict;
  11. use common\components\httpUtil;
  12. use common\components\jwt;
  13. use common\components\util;
  14. use function GuzzleHttp\Promise\iter_for;
  15. use Yii;
  16. class BaseController extends PublicController
  17. {
  18. public $validate = true;
  19. public $guestAccess = [];
  20. public $userId = 0, $user, $shopId, $shop;
  21. public $sjId, $sj, $sjExtend, $customId = 0, $custom = [];
  22. public $isWx;
  23. public function beforeAction($action)
  24. {
  25. //定义平台类型 1花店 2供货商 3商城
  26. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'mall');
  27. $userId = jwt::getLoginId();
  28. if (empty($userId)) {
  29. util::fail('您还没有登陆');
  30. }
  31. $this->userId = $userId;
  32. $user = UserClass::getById($userId, true);
  33. if (empty($user)) {
  34. util::fail('没有找到客户信息');
  35. }
  36. $this->user = $user;
  37. $this->isWx = util::isWx();
  38. $header = httpUtil::getHeader();
  39. $shopId = isset($header['account']) ? $header['account'] : 0;
  40. $this->shopId = $shopId;
  41. if (!empty($shopId)) {
  42. $shop = ShopClass::getById($shopId, true);
  43. if (empty($shop)) {
  44. util::fail('没有找到门店');
  45. }
  46. $sjId = $shop->sjId ?? 0;
  47. if (empty($sjId)) {
  48. util::fail('没有找到商家哦');
  49. }
  50. $sj = SjClass::getById($sjId, true);
  51. if (empty($sj)) {
  52. util::fail('没有找到商家信息');
  53. }
  54. $this->sjId = $sjId;
  55. $this->sj = $sj;
  56. $this->shop = $shop;
  57. $this->sjExtend = MerchantExtendService::getBySjId($sjId);
  58. $userInfo = isset($user->attributes) ? $user->attributes : [];
  59. $custom = CustomClass::replaceCustom($sjId, $userInfo, $shopId);
  60. if (!empty($custom)) {
  61. $this->custom = $custom;
  62. $customId = $custom['id'] ?? 0;
  63. $this->customId = $customId;
  64. } else {
  65. if (in_array($action->id, $this->guestAccess) == false) {
  66. util::fail('没有找到客户信息哦');
  67. }
  68. }
  69. }
  70. return parent::beforeAction($action);
  71. }
  72. }