BaseController.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace client\controllers;
  3. use Yii;
  4. use biz\merchant\services\MerchantExtendService;
  5. use biz\merchant\services\MerchantService;
  6. use biz\user\services\UserService;
  7. use common\components\httpUtil;
  8. use common\components\jwt;
  9. use common\components\util;
  10. /**
  11. * Created by PhpStorm.
  12. * User: shish <shish@zhhinc.com>
  13. * Date: 2019/11/18 0018
  14. * Time: 14:58
  15. */
  16. class BaseController extends PublicController
  17. {
  18. public $validate = true;
  19. public $withoutLogin = [];//不需要登陆可以访问的方法
  20. public $userId, $user, $shopId;
  21. public $merchantId, $merchant, $merchantExtend;
  22. public $isWx;
  23. public function beforeAction($action)
  24. {
  25. //token验证
  26. $userId = jwt::getLoginId();
  27. $header = httpUtil::getHeader();
  28. $account = isset($header['account']) ? $header['account'] : 0;
  29. $shopId = isset($header['shopId']) ? $header['shopId'] : 0;
  30. if (empty($account)) {
  31. util::fail('商家无效');
  32. }
  33. $merchant = MerchantService::getMerchantById($account);
  34. if (empty($merchant)) {
  35. util::fail('商家信息无效');
  36. }
  37. $this->merchantId = $account;
  38. $this->merchant = $merchant;
  39. $this->shopId = $shopId;
  40. $this->merchantExtend = MerchantExtendService::getByMerchantId($account);
  41. $this->isWx = util::isWx();
  42. if (empty($userId)) {
  43. $this->validate = false;
  44. } else {
  45. $this->validate = true;
  46. $this->userId = $userId;
  47. $this->user = UserService::getUserInfo($userId);
  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. return parent::beforeAction($action);
  57. }
  58. }