BaseController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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\hd\classes\HdClass;
  7. use bizMall\merchant\services\MerchantExtendService;
  8. use bizMall\merchant\services\MerchantService;
  9. use bizMall\shop\classes\MainClass;
  10. use bizMall\user\classes\UserClass;
  11. use bizMall\user\services\UserService;
  12. use common\components\dict;
  13. use common\components\httpUtil;
  14. use common\components\jwt;
  15. use common\components\util;
  16. use function GuzzleHttp\Promise\iter_for;
  17. use Yii;
  18. class BaseController extends PublicController
  19. {
  20. public $validate = true;
  21. public $guestAccess = [];
  22. public $userId = 0, $user, $shopId, $shop;
  23. public $sjId, $sj, $sjExtend, $customId = 0, $custom, $hd, $mainId = 0, $main;
  24. public $isWx;
  25. public function beforeAction($action)
  26. {
  27. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'mall');
  28. $this->isWx = util::isWx();
  29. $userId = jwt::getLoginId();
  30. if (!empty($userId)) {
  31. $this->userId = $userId;
  32. $user = UserClass::getById($userId, true);
  33. if (empty($user)) {
  34. if (!in_array($action->id, $this->guestAccess)){
  35. util::fail('请先登录');
  36. }
  37. }
  38. $this->user = $user;
  39. $shopId = Yii::$app->request->get('account', 0);
  40. if (!empty($shopId)) {
  41. $this->shopId = $shopId;
  42. $shop = ShopClass::getById($shopId, true);
  43. if (!empty($shop)) {
  44. $this->shop = $shop;
  45. $mainId = $shop->mainId ?? 0;
  46. $main = MainClass::getById($mainId, true);
  47. $this->main = $main;
  48. $this->mainId = $mainId;
  49. $hdId = Yii::$app->request->get('hdId', 0);
  50. $hd = HdClass::getById($hdId, true);
  51. if (!empty($hd)) {
  52. if ($hd->userId != $this->userId) {
  53. util::fail('不是你的花店,无法访问哦');
  54. }
  55. if ($shopId != $hd->shopId) {
  56. util::fail('花店信息有问题哦');
  57. }
  58. $this->hd = $hd;
  59. $customId = $hd->customId ?? 0;
  60. $custom = CustomClass::getById($customId, true);
  61. $this->custom = $custom;
  62. } else {
  63. $respond = CustomClass::buildRelation($shop, $user);
  64. $hd = $respond['hd'];
  65. $custom = $respond['custom'];
  66. $this->hd = $hd;
  67. $this->custom = $custom;
  68. }
  69. }
  70. }
  71. }
  72. return parent::beforeAction($action);
  73. }
  74. }