WebIndexApi.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\Person;
  4. use app\common\library\Token;
  5. use think\exception\HttpResponseException;
  6. use think\exception\ValidateException;
  7. use think\Loader;
  8. use think\Request;
  9. use think\Response;
  10. header('Access-Control-Allow-Headers: content-type,x-requested-with,Access-Token,sign-no,Token,token,Authorization');
  11. class WebIndexApi
  12. {
  13. protected $request;
  14. protected $personId;
  15. protected $personInfo;
  16. /**
  17. * @var bool 是否批量验证
  18. */
  19. protected $batchValidate = false;
  20. /**
  21. * @var bool 验证失败是否抛出异常
  22. */
  23. protected $failException = false;
  24. /**
  25. * 默认响应输出类型,支持json/xml
  26. * @var string
  27. */
  28. protected $responseType = 'json';
  29. /**
  30. * 无需登录的方法,同时也就不需要鉴权了
  31. * @var array
  32. */
  33. protected $noNeedLogin = [];
  34. /**
  35. * 无需鉴权的方法,但需要登录
  36. * @var array
  37. */
  38. protected $noNeedRight = [];
  39. //Token默认有效时长
  40. protected $keeptime = 2592000;
  41. public function __construct(Request $request = null) {
  42. $this->request = is_null($request) ? Request::instance() : $request;
  43. // 控制器初始化
  44. $this->_initialize();
  45. }
  46. public function _initialize(Request $request = null)
  47. {
  48. //跨域请求检测
  49. check_cors_request();
  50. $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token')));
  51. if (!array_search('*', $this->noNeedLogin) && !in_array(request()->action(), $this->noNeedLogin)) {
  52. if (!$info = Token::get($token)) {
  53. $this->error('token 已过期', null, 401);
  54. }
  55. if (empty($info['user_id'])) {
  56. $this->error('token无效请重新登陆', ['token' => $token, 'info' => $info], 401);
  57. }
  58. $this->personId = $info['user_id'];
  59. $this->personInfo = Person::where('id', $this->personId)->find();
  60. }else if($token){
  61. if (!$info = Token::get($token)) {
  62. $this->error('token 已过期', null, 401);
  63. }
  64. if (empty($info['user_id'])) {
  65. $this->error('token无效请重新登陆', ['token' => $token, 'info' => $info], 401);
  66. }
  67. $this->personId = $info['user_id'];
  68. $this->personInfo = Person::where('id', $this->personId)->find();
  69. }
  70. }
  71. /**
  72. * 操作成功返回的数据
  73. * @param string $msg 提示信息
  74. * @param mixed $data 要返回的数据
  75. * @param int $code 错误码,默认为1
  76. * @param string $type 输出类型
  77. * @param array $header 发送的 Header 信息
  78. */
  79. protected function success($msg = '', $data = null, $code = 1, $type = null, array $header = []) {
  80. $this->result($msg, $data, $code, $type, $header);
  81. }
  82. /**
  83. * 操作失败返回的数据
  84. * @param string $msg 提示信息
  85. * @param mixed $data 要返回的数据
  86. * @param int $code 错误码,默认为0
  87. * @param string $type 输出类型
  88. * @param array $header 发送的 Header 信息
  89. */
  90. protected function error($msg = '', $data = null, $code = 0, $type = null, array $header = []) {
  91. $this->result($msg, $data, $code, $type, $header);
  92. }
  93. /**
  94. * 返回封装后的 API 数据到客户端
  95. * @access protected
  96. * @param mixed $msg 提示信息
  97. * @param mixed $data 要返回的数据
  98. * @param int $code 错误码,默认为0
  99. * @param string $type 输出类型,支持json/xml/jsonp
  100. * @param array $header 发送的 Header 信息
  101. * @return void
  102. * @throws HttpResponseException
  103. */
  104. protected function result($msg, $data = null, $code = 0, $type = null, array $header = []) {
  105. $result = [
  106. 'code' => $code,
  107. 'msg' => $msg,
  108. 'time' => Request::instance()->server('REQUEST_TIME'),
  109. 'data' => $data,
  110. ];
  111. // 如果未设置类型则自动判断
  112. $type = $type ? $type : ($this->request->param(config('var_jsonp_handler')) ? 'jsonp' : $this->responseType);
  113. $response = Response::create($result, $type, 200)->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. $this->failException = $fail;
  124. return $this;
  125. }
  126. /**
  127. * 验证数据
  128. * @access protected
  129. * @param array $data 数据
  130. * @param string|array $validate 验证器名或者验证规则数组
  131. * @param array $message 提示信息
  132. * @param bool $batch 是否批量验证
  133. * @param mixed $callback 回调方法(闭包)
  134. * @return array|string|true
  135. * @throws ValidateException
  136. */
  137. protected function validate($data, $validate, $message = [], $batch = false, $callback = null) {
  138. if (is_array($validate)) {
  139. $v = Loader::validate();
  140. $v->rule($validate);
  141. } else {
  142. // 支持场景
  143. if (strpos($validate, '.')) {
  144. list($validate, $scene) = explode('.', $validate);
  145. }
  146. $v = Loader::validate($validate);
  147. !empty($scene) && $v->scene($scene);
  148. }
  149. // 批量验证
  150. if ($batch || $this->batchValidate) {
  151. $v->batch(true);
  152. }
  153. // 设置错误信息
  154. if (is_array($message)) {
  155. $v->message($message);
  156. }
  157. // 使用回调验证
  158. if ($callback && is_callable($callback)) {
  159. call_user_func_array($callback, [$v, &$data]);
  160. }
  161. if (!$v->check($data)) {
  162. if ($this->failException) {
  163. throw new ValidateException($v->getError());
  164. }
  165. return $v->getError();
  166. }
  167. return true;
  168. }
  169. }