BaseController.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace shop\controllers;
  3. use biz\admin\services\AdminShopService;
  4. use biz\merchant\services\AdminService;
  5. use biz\merchant\services\MerchantExtendService;
  6. use biz\merchant\services\MerchantService;
  7. use biz\merchant\services\ShopService;
  8. use biz\wx\services\WxOpenService;
  9. use common\components\jwt;
  10. use Yii;
  11. use common\components\util;
  12. class BaseController extends PublicController
  13. {
  14. public $admin, $adminId, $adminAvatar, $account;
  15. public $merchantId = 0, $merchantName, $merchant, $merchantExtend, $merchantLogo, $signPackage, $shopId = 0;
  16. public $openMerchant;
  17. public $appId;
  18. public $isWx = false;
  19. public $isLogin = false;
  20. public $withoutLogin = [];//不需要登陆的方法名
  21. public $validate = true;
  22. //shish 2020.3.8
  23. public function beforeAction($action)
  24. {
  25. //token验证
  26. $adminId = jwt::getLoginId();
  27. $merchantId = 0;
  28. $merchant = [];
  29. $merchantExtend = [];
  30. $admin = [];
  31. if (empty($adminId)) {
  32. $this->validate = false;
  33. } else {
  34. $admin = AdminService::getById($adminId);
  35. if (empty($admin)) {
  36. util::fail('帐号无效');
  37. }
  38. $adminId = $admin['id'];
  39. $shopId = Yii::$app->redis->executeCommand('GET', ['LOGIN_ADMIN_SHOP' . $adminId]);
  40. //取不到门店id可能被清缓存,需要重新登陆,$shopId = 0 则表示非管理员,没有关联的门店,只可以访问官网
  41. if (is_numeric($shopId) == false) {
  42. util::notLogin('登陆已失效,请重新登陆');
  43. }
  44. if (!empty($shopId)) {
  45. $shop = ShopService::getById($shopId);
  46. if (empty($shop)) {
  47. util::fail('您所管理的门店已经跑到月球上去了');
  48. }
  49. $merchantId = $shop['merchantId'];
  50. $merchant = MerchantService::getMerchantById($merchantId);
  51. $merchantExtend = MerchantExtendService::getByMerchantId($merchantId);
  52. }
  53. $this->shopId = $shopId;
  54. }
  55. //游客可以访问的方法
  56. if (in_array($action->id, $this->withoutLogin)) {
  57. $this->validate = true;
  58. }
  59. if ($this->validate == false) {
  60. util::notLogin('您还没有登陆哦');
  61. }
  62. $this->admin = $admin;
  63. $this->adminId = $adminId;
  64. $this->merchantId = $merchantId;
  65. $this->merchant = $merchant;
  66. $this->merchantExtend = $merchantExtend;
  67. $this->openMerchant = WxOpenService::getWxInfo();
  68. return parent::beforeAction($action);
  69. }
  70. }