No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Api.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 接口控制模块
  13. // +----------------------------------------------------------------------
  14. namespace app\common\controller;
  15. use think\App;
  16. use think\exception\HttpResponseException;
  17. use think\exception\ValidateException;
  18. use think\Response;
  19. use think\Validate;
  20. class Api
  21. {
  22. /**
  23. * @var Request Request 实例
  24. */
  25. protected $request;
  26. /**
  27. * @var bool 验证失败是否抛出异常
  28. */
  29. protected $failException = false;
  30. /**
  31. * @var bool 是否批量验证
  32. */
  33. protected $batchValidate = false;
  34. /**
  35. * 默认响应输出类型,支持json/xml
  36. * @var string
  37. */
  38. protected $responseType = 'json';
  39. /**
  40. * 构造方法
  41. * @access public
  42. * @param Request $request Request 对象
  43. */
  44. public function __construct(App $app)
  45. {
  46. $this->request = $app->request;
  47. // 控制器初始化
  48. $this->initialize();
  49. }
  50. /**
  51. * 初始化操作
  52. * @access protected
  53. */
  54. protected function initialize()
  55. {
  56. //跨域请求检测
  57. check_cors_request();
  58. //移除HTML标签
  59. $this->request->filter('trim,strip_tags,htmlspecialchars');
  60. }
  61. /**
  62. * 操作成功返回的数据
  63. * @param string $msg 提示信息
  64. * @param mixed $data 要返回的数据
  65. * @param int $code 错误码,默认为1
  66. * @param string $type 输出类型
  67. * @param array $header 发送的 Header 信息
  68. */
  69. protected function success($msg = '', $data = null, $code = 1, $type = null, array $header = [])
  70. {
  71. $this->result($msg, $data, $code, $type, $header);
  72. }
  73. /**
  74. * 操作失败返回的数据
  75. * @param string $msg 提示信息
  76. * @param mixed $data 要返回的数据
  77. * @param int $code 错误码,默认为0
  78. * @param string $type 输出类型
  79. * @param array $header 发送的 Header 信息
  80. */
  81. protected function error($msg = '', $data = null, $code = 0, $type = null, array $header = [])
  82. {
  83. $this->result($msg, $data, $code, $type, $header);
  84. }
  85. /**
  86. * 返回封装后的 API 数据到客户端
  87. * @access protected
  88. * @param mixed $msg 提示信息
  89. * @param mixed $data 要返回的数据
  90. * @param int $code 错误码,默认为0
  91. * @param string $type 输出类型,支持json/xml/jsonp
  92. * @param array $header 发送的 Header 信息
  93. * @return void
  94. * @throws HttpResponseException
  95. */
  96. protected function result($msg, $data = null, $code = 0, $type = null, array $header = [])
  97. {
  98. $result = [
  99. 'code' => $code,
  100. 'msg' => $msg,
  101. 'time' => $this->request->server('REQUEST_TIME'),
  102. 'data' => $data,
  103. ];
  104. // 如果未设置类型则自动判断
  105. $type = $type ? $type : ($this->request->param(config('var_jsonp_handler')) ? 'jsonp' : $this->responseType);
  106. if (isset($header['statuscode'])) {
  107. $code = $header['statuscode'];
  108. unset($header['statuscode']);
  109. } else {
  110. //未设置状态码,根据code值判断
  111. $code = $code >= 1000 || $code < 200 ? 200 : $code;
  112. }
  113. $response = Response::create($result, $type, $code)->header($header);
  114. throw new HttpResponseException($response);
  115. }
  116. /**
  117. * 设置验证失败后是否抛出异常
  118. * @access protected
  119. * @param bool $fail 是否抛出异常
  120. * @return $this
  121. */
  122. protected function validateFailException($fail = true)
  123. {
  124. $this->failException = $fail;
  125. return $this;
  126. }
  127. /**
  128. * 验证数据
  129. * @access protected
  130. * @param array $data 数据
  131. * @param string|array $validate 验证器名或者验证规则数组
  132. * @param array $message 提示信息
  133. * @param bool $batch 是否批量验证
  134. * @param mixed $callback 回调方法(闭包)
  135. * @return array|string|true
  136. * @throws ValidateException
  137. */
  138. protected function validate($data, $validate, $message = [], $batch = false, $callback = null)
  139. {
  140. if (is_array($validate)) {
  141. $v = new Validate();
  142. $v->rule($validate);
  143. } else {
  144. if (strpos($validate, '.')) {
  145. // 支持场景
  146. list($validate, $scene) = explode('.', $validate);
  147. }
  148. $v = new Validate($validate);
  149. if (!empty($scene)) {
  150. $v->scene($scene);
  151. }
  152. }
  153. // 是否批量验证
  154. if ($batch || $this->batchValidate) {
  155. $v->batch(true);
  156. }
  157. if (is_array($message)) {
  158. $v->message($message);
  159. }
  160. if ($callback && is_callable($callback)) {
  161. call_user_func_array($callback, [$v, &$data]);
  162. }
  163. if (!$v->check($data)) {
  164. if ($this->failException) {
  165. throw new ValidateException($v->getError());
  166. }
  167. return $v->getError();
  168. }
  169. return true;
  170. }
  171. //刷新Token
  172. protected function token()
  173. {
  174. $token = $this->request->param('__token__');
  175. //验证Token
  176. if (!Validate::make()->check(['__token__' => $token], ['__token__' => 'require|token'])) {
  177. $this->error('令牌错误!', '', ['__token__' => $this->request->token()]);
  178. }
  179. //刷新Token
  180. $this->request->token();
  181. }
  182. }