diff --git a/app/api/BaseController.php b/app/api/BaseController.php index 255b64a..f337228 100644 --- a/app/api/BaseController.php +++ b/app/api/BaseController.php @@ -1,247 +1,247 @@ - 'ae47e94a7dcd1fdfacb499b60e361a8d', - 'iss' => '', //签发者 可选 - 'aud' => '', //接收该JWT的一方,可选 - 'exptime' => '', //过期时间,这里设置2个小时 - ]; - /** - * 构造方法 - * @access public - * @param App $app 应用对象 - */ - public function __construct(App $app) - { - $this->app = $app; - $this->request = $this->app->request; - $this->jwt_conf = get_system_config('token'); - // 控制器初始化 - $this->initialize(); - } - - // 初始化 - protected function initialize() - { - - //每页显示数据量 - $this->pageSize = Request::param('page_size', \think\facade\Config::get('app.page_size')); - -<<<<<<< HEAD -// 暂时去除 ----------------------------------------------------- -// $token = Request::header('x-Token'); -// if ($token) { -// if (strpos($token, 'Bearer') === 0){ -// $token = trim(substr($token, 6)); -// } -// if (count(explode('.', $token)) != 3) { -// return json(['code'=>404,'msg'=>'非法请求']); -// } -// $config = get_system_config('token'); - -// return json($token); -// JWT::$leeway = 60;//当前时间减去60,把时间留点余地 -// $decoded = JWT::decode($token, new Key('ae47e94a7dcd1fdfacb499b60e361a8d', 'HS256')); //HS256方式,这里要和签发的时候对应 - -// return $decoded->jti[0]; -// exit; -// //var_dump($config);exit; -// try { -// JWT::$leeway = 60;//当前时间减去60,把时间留点余地 -// $decoded = JWT::decode($token, new Key('ae47e94a7dcd1fdfacb499b60e361a8d', 'HS256')); //HS256方式,这里要和签发的时候对应 - -// //return (array)$decoded; -// // $decoded_array = json_decode(json_encode($decoded),TRUE); -// // $jwt_data = $decoded_array['data']; - -// $user=Db::connect('shop')->name('nk_user')->where('user_id',$decoded->jti[0])->find(); -// if (!$user){ -// return json(['code'=>403,'msg'=>'签名错误']); -// return false; -// } -// $user = Db::table('fa_user')->where('id',$user['n_user_id'])->find(); -// //$request->uid = $jwt_data['userid']; -// define('$this->request->uid', $user['id']); -// // $response = $next($request); -// // return $response; -// //return $next($request); -// } catch(\Firebase\JWT\SignatureInvalidException $e) { //签名不正确 -// return json(['code'=>403,'msg'=>'签名错误']); -// }catch(\Firebase\JWT\BeforeValidException $e) { // 签名在某个时间点之后才能用 -// return json(['code'=>401,'msg'=>'token失效']); -// }catch(\Firebase\JWT\ExpiredException $e) { // token过期 -// return json(['code'=>401,'msg'=>'token已过期']); -// }catch(Exception $e) { //其他错误 -// return json(['code'=>404,'msg'=>'非法请求']); -// }catch(\UnexpectedValueException $e) { //其他错误 -// return json(['code'=>404,'msg'=>'非法请求']); -// } catch(\DomainException $e) { //其他错误 -// return json(['code'=>404,'msg'=>'非法请求']); -// } -// }else{ -// define('$this->request->uid', ''); -// } - -// 暂时去除 ----------------------------------------------------- -======= - $token = Request::header('x-Token'); - if ($token) { - if (strpos($token, 'Bearer') === 0){ - $token = trim(substr($token, 6)); - } - if (count(explode('.', $token)) != 3) { - return json(['code'=>404,'msg'=>'非法请求']); - } - $config = get_system_config('token'); - try { - JWT::$leeway = 60;//当前时间减去60,把时间留点余地 - $decoded = JWT::decode($token, new Key('ae47e94a7dcd1fdfacb499b60e361a8d', 'HS256')); //HS256方式,这里要和签发的时候对应 - $user=Db::connect('shop')->name('nk_user')->where('user_id',$decoded->jti[0])->find(); - if ($user && $user['n_user_id']!=0){ - $this->request->uid=$user['n_user_id']; - }else{ - $this->request->uid=$this->addUser($decoded->jti[0],$user); - } - //return (array)$decoded; -// $decoded_array = json_decode(json_encode($decoded),TRUE); -// $jwt_data = $decoded_array['data']; - - if (!$user){ - return json(['code'=>403,'msg'=>'签名错误']); - return false; - } - $user = Db::table('fa_user')->where('id',$user['n_user_id'])->find(); - //$request->uid = $jwt_data['userid']; - // define('JWT_UID', $user['id']); -// $response = $next($request); -// return $response; - //return $next($request); - } catch(\Firebase\JWT\SignatureInvalidException $e) { //签名不正确 - return json(['code'=>403,'msg'=>'签名错误']); - }catch(\Firebase\JWT\BeforeValidException $e) { // 签名在某个时间点之后才能用 - return json(['code'=>401,'msg'=>'token失效']); - }catch(\Firebase\JWT\ExpiredException $e) { // token过期 - return json(['code'=>401,'msg'=>'token已过期']); - }catch(Exception $e) { //其他错误 - return json(['code'=>404,'msg'=>'非法请求']); - }catch(\UnexpectedValueException $e) { //其他错误 - return json(['code'=>404,'msg'=>'非法请求']); - } catch(\DomainException $e) { //其他错误 - return json(['code'=>404,'msg'=>'非法请求']); - } - }else{ - // define('JWT_UID', ''); - return json(['code'=>404,'msg'=>'token不能为空']); - } - ->>>>>>> 更新接口验证方式,统一改为:$this->request->参数 - } - - /** - * Api处理成功结果返回方法 - * @param $message - * @param null $redirect - * @param null $extra - * @return mixed - * @throws ReturnException - */ - protected function apiSuccess($msg = 'success',$data=[]) - { - return $this->apiReturn($data, 1, $msg); - } - - /** - * Api处理结果失败返回方法 - * @param $error_code - * @param $message - * @param null $redirect - * @param null $extra - * @return mixed - * @throws ReturnException - */ - protected function apiError($msg = 'fail',$data=[], $code = 0) - { - return $this->apiReturn($data, $code, $msg); - } - - /** - * 返回封装后的API数据到客户端 - * @param mixed $data 要返回的数据 - * @param integer $code 返回的code - * @param mixed $msg 提示信息 - * @param string $type 返回数据格式 - * @param array $header 发送的Header信息 - * @return Response - */ - protected function apiReturn($data, int $code = 0, $msg = '', string $type = '', array $header = []): Response - { - $result = [ - 'code' => $code, - 'msg' => $msg, - 'time' => time(), - 'data' => $data, - ]; - - $type = $type ?: 'json'; - $response = Response::create($result, $type)->header($header); - - throw new HttpResponseException($response); - } - -} + 'ae47e94a7dcd1fdfacb499b60e361a8d', + 'iss' => '', //签发者 可选 + 'aud' => '', //接收该JWT的一方,可选 + 'exptime' => '', //过期时间,这里设置2个小时 + ]; + /** + * 构造方法 + * @access public + * @param App $app 应用对象 + */ + public function __construct(App $app) + { + $this->app = $app; + $this->request = $this->app->request; + $this->jwt_conf = get_system_config('token'); + // 控制器初始化 + $this->initialize(); + } + + // 初始化 + protected function initialize() + { + + //每页显示数据量 + $this->pageSize = Request::param('page_size', \think\facade\Config::get('app.page_size')); + +<<<<<<< HEAD +// 暂时去除 ----------------------------------------------------- +// $token = Request::header('x-Token'); +// if ($token) { +// if (strpos($token, 'Bearer') === 0){ +// $token = trim(substr($token, 6)); +// } +// if (count(explode('.', $token)) != 3) { +// return json(['code'=>404,'msg'=>'非法请求']); +// } +// $config = get_system_config('token'); + +// return json($token); +// JWT::$leeway = 60;//当前时间减去60,把时间留点余地 +// $decoded = JWT::decode($token, new Key('ae47e94a7dcd1fdfacb499b60e361a8d', 'HS256')); //HS256方式,这里要和签发的时候对应 + +// return $decoded->jti[0]; +// exit; +// //var_dump($config);exit; +// try { +// JWT::$leeway = 60;//当前时间减去60,把时间留点余地 +// $decoded = JWT::decode($token, new Key('ae47e94a7dcd1fdfacb499b60e361a8d', 'HS256')); //HS256方式,这里要和签发的时候对应 + +// //return (array)$decoded; +// // $decoded_array = json_decode(json_encode($decoded),TRUE); +// // $jwt_data = $decoded_array['data']; + +// $user=Db::connect('shop')->name('nk_user')->where('user_id',$decoded->jti[0])->find(); +// if (!$user){ +// return json(['code'=>403,'msg'=>'签名错误']); +// return false; +// } +// $user = Db::table('fa_user')->where('id',$user['n_user_id'])->find(); +// //$request->uid = $jwt_data['userid']; +// define('$this->request->uid', $user['id']); +// // $response = $next($request); +// // return $response; +// //return $next($request); +// } catch(\Firebase\JWT\SignatureInvalidException $e) { //签名不正确 +// return json(['code'=>403,'msg'=>'签名错误']); +// }catch(\Firebase\JWT\BeforeValidException $e) { // 签名在某个时间点之后才能用 +// return json(['code'=>401,'msg'=>'token失效']); +// }catch(\Firebase\JWT\ExpiredException $e) { // token过期 +// return json(['code'=>401,'msg'=>'token已过期']); +// }catch(Exception $e) { //其他错误 +// return json(['code'=>404,'msg'=>'非法请求']); +// }catch(\UnexpectedValueException $e) { //其他错误 +// return json(['code'=>404,'msg'=>'非法请求']); +// } catch(\DomainException $e) { //其他错误 +// return json(['code'=>404,'msg'=>'非法请求']); +// } +// }else{ +// define('$this->request->uid', ''); +// } + +// 暂时去除 ----------------------------------------------------- +======= + $token = Request::header('x-Token'); + if ($token) { + if (strpos($token, 'Bearer') === 0){ + $token = trim(substr($token, 6)); + } + if (count(explode('.', $token)) != 3) { + return json(['code'=>404,'msg'=>'非法请求']); + } + $config = get_system_config('token'); + try { + JWT::$leeway = 60;//当前时间减去60,把时间留点余地 + $decoded = JWT::decode($token, new Key('ae47e94a7dcd1fdfacb499b60e361a8d', 'HS256')); //HS256方式,这里要和签发的时候对应 + $user=Db::connect('shop')->name('nk_user')->where('user_id',$decoded->jti[0])->find(); + if ($user && $user['n_user_id']!=0){ + $this->request->uid=$user['n_user_id']; + }else{ + $this->request->uid=$this->addUser($decoded->jti[0],$user); + } + //return (array)$decoded; +// $decoded_array = json_decode(json_encode($decoded),TRUE); +// $jwt_data = $decoded_array['data']; + + if (!$user){ + return json(['code'=>403,'msg'=>'签名错误']); + return false; + } + $user = Db::table('fa_user')->where('id',$user['n_user_id'])->find(); + //$request->uid = $jwt_data['userid']; + // define('JWT_UID', $user['id']); +// $response = $next($request); +// return $response; + //return $next($request); + } catch(\Firebase\JWT\SignatureInvalidException $e) { //签名不正确 + return json(['code'=>403,'msg'=>'签名错误']); + }catch(\Firebase\JWT\BeforeValidException $e) { // 签名在某个时间点之后才能用 + return json(['code'=>401,'msg'=>'token失效']); + }catch(\Firebase\JWT\ExpiredException $e) { // token过期 + return json(['code'=>401,'msg'=>'token已过期']); + }catch(Exception $e) { //其他错误 + return json(['code'=>404,'msg'=>'非法请求']); + }catch(\UnexpectedValueException $e) { //其他错误 + return json(['code'=>404,'msg'=>'非法请求']); + } catch(\DomainException $e) { //其他错误 + return json(['code'=>404,'msg'=>'非法请求']); + } + }else{ + // define('JWT_UID', ''); + return json(['code'=>404,'msg'=>'token不能为空']); + } + +>>>>>>> 更新接口验证方式,统一改为:$this->request->参数 + } + + /** + * Api处理成功结果返回方法 + * @param $message + * @param null $redirect + * @param null $extra + * @return mixed + * @throws ReturnException + */ + protected function apiSuccess($msg = 'success',$data=[]) + { + return $this->apiReturn($data, 1, $msg); + } + + /** + * Api处理结果失败返回方法 + * @param $error_code + * @param $message + * @param null $redirect + * @param null $extra + * @return mixed + * @throws ReturnException + */ + protected function apiError($msg = 'fail',$data=[], $code = 0) + { + return $this->apiReturn($data, $code, $msg); + } + + /** + * 返回封装后的API数据到客户端 + * @param mixed $data 要返回的数据 + * @param integer $code 返回的code + * @param mixed $msg 提示信息 + * @param string $type 返回数据格式 + * @param array $header 发送的Header信息 + * @return Response + */ + protected function apiReturn($data, int $code = 0, $msg = '', string $type = '', array $header = []): Response + { + $result = [ + 'code' => $code, + 'msg' => $msg, + 'time' => time(), + 'data' => $data, + ]; + + $type = $type ?: 'json'; + $response = Response::create($result, $type)->header($header); + + throw new HttpResponseException($response); + } + +} diff --git a/app/api/middleware.php b/app/api/middleware.php index e546f84..9dc2b99 100644 --- a/app/api/middleware.php +++ b/app/api/middleware.php @@ -1,14 +1,14 @@ ->>>>>> 更新接口验证方式,统一改为:$this->request->参数 +>>>>>> 更新接口验证方式,统一改为:$this->request->参数 ]; \ No newline at end of file