BaseController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace hd\controllers;
  3. use biz\sj\classes\SjClass;
  4. use bizHd\admin\classes\AdminClass;
  5. use biz\shop\classes\ShopAdminClass;
  6. use biz\sj\classes\MerchantExtendClass;
  7. use bizHd\merchant\services\ShopService;
  8. use bizHd\shop\classes\MainClass;
  9. use common\components\dict;
  10. use common\components\jwt;
  11. use Yii;
  12. use common\components\util;
  13. class BaseController extends PublicController
  14. {
  15. public $admin, $adminId, $adminAvatar, $account, $customId = 0, $custom;
  16. public $sjId = 0, $sj = [], $sjExtend = [], $signPackage, $shopId = 0, $mainId = 0, $main, $shop, $shopAdminId = 0, $shopAdmin;
  17. public $appId;
  18. public $isWx = false;
  19. public $isLogin = false;
  20. //游客可以访问的方法
  21. public $guestAccess = [];
  22. //ssh 2020.3.8
  23. public function beforeAction($action)
  24. {
  25. //定义平台类型 1花店 2供货商 3商城
  26. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'hd');
  27. //token验证
  28. $adminId = jwt::getLoginId();
  29. if (getenv('YII_ENV', 'local') != 'production') {
  30. //$adminId = 253;
  31. }
  32. if (empty($adminId) && in_array($action->id, $this->guestAccess) == false) {
  33. util::notLogin();
  34. }
  35. $admin = AdminClass::getById($adminId, true);
  36. if (empty($admin) && in_array($action->id, $this->guestAccess) == false) {
  37. util::notLogin();
  38. }
  39. $adminId = $admin->id ?? 0;
  40. $this->admin = $admin;
  41. $this->adminId = $adminId;
  42. //后台
  43. $currentShopId = $admin->currentShopId ?? 0;
  44. if (empty($currentShopId)) {
  45. if (in_array($action->id, $this->guestAccess) == false) {
  46. util::fail('请注册登录');
  47. }
  48. } else {
  49. $this->shopId = $currentShopId;
  50. $shop = ShopService::getById($currentShopId, true);
  51. if (empty($shop)) {
  52. util::fail('没有找到店铺');
  53. }
  54. $this->shop = $shop;
  55. $mainId = $shop->mainId ?? 0;
  56. $this->mainId = $mainId;
  57. $main = MainClass::getById($mainId, true);
  58. if (empty($main)) {
  59. util::fail('没有找到main信息');
  60. }
  61. $this->main = $main;
  62. $shopAdmin = ShopAdminClass::getByCondition(['mainId' => $mainId, 'adminId' => $adminId, 'delStatus' => 0], true);
  63. if (empty($shopAdmin)) {
  64. util::logout('没有找到员工信息');
  65. }
  66. if (isset($shopAdmin->delStatus) == false || $shopAdmin->delStatus == 1) {
  67. util::fail('您不是本店员工哦');
  68. }
  69. if (isset($shopAdmin->status) == false || $shopAdmin->status == 0) {
  70. util::fail('您已被平台禁止登录');
  71. }
  72. $this->shopAdmin = $shopAdmin;
  73. $this->shopAdminId = $shopAdmin->id ?? 0;
  74. $sjId = $shop->sjId;
  75. $this->sjId = $sjId;
  76. $sj = SjClass::getById($sjId, true);
  77. $this->sj = $sj;
  78. $sjExtend = MerchantExtendClass::getBySjId($sjId, true);
  79. $this->sjExtend = $sjExtend;
  80. }
  81. $this->formParams();
  82. return parent::beforeAction($action);
  83. }
  84. /**
  85. * 把必要参数放 $_POST 或 $_GET 中
  86. */
  87. private function formParams()
  88. {
  89. // 将参数放入 $_POST 中(如果是 POST 请求)
  90. if (Yii::$app->request->isPost) {
  91. $_POST['adminId'] = intval($this->adminId);
  92. $_POST['mainId'] = intval($this->mainId);
  93. $_POST['shopId'] = intval($this->shopId);
  94. }
  95. // 将参数放入 $_GET 中(如果是 GET 请求)
  96. if (Yii::$app->request->isGet) {
  97. $_GET['adminId'] = intval($this->adminId);
  98. $_GET['mainId'] = intval($this->mainId);
  99. $_GET['shopId'] = intval($this->shopId);
  100. }
  101. }
  102. }