BaseController.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. /**
  16. * Request实例
  17. * @var \think\Request
  18. */
  19. protected $request;
  20. /**
  21. * 应用实例
  22. * @var \think\App
  23. */
  24. protected $app;
  25. /**
  26. * 是否批量验证
  27. * @var bool
  28. */
  29. protected $batchValidate = false;
  30. /**
  31. * 控制器中间件
  32. * @var array
  33. */
  34. protected $middleware = [];
  35. public $userId = 0;
  36. public $isAdmin = 0;
  37. /**
  38. * 构造方法
  39. * @access public
  40. * @param App $app 应用对象
  41. */
  42. public function __construct(App $app)
  43. {
  44. $this->app = $app;
  45. $this->request = $this->app->request;
  46. Session::start();
  47. // $this->request['Accept'] = 'application/json';
  48. $route = [
  49. '/api/Token/getToken',
  50. '/api/index',
  51. '/api/user/login',
  52. '/api/admin/login',
  53. ];
  54. if (!in_array($this->request->baseUrl(),$route)){
  55. if (empty($this->request->header('token'))) {
  56. throw new ApiException('token不为空');
  57. }else{
  58. $token = new Token();
  59. $decodeToken = $token->decodeToken();
  60. $this->userId = $decodeToken['userId'];
  61. $this->isAdmin = $decodeToken['isAdmin'];
  62. }
  63. // $token = new Token();
  64. // $decodeToken = $token->decodeToken();
  65. // $this->userId = $decodeToken['userId'];
  66. // $this->isAdmin = $decodeToken['isAdmin'];
  67. }
  68. // 控制器初始化
  69. $this->initialize();
  70. }
  71. // 初始化
  72. protected function initialize()
  73. {}
  74. protected function isAdmin() {
  75. if ($this->isAdmin == 1){
  76. return true;
  77. }
  78. return false;
  79. }
  80. }