BaseController.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. $userId = intval($userId);
  30. $this->userId = $userId;
  31. $user = UserClass::getById($userId, true);
  32. if (empty($user)) {
  33. if (!in_array($action->id, $this->guestAccess)) {
  34. util::fail('请先登录呢');
  35. }
  36. }
  37. $this->user = $user;
  38. $shopId = Yii::$app->request->get('account'); // 从 $_GET 中取
  39. $shopId = empty($shopId) ? Yii::$app->request->post('account') : $shopId; // 从 $_POST 中取
  40. $shopId = intval($shopId);
  41. if (!empty($shopId)) {
  42. $this->shopId = $shopId;
  43. $shop = ShopClass::getById($shopId, true);
  44. if (!empty($shop)) {
  45. $this->shop = $shop;
  46. $mainId = $shop->mainId ?? 0;
  47. $main = MainClass::getById($mainId, true);
  48. $this->main = $main;
  49. $this->mainId = $mainId;
  50. $hdId = Yii::$app->request->get('hdId', 0);
  51. if (empty($hdId)) {
  52. //兼容早期代码,勿删,扫码付款页也用到,只写POST方式
  53. $hdId = Yii::$app->request->post('hdId', 0);
  54. }
  55. if (!empty($hdId)) {
  56. $hd = HdClass::getById($hdId, true);
  57. if (!empty($hd)) {
  58. if (!empty($this->userId) && $hd->userId == $this->userId && $shopId == $hd->shopId) {
  59. $this->hd = $hd;
  60. $this->hdId = $hdId;
  61. $customId = $hd->customId ?? 0;
  62. $custom = CustomClass::getById($customId, true);
  63. $this->custom = $custom;
  64. $this->customId = $customId;
  65. }
  66. }
  67. }
  68. }
  69. }
  70. return parent::beforeAction($action);
  71. }
  72. }