Check.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2021/1/13
  6. * Time: 15:56
  7. */
  8. namespace addons\ddrive\library;
  9. use think\exception\HttpResponseException;
  10. use think\Request;
  11. use think\Response;
  12. use think\Loader;
  13. use think\exception\ValidateException;
  14. class Check{
  15. /**
  16. * @var Request Request 实例
  17. */
  18. protected $request;
  19. /**
  20. * 默认响应输出类型,支持json/xml
  21. * @var string
  22. */
  23. protected $responseType = 'json';
  24. /**
  25. * @var bool 验证失败是否抛出异常
  26. */
  27. protected $failException = false;
  28. /**
  29. * @var bool 是否批量验证
  30. */
  31. protected $batchValidate = false;
  32. /**
  33. * 构造方法
  34. * @access public
  35. * @param Request $request Request 对象
  36. */
  37. public function __construct(Request $request = null)
  38. {
  39. $this->request = is_null($request) ? Request::instance() : $request;
  40. }
  41. /**
  42. * 检验参数
  43. * @param $rule
  44. * @return mixed
  45. */
  46. public function checkParam($rule)
  47. {
  48. $data = $this->request->param();
  49. $res = $this->validate($data, $rule);
  50. if ($res !== true) {
  51. $this->error($res, []);
  52. }
  53. return $data;
  54. }
  55. /**
  56. * 验证数据
  57. * @access protected
  58. * @param array $data 数据
  59. * @param string|array $validate 验证器名或者验证规则数组
  60. * @param array $message 提示信息
  61. * @param bool $batch 是否批量验证
  62. * @param mixed $callback 回调方法(闭包)
  63. * @return array|string|true
  64. * @throws ValidateException
  65. */
  66. protected function validate($data, $validate, $message = [], $batch = false, $callback = null)
  67. {
  68. if (is_array($validate)) {
  69. $v = Loader::validate();
  70. $v->rule($validate);
  71. } else {
  72. // 支持场景
  73. if (strpos($validate, '.')) {
  74. list($validate, $scene) = explode('.', $validate);
  75. }
  76. $v = Loader::validate($validate);
  77. !empty($scene) && $v->scene($scene);
  78. }
  79. // 批量验证
  80. if ($batch || $this->batchValidate) {
  81. $v->batch(true);
  82. }
  83. // 设置错误信息
  84. if (is_array($message)) {
  85. $v->message($message);
  86. }
  87. // 使用回调验证
  88. if ($callback && is_callable($callback)) {
  89. call_user_func_array($callback, [$v, &$data]);
  90. }
  91. if (!$v->check($data)) {
  92. if ($this->failException) {
  93. throw new ValidateException($v->getError());
  94. }
  95. return $v->getError();
  96. }
  97. return true;
  98. }
  99. /**
  100. * 操作失败返回的数据
  101. * @param string $msg 提示信息
  102. * @param mixed $data 要返回的数据
  103. * @param int $code 错误码,默认为0
  104. * @param string $type 输出类型
  105. * @param array $header 发送的 Header 信息
  106. */
  107. protected function error($msg = '', $data = null, $code = 0, $type = null, array $header = [])
  108. {
  109. $this->result($msg, $data, $code, $type, $header);
  110. }
  111. /**
  112. * 返回封装后的 API 数据到客户端
  113. * @access protected
  114. * @param mixed $msg 提示信息
  115. * @param mixed $data 要返回的数据
  116. * @param int $code 错误码,默认为0
  117. * @param string $type 输出类型,支持json/xml/jsonp
  118. * @param array $header 发送的 Header 信息
  119. * @return void
  120. * @throws HttpResponseException
  121. */
  122. protected function result($msg, $data = null, $code = 0, $type = null, array $header = [])
  123. {
  124. $result = [
  125. 'code' => $code,
  126. 'msg' => $msg,
  127. 'time' => Request::instance()->server('REQUEST_TIME'),
  128. 'data' => $data,
  129. ];
  130. // 如果未设置类型则自动判断
  131. $type = $type ? $type : ($this->request->param(config('var_jsonp_handler')) ? 'jsonp' : $this->responseType);
  132. if (isset($header['statuscode'])) {
  133. $code = $header['statuscode'];
  134. unset($header['statuscode']);
  135. } else {
  136. //未设置状态码,根据code值判断
  137. $code = $code >= 1000 || $code < 200 ? 200 : $code;
  138. }
  139. $response = Response::create($result, $type, $code)->header($header);
  140. throw new HttpResponseException($response);
  141. }
  142. }