BaseController.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace shop\controllers;
  3. use biz\admin\services\AdminRelateService;
  4. use biz\merchant\services\AdminService;
  5. use biz\merchant\services\MerchantExtendService;
  6. use biz\merchant\services\MerchantService;
  7. use common\components\jwt;
  8. use Yii;
  9. use common\components\util;
  10. class BaseController extends PublicController
  11. {
  12. public $admin, $adminId, $adminAvatar, $account;
  13. public $merchantId = 0, $merchantName, $merchant, $merchantExtend, $merchantLogo, $signPackage;
  14. public $openMerchant, $openMerchantId;
  15. public $appId;
  16. public $isWx = false;
  17. public $isLogin = false;
  18. public $withoutLogin = [];//不需要登陆的方法名
  19. public $validate = true;
  20. //shish 2020.3.8
  21. public function beforeAction($action)
  22. {
  23. //token验证
  24. $adminId = jwt::getLoginId();
  25. $merchantId = 0;
  26. $merchant = [];
  27. $merchantExtend = [];
  28. $admin = [];
  29. if (empty($adminId)) {
  30. $this->validate = false;
  31. } else {
  32. $admin = AdminService::getById($adminId);
  33. if (empty($admin)) {
  34. util::fail('帐号无效');
  35. }
  36. $adminId = $admin['id'];
  37. $relateId = Yii::$app->redis->executeCommand('GET', ['ADMIN_RELATION_' . $adminId]);
  38. //缓存取得的不是数字可能被清缓存,需要重新登陆,$relateId = 0 则表示没有关联的商家
  39. if (is_numeric($relateId) == false) {
  40. util::notLogin('请重新进行登陆');
  41. }
  42. if (!empty($relateId)) {
  43. $relate = AdminRelateService::getById($relateId);
  44. $merchantId = $relate['merchantId'];
  45. $merchant = MerchantService::getMerchantById($merchantId);
  46. $merchantExtend = MerchantExtendService::getByMerchantId($merchantId);
  47. }
  48. }
  49. //游客可以访问的方法
  50. if (in_array($action->id, $this->withoutLogin)) {
  51. $this->validate = true;
  52. }
  53. if ($this->validate == false) {
  54. util::notLogin('您还没有登陆哦');
  55. }
  56. $this->admin = $admin;
  57. $this->adminId = $adminId;
  58. $this->merchantId = $merchantId;
  59. $this->merchant = $merchant;
  60. $this->merchantExtend = $merchantExtend;
  61. return parent::beforeAction($action);
  62. }
  63. }