BaseController.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace mall\controllers;
  3. use biz\shop\classes\ShopClass;
  4. use bizHd\custom\classes\CustomClass;
  5. use bizMall\hd\classes\HdClass;
  6. use bizMall\shop\classes\MainClass;
  7. use bizMall\user\classes\UserClass;
  8. use common\components\dict;
  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, $hd, $hdId, $mainId = 0, $main;
  18. public $isWx;
  19. public function beforeAction($action)
  20. {
  21. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'mall');
  22. $this->isWx = util::isWx();
  23. $userId = jwt::getLoginId();
  24. if (empty($userId)) {
  25. if (!in_array($action->id, $this->guestAccess)) {
  26. util::fail('请先登录哈');
  27. }
  28. }
  29. $this->userId = $userId;
  30. $user = UserClass::getById($userId, true);
  31. if (empty($user)) {
  32. if (!in_array($action->id, $this->guestAccess)) {
  33. util::fail('请先登录呢');
  34. }
  35. }
  36. $this->user = $user;
  37. $shopId = Yii::$app->request->get('account'); // 从 $_GET 中取
  38. $shopId = empty($shopId) ? Yii::$app->request->post('account') : $shopId; // 从 $_POST 中取
  39. $shopId = intval($shopId);
  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. if (empty($hdId)) {
  51. //兼容早期代码,勿删,扫码付款页也用到,只写POST方式
  52. $hdId = Yii::$app->request->post('hdId', 0);
  53. }
  54. if (!empty($hdId)) {
  55. $hd = HdClass::getById($hdId, true);
  56. if (!empty($hd)) {
  57. if (!empty($this->userId) && $hd->userId == $this->userId && $shopId == $hd->shopId) {
  58. $this->hd = $hd;
  59. $this->hdId = $hdId;
  60. $customId = $hd->customId ?? 0;
  61. $custom = CustomClass::getById($customId, true);
  62. $this->custom = $custom;
  63. $this->customId = $customId;
  64. }
  65. }
  66. }
  67. }
  68. }
  69. return parent::beforeAction($action);
  70. }
  71. }