BaseController.php 880 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace mobile\controllers;
  3. use biz\user\services\UserService;
  4. use common\components\jwt;
  5. use common\components\util;
  6. /**
  7. * Created by PhpStorm.
  8. * User: shish <shish@zhhinc.com>
  9. * Date: 2019/11/18 0018
  10. * Time: 14:58
  11. */
  12. class BaseController extends PublicController
  13. {
  14. public $validate = true;
  15. public $withoutLogin = [];//不需要登陆可以访问的方法
  16. public $userId, $user;
  17. public function beforeAction($action)
  18. {
  19. //token验证
  20. $userId = jwt::checkLogin();
  21. if (empty($userId)) {
  22. $this->validate = false;
  23. } else {
  24. $this->validate = true;
  25. $this->userId = $userId;
  26. $this->user = UserService::getById($userId);
  27. }
  28. //游客可以访问的方法
  29. if (in_array($action->id, $this->withoutLogin)) {
  30. $this->validate = true;
  31. }
  32. if ($this->validate == false) {
  33. util::notLogin();
  34. }
  35. return parent::beforeAction($action);
  36. }
  37. }