123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- // +----------------------------------------------------------------------
- // | Yzncms [ 御宅男工作室 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2018 http://yzncms.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: 御宅男 <530765310@qq.com>
- // +----------------------------------------------------------------------
-
- // +----------------------------------------------------------------------
- // | 接口控制模块
- // +----------------------------------------------------------------------
- namespace app\common\controller;
-
- use think\App;
- use think\exception\HttpResponseException;
- use think\exception\ValidateException;
- use think\Response;
- use think\Validate;
-
- class Api
- {
- /**
- * @var Request Request 实例
- */
- protected $request;
-
- /**
- * @var bool 验证失败是否抛出异常
- */
- protected $failException = false;
-
- /**
- * @var bool 是否批量验证
- */
- protected $batchValidate = false;
-
- /**
- * 默认响应输出类型,支持json/xml
- * @var string
- */
- protected $responseType = 'json';
-
- /**
- * 构造方法
- * @access public
- * @param Request $request Request 对象
- */
- public function __construct(App $app)
- {
- $this->request = $app->request;
- // 控制器初始化
- $this->initialize();
- }
-
- /**
- * 初始化操作
- * @access protected
- */
- protected function initialize()
- {
- //跨域请求检测
- check_cors_request();
- //移除HTML标签
- $this->request->filter('trim,strip_tags,htmlspecialchars');
- }
-
- /**
- * 操作成功返回的数据
- * @param string $msg 提示信息
- * @param mixed $data 要返回的数据
- * @param int $code 错误码,默认为1
- * @param string $type 输出类型
- * @param array $header 发送的 Header 信息
- */
- protected function success($msg = '', $data = null, $code = 1, $type = null, array $header = [])
- {
- $this->result($msg, $data, $code, $type, $header);
- }
-
- /**
- * 操作失败返回的数据
- * @param string $msg 提示信息
- * @param mixed $data 要返回的数据
- * @param int $code 错误码,默认为0
- * @param string $type 输出类型
- * @param array $header 发送的 Header 信息
- */
- protected function error($msg = '', $data = null, $code = 0, $type = null, array $header = [])
- {
- $this->result($msg, $data, $code, $type, $header);
- }
-
- /**
- * 返回封装后的 API 数据到客户端
- * @access protected
- * @param mixed $msg 提示信息
- * @param mixed $data 要返回的数据
- * @param int $code 错误码,默认为0
- * @param string $type 输出类型,支持json/xml/jsonp
- * @param array $header 发送的 Header 信息
- * @return void
- * @throws HttpResponseException
- */
- protected function result($msg, $data = null, $code = 0, $type = null, array $header = [])
- {
- $result = [
- 'code' => $code,
- 'msg' => $msg,
- 'time' => $this->request->server('REQUEST_TIME'),
- 'data' => $data,
- ];
- // 如果未设置类型则自动判断
- $type = $type ? $type : ($this->request->param(config('var_jsonp_handler')) ? 'jsonp' : $this->responseType);
-
- if (isset($header['statuscode'])) {
- $code = $header['statuscode'];
- unset($header['statuscode']);
- } else {
- //未设置状态码,根据code值判断
- $code = $code >= 1000 || $code < 200 ? 200 : $code;
- }
- $response = Response::create($result, $type, $code)->header($header);
- throw new HttpResponseException($response);
- }
-
- /**
- * 设置验证失败后是否抛出异常
- * @access protected
- * @param bool $fail 是否抛出异常
- * @return $this
- */
- protected function validateFailException($fail = true)
- {
- $this->failException = $fail;
-
- return $this;
- }
-
- /**
- * 验证数据
- * @access protected
- * @param array $data 数据
- * @param string|array $validate 验证器名或者验证规则数组
- * @param array $message 提示信息
- * @param bool $batch 是否批量验证
- * @param mixed $callback 回调方法(闭包)
- * @return array|string|true
- * @throws ValidateException
- */
- protected function validate($data, $validate, $message = [], $batch = false, $callback = null)
- {
- if (is_array($validate)) {
- $v = new Validate();
- $v->rule($validate);
- } else {
- if (strpos($validate, '.')) {
- // 支持场景
- list($validate, $scene) = explode('.', $validate);
- }
- $v = new Validate($validate);
- if (!empty($scene)) {
- $v->scene($scene);
- }
- }
-
- // 是否批量验证
- if ($batch || $this->batchValidate) {
- $v->batch(true);
- }
-
- if (is_array($message)) {
- $v->message($message);
- }
-
- if ($callback && is_callable($callback)) {
- call_user_func_array($callback, [$v, &$data]);
- }
-
- if (!$v->check($data)) {
- if ($this->failException) {
- throw new ValidateException($v->getError());
- }
- return $v->getError();
- }
-
- return true;
- }
-
- //刷新Token
- protected function token()
- {
- $token = $this->request->param('__token__');
- //验证Token
- if (!Validate::make()->check(['__token__' => $token], ['__token__' => 'require|token'])) {
- $this->error('令牌错误!', '', ['__token__' => $this->request->token()]);
- }
- //刷新Token
- $this->request->token();
- }
- }
|