BaseController.php 1.5 KB

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