BaseController.php 1.4 KB

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