BaseController.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. $account = 12362;
  31. $userId = 12543743;
  32. if (empty($account)) {
  33. util::fail('商家无效');
  34. }
  35. $merchant = MerchantService::getMerchantById($account);
  36. if (empty($merchant)) {
  37. util::fail('商家信息无效');
  38. }
  39. $this->merchantId = $account;
  40. $this->merchant = $merchant;
  41. $this->shopId = $shopId;
  42. $this->merchantExtend = MerchantExtendService::getByMerchantId($account);
  43. $this->isWx = util::isWx();
  44. if (empty($userId)) {
  45. $this->validate = false;
  46. } else {
  47. $this->validate = true;
  48. $this->userId = $userId;
  49. $this->user = UserService::getUserInfo($userId);
  50. }
  51. //游客可以访问的方法
  52. if (in_array($action->id, $this->withoutLogin)) {
  53. $this->validate = true;
  54. }
  55. if ($this->validate == false) {
  56. util::notLogin();
  57. }
  58. return parent::beforeAction($action);
  59. }
  60. }