BaseController.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace shop\controllers;
  3. use biz\admin\classes\AdminClass;
  4. use biz\admin\services\ShopAdminService;
  5. use biz\custom\classes\CustomClass;
  6. use biz\merchant\classes\MerchantExtendClass;
  7. use biz\merchant\classes\ShopClass;
  8. use biz\merchant\services\AdminService;
  9. use biz\merchant\services\MerchantExtendService;
  10. use biz\merchant\services\MerchantService;
  11. use biz\merchant\services\ShopService;
  12. use biz\wx\services\WxOpenService;
  13. use common\components\httpUtil;
  14. use common\components\jwt;
  15. use Yii;
  16. use common\components\util;
  17. class BaseController extends PublicController
  18. {
  19. public $admin, $adminId, $adminAvatar, $account, $customId, $custom;
  20. public $sjId = 0, $sj = [], $sjExtend = [], $signPackage, $shopId = 0;
  21. public $appId;
  22. public $isWx = false;
  23. public $isLogin = false;
  24. //不需要登陆的方法名
  25. public $withoutLoginAction = [];
  26. //需求带account请求头才能访问的方法
  27. public $needShopIdAction = [];
  28. public $validate = true;
  29. //shish 2020.3.8
  30. public function beforeAction($action)
  31. {
  32. //token验证
  33. //$adminId = jwt::getLoginId();
  34. $adminId = 18569;
  35. $admin = AdminClass::getAdminById($adminId, true);
  36. if (empty($admin)) {
  37. util::fail('帐号无效');
  38. }
  39. $adminId = $admin['id'];
  40. $header = httpUtil::getHeader();
  41. $shopId = $header['shop-id'] ?? 0;
  42. if (!empty($shopId)) {
  43. $shop = ShopClass::getById($shopId);
  44. if (empty($shop)) {
  45. util::fail('没有找到店铺');
  46. }
  47. $sjId = $shop['merchantId'] ?? 0;
  48. $custom = CustomClass::getCustom(['sjId' => $sjId, 'adminId' => $adminId]);
  49. $customId = $custom['id'] ?? 0;
  50. if (empty($customId)) {
  51. util::fail('没有找到客户信息');
  52. }
  53. $this->custom = $custom;
  54. $this->customId = $customId;
  55. $this->sjId = $sjId;
  56. $sj = MerchantService::getMerchantById($sjId);
  57. $this->sj = $sj;
  58. $this->sjExtend = MerchantExtendClass::getByMerchantId($sjId);
  59. } else {
  60. if (in_array($action->id, $this->needShopIdAction)) {
  61. util::fail('您正在访问店铺,但没有找到店铺id');
  62. }
  63. $shopId = $admin['currentShopId'] ?? 0;
  64. if (!empty($shopId)) {
  65. $shop = ShopService::getById($shopId);
  66. $sjId = $shop['merchantId'];
  67. $sj = MerchantService::getMerchantById($sjId);
  68. $this->sj = $sj;
  69. $this->shopId = $shopId;
  70. $this->sjId = $sjId;
  71. $this->sjExtend = MerchantExtendClass::getByMerchantId($sjId);
  72. }
  73. }
  74. $this->admin = $admin;
  75. $this->adminId = $adminId;
  76. //游客可以访问的方法
  77. if (in_array($action->id, $this->withoutLoginAction)) {
  78. $this->validate = true;
  79. }
  80. if ($this->validate == false) {
  81. util::notLogin();
  82. }
  83. return parent::beforeAction($action);
  84. }
  85. }