BaseController.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. } else { // 用户和店铺不匹配,清空店铺信息,防止后续使用(防止用户恶意使用店铺信息)
  66. $this->shop = null;
  67. $this->main = null;
  68. $this->mainId = 0;
  69. }
  70. }
  71. }
  72. }
  73. }
  74. return parent::beforeAction($action);
  75. }
  76. }