BaseController.php 2.7 KB

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