BaseController.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\api;
  4. use app\api\exception\ApiException;
  5. use app\common\until\Token;
  6. use think\App;
  7. use think\exception\ValidateException;
  8. use think\facade\Session;
  9. use think\Validate;
  10. /**
  11. * 控制器基础类
  12. */
  13. abstract class BaseController {
  14. /**
  15. * Request实例
  16. * @var \think\Request
  17. */
  18. protected $request;
  19. /**
  20. * 应用实例
  21. * @var \think\App
  22. */
  23. protected $app;
  24. /**
  25. * 是否批量验证
  26. * @var bool
  27. */
  28. protected $batchValidate = false;
  29. /**
  30. * 控制器中间件
  31. * @var array
  32. */
  33. protected $middleware = [];
  34. public $userId = 0;
  35. public $isAdmin = 0;
  36. /**
  37. * 构造方法
  38. * @access public
  39. * @param App $app 应用对象
  40. */
  41. public function __construct(App $app) {
  42. $this->app = $app;
  43. $this->request = $this->app->request;
  44. Session::start();
  45. //$this->request['Accept'] = 'application/json';
  46. $route = [
  47. '/api/token/getToken',
  48. '/api/index',
  49. '/api/user/login',
  50. '/api/admin/login',
  51. '/api/admin/verifyimg',
  52. ];
  53. if (!in_array(strtolower($this->request->baseUrl()), $route)) {
  54. if (empty($this->request->header('token'))) {
  55. throw new ApiException('token不为空');
  56. }
  57. $token = new Token();
  58. $decodeToken = $token->decodeToken();
  59. $this->userId = $decodeToken['userId'];
  60. $this->isAdmin = $decodeToken['isAdmin'];
  61. }
  62. // 控制器初始化
  63. $this->initialize();
  64. }
  65. // 初始化
  66. protected function initialize() {
  67. }
  68. protected function isAdmin() {
  69. if ($this->isAdmin == 1) {
  70. return true;
  71. }
  72. return false;
  73. }
  74. }