BaseController.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 app\common\until\Until;
  7. use think\App;
  8. use think\exception\ValidateException;
  9. use think\facade\Session;
  10. use think\Validate;
  11. /**
  12. * 控制器基础类
  13. */
  14. abstract class BaseController {
  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. public $adminId = 0;
  38. /**
  39. * 构造方法
  40. * @access public
  41. * @param App $app 应用对象
  42. */
  43. public function __construct(App $app) {
  44. $this->app = $app;
  45. $this->request = $this->app->request;
  46. Session::start();
  47. //$this->request['Accept'] = 'application/json';
  48. Until::$startTime = time();
  49. $route = [
  50. '/api/token/getToken',
  51. '/api/index',
  52. '/api/user/login',
  53. '/api/admin/login',
  54. '/api/admin/verifyimg',
  55. '/api/order/notifyorder'
  56. ];
  57. if (!in_array(strtolower($this->request->baseUrl()), $route)) {
  58. if (empty($this->request->header('token'))) {
  59. throw new ApiException('token不为空');
  60. }
  61. $token = new Token();
  62. $decodeToken = $token->decodeToken();
  63. $this->userId = $decodeToken['userId'];
  64. Until::$userId = $this->userId;
  65. $this->isAdmin = $decodeToken['isAdmin'];
  66. $this->adminId = $decodeToken['adminId'];
  67. }
  68. // 控制器初始化
  69. $this->initialize();
  70. }
  71. // 初始化
  72. protected function initialize() {
  73. }
  74. protected function isAdmin() {
  75. if ($this->isAdmin == 1) {
  76. Until::$isAdmin = true;
  77. return true;
  78. }
  79. Until::$isAdmin = false;
  80. return false;
  81. }
  82. }