BaseController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/Apache-2.0
  5. * @link https://www.gougucms.com
  6. */
  7. declare (strict_types = 1);
  8. namespace app\api;
  9. use think\App;
  10. use think\exception\HttpResponseException;
  11. use think\facade\Request;
  12. use think\Response;
  13. /**
  14. * 控制器基础类
  15. */
  16. abstract class BaseController
  17. {
  18. /**
  19. * Request实例
  20. * @var \think\Request
  21. */
  22. protected $request;
  23. /**
  24. * 应用实例
  25. * @var \think\App
  26. */
  27. protected $app;
  28. /**
  29. * 是否批量验证
  30. * @var bool
  31. */
  32. protected $batchValidate = false;
  33. /**
  34. * 控制器中间件
  35. * @var array
  36. */
  37. protected $middleware = [];
  38. /**
  39. * 分页数量
  40. * @var string
  41. */
  42. protected $pageSize = '';
  43. /**
  44. * jwt配置
  45. * @var string
  46. */
  47. protected $jwt_conf = [
  48. 'secrect' => 'gougucms',
  49. 'iss' => 'www.gougucms.com', //签发者 可选
  50. 'aud' => 'gougucms', //接收该JWT的一方,可选
  51. 'exptime' => 7200, //过期时间,这里设置2个小时
  52. ];
  53. /**
  54. * 构造方法
  55. * @access public
  56. * @param App $app 应用对象
  57. */
  58. public function __construct(App $app)
  59. {
  60. $this->app = $app;
  61. $this->request = $this->app->request;
  62. $this->jwt_conf = get_system_config('token');
  63. // 控制器初始化
  64. $this->initialize();
  65. }
  66. // 初始化
  67. protected function initialize()
  68. {
  69. //每页显示数据量
  70. $this->pageSize = Request::param('page_size', \think\facade\Config::get('app.page_size'));
  71. }
  72. /**
  73. * Api处理成功结果返回方法
  74. * @param $message
  75. * @param null $redirect
  76. * @param null $extra
  77. * @return mixed
  78. * @throws ReturnException
  79. */
  80. protected function apiSuccess($msg = 'success',$data=[])
  81. {
  82. return $this->apiReturn($data, 0, $msg);
  83. }
  84. /**
  85. * Api处理结果失败返回方法
  86. * @param $error_code
  87. * @param $message
  88. * @param null $redirect
  89. * @param null $extra
  90. * @return mixed
  91. * @throws ReturnException
  92. */
  93. protected function apiError($msg = 'fail',$data=[], $code = 1)
  94. {
  95. return $this->apiReturn($data, $code, $msg);
  96. }
  97. /**
  98. * 返回封装后的API数据到客户端
  99. * @param mixed $data 要返回的数据
  100. * @param integer $code 返回的code
  101. * @param mixed $msg 提示信息
  102. * @param string $type 返回数据格式
  103. * @param array $header 发送的Header信息
  104. * @return Response
  105. */
  106. protected function apiReturn($data, int $code = 0, $msg = '', string $type = '', array $header = []): Response
  107. {
  108. $result = [
  109. 'code' => $code,
  110. 'msg' => $msg,
  111. 'time' => time(),
  112. 'data' => $data,
  113. ];
  114. $type = $type ?: 'json';
  115. $response = Response::create($result, $type)->header($header);
  116. throw new HttpResponseException($response);
  117. }
  118. }