解决冲突文件

This commit is contained in:
monanxiao 2023-03-15 18:07:41 +08:00 committed by liuxiaoquan
parent 2153c2b245
commit 56e96cb999
2 changed files with 260 additions and 260 deletions

View File

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

View File

@ -1,14 +1,14 @@
<?php <?php
// 这是系统自动生成的middleware定义文件 // 这是系统自动生成的middleware定义文件
return [ return [
//开启session中间件 //开启session中间件
//'think\middleware\SessionInit', //'think\middleware\SessionInit',
//验证勾股cms是否完成安装 //验证勾股cms是否完成安装
\app\home\middleware\Install::class, \app\home\middleware\Install::class,
\app\api\middleware\AllowOriginMiddleware::class, \app\api\middleware\AllowOriginMiddleware::class,
<<<<<<< HEAD <<<<<<< HEAD
======= =======
// \app\api\middleware\Auth::class, // \app\api\middleware\Auth::class,
>>>>>>> 更新接口验证方式,统一改为:$this->request->参数 >>>>>>> 更新接口验证方式,统一改为:$this->request->参数
]; ];