BaseController.php 2.3 KB

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