BaseController.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace www\controllers;
  3. use biz\merchant\services\MerchantService;
  4. use biz\saas\services\StaffService;
  5. use biz\user\services\UserService;
  6. use biz\weixin\services\WxOpenService;
  7. use common\components\jwt;
  8. use common\components\util;
  9. use Yii;
  10. use yii\web\Controller;
  11. /**
  12. * 平台次级基类,次于PublicController
  13. */
  14. class BaseController extends PublicController
  15. {
  16. public $staff, $staffId, $userId, $user, $merchantId, $merchant;
  17. public $isLogin = false;
  18. public $withoutLogin = [];//不需要登陆的方法名
  19. public $validate = true;
  20. public $wxOpenId = 1;
  21. public $wxOpen;
  22. public function beforeAction($action)
  23. {
  24. //token验证
  25. $staffId = jwt::getLoginId();
  26. $userId = $staffId;
  27. if (empty($staffId)) {
  28. $this->validate = false;
  29. }
  30. //游客可以访问的方法
  31. if (in_array($action->id, $this->withoutLogin)) {
  32. $this->validate = true;
  33. }
  34. if ($this->validate == false) {
  35. util::notLogin();
  36. }
  37. if (!empty($userId)) {
  38. $staff = UserService::getUserInfo($userId);
  39. $this->staff = $staff;
  40. $this->user = $staff;
  41. }
  42. $this->staffId = $staffId;
  43. $this->userId = $userId;
  44. $open = WxOpenService::getById($this->wxOpenId);
  45. $this->wxOpen = $open;
  46. $merchantId = isset($open['merchantId']) ? $open['merchantId'] : 0;
  47. $this->merchantId = $merchantId;
  48. if (!empty($merchantId)) {
  49. $this->merchant = MerchantService::getById($merchantId);
  50. }
  51. return parent::beforeAction($action);
  52. }
  53. }