From fe918fe4ed0433a8ff7959721c9ed6dbe60fa8a4 Mon Sep 17 00:00:00 2001 From: wanghongjun <1445693971@qq.com> Date: Tue, 30 Jun 2026 14:54:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=AF=B7=E6=B1=82=E5=A4=B4?= =?UTF-8?q?=E9=83=A8=E5=85=A8=E9=83=A8=E5=BC=80=E5=8F=91=EF=BC=8C=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E8=BF=94=E5=9B=9E=E8=AF=B7=E6=B1=82=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/admin/Base.php | 17 ++++++++++------- app/controller/api/Base.php | 17 ++++++++++------- app/controller/api/Miss.php | 14 ++------------ app/middleware.php | 2 +- app/middleware/AdminAuth.php | 9 +++++---- app/middleware/AdminResponse.php | 20 +++++++++++++++++--- app/middleware/WikiAuth.php | 2 +- config/apiadmin.php | 9 ++++++++- config/cookie.php | 5 +++-- config/session.php | 3 +++ route/apiRoute.php | 12 ++++++------ 11 files changed, 66 insertions(+), 44 deletions(-) diff --git a/app/controller/admin/Base.php b/app/controller/admin/Base.php index 5c2a030..b465332 100644 --- a/app/controller/admin/Base.php +++ b/app/controller/admin/Base.php @@ -80,16 +80,19 @@ class Base extends BaseController { * @author zhaoxiang */ public function buildFailed(int $code, string $msg = '操作失败', array $data = []): Response { - $return = [ - 'code' => $code, - 'msg' => $msg, - 'data' => $data - ]; + $return = ['code' => $code, 'msg' => $msg, 'data' => $data]; if (Env::get('APP_DEBUG') && $this->debug) { $return['debug'] = $this->debug; } - - return json($return); + // 根据 code 映射 HTTP 状态码 + $httpCode = match($code) { + ReturnCode::AUTH_ERROR => 401, + ReturnCode::ACCESS_TOKEN_TIMEOUT => 401, + ReturnCode::INVALID => 404, + //ReturnCode::NO_PERMISSION => 403, + default => 400, + }; + return json($return, $httpCode); } /** diff --git a/app/controller/api/Base.php b/app/controller/api/Base.php index e29b357..805e94b 100644 --- a/app/controller/api/Base.php +++ b/app/controller/api/Base.php @@ -36,16 +36,19 @@ class Base extends BaseController { } public function buildFailed(int $code, string $msg = '操作失败', array $data = []): Response { - $return = [ - 'code' => $code, - 'msg' => $msg, - 'data' => $data - ]; + $return = ['code' => $code, 'msg' => $msg, 'data' => $data]; if (Env::get('APP_DEBUG') && $this->debug) { $return['debug'] = $this->debug; } - - return json($return); + // 根据 code 映射 HTTP 状态码 + $httpCode = match($code) { + ReturnCode::AUTH_ERROR => 401, + ReturnCode::ACCESS_TOKEN_TIMEOUT => 401, + ReturnCode::INVALID => 404, + //ReturnCode::NO_PERMISSION => 403, + default => 400, + }; + return json($return, $httpCode); } protected function debug($data): void { diff --git a/app/controller/api/Miss.php b/app/controller/api/Miss.php index 21cb289..552c691 100644 --- a/app/controller/api/Miss.php +++ b/app/controller/api/Miss.php @@ -3,6 +3,7 @@ declare (strict_types=1); namespace app\controller\api; +use app\util\ReturnCode; use think\Exception; use think\facade\App; use think\Response; @@ -10,17 +11,6 @@ use think\Response; class Miss extends Base { public function index(): Response { - $version = config('apiadmin.APP_VERSION'); - if (!$version) { - throw new Exception('请先执行安装脚本,完成项目初始化!'); - } else { - return $this->buildSuccess([ - 'Product' => config('apiadmin.APP_NAME'), - 'ApiVersion' => $version, - 'TpVersion' => App::version(), - 'Company' => config('apiadmin.COMPANY_NAME'), - 'ToYou' => "I'm glad to meet you(终于等到你!)" - ]); - } + return $this->buildFailed(ReturnCode::INVALID); } } diff --git a/app/middleware.php b/app/middleware.php index 9971bdf..158080f 100644 --- a/app/middleware.php +++ b/app/middleware.php @@ -7,5 +7,5 @@ return [ // \think\middleware\LoadLangPack::class, // Session初始化 \think\middleware\SessionInit::class, - \think\middleware\AllowCrossDomain::class, + //\think\middleware\AllowCrossDomain::class, ]; diff --git a/app/middleware/AdminAuth.php b/app/middleware/AdminAuth.php index 0d03a90..17c0a3a 100644 --- a/app/middleware/AdminAuth.php +++ b/app/middleware/AdminAuth.php @@ -17,7 +17,8 @@ class AdminAuth { */ public function handle($request, \Closure $next): Response { $header = config('apiadmin.CROSS_DOMAIN'); - $ApiAuth = $request->header('Api-Auth', ''); + //$ApiAuth = $request->header('Api-Auth', ''); + $ApiAuth = $request->header('token', ''); if ($ApiAuth) { $userInfo = cache('Login:' . $ApiAuth); if ($userInfo) { @@ -28,7 +29,7 @@ class AdminAuth { 'code' => ReturnCode::AUTH_ERROR, 'msg' => 'ApiAuth不匹配', 'data' => [] - ])->header($header); + ], 403)->header($header); } else { $request->API_ADMIN_USER_INFO = $userInfo; } @@ -37,9 +38,9 @@ class AdminAuth { } else { return json([ 'code' => ReturnCode::AUTH_ERROR, - 'msg' => '缺少ApiAuth', + 'msg' => 'ApiAuth不匹配', 'data' => [] - ])->header($header); + ], 403)->header($header); } } } diff --git a/app/middleware/AdminResponse.php b/app/middleware/AdminResponse.php index 40b8946..6967fbb 100644 --- a/app/middleware/AdminResponse.php +++ b/app/middleware/AdminResponse.php @@ -6,9 +6,23 @@ namespace app\middleware; use think\facade\Config; use think\Response; -class AdminResponse { +class AdminResponse +{ - public function handle($request, \Closure $next): Response { - return $next($request)->header(Config::get('apiadmin.CROSS_DOMAIN')); + public function handle($request, \Closure $next): Response + { + $response = $next($request); + $origin = $request->header('origin', ''); + $whitelist = Config::get('apiadmin.CORS_ORIGIN_WHITELIST', []); + if ($origin && in_array($origin, $whitelist, true)) { + $response->header(['Access-Control-Allow-Origin' => $origin]); + } + $response->header([ + 'Vary' => 'Origin', + 'Access-Control-Allow-Methods' => 'POST,GET,OPTIONS', + 'Access-Control-Allow-Headers' => 'Api-Auth,token,Authorization,Content-Type,X-Requested-With', + 'Access-Control-Allow-Credentials' => 'true', + ]); + return $response; } } diff --git a/app/middleware/WikiAuth.php b/app/middleware/WikiAuth.php index 14d5042..0aa7c15 100644 --- a/app/middleware/WikiAuth.php +++ b/app/middleware/WikiAuth.php @@ -30,7 +30,7 @@ class WikiAuth { 'code' => ReturnCode::AUTH_ERROR, 'msg' => 'ApiAuth不匹配', 'data' => [] - ])->header($header); + ], 403)->header($header); } else { $request->API_WIKI_USER_INFO = $userInfo; } diff --git a/config/apiadmin.php b/config/apiadmin.php index 9932349..72414ce 100644 --- a/config/apiadmin.php +++ b/config/apiadmin.php @@ -27,7 +27,7 @@ return [ //跨域配置 'CROSS_DOMAIN' => [ - 'Access-Control-Allow-Origin' => '*', + 'Access-Control-Allow-Origin' => '', 'Access-Control-Allow-Methods' => 'POST,PUT,GET,DELETE', 'Access-Control-Allow-Headers' => 'Version, Access-Token, User-Token, Api-Auth, User-Agent, Keep-Alive, Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With, Token', 'Access-Control-Allow-Credentials' => 'true' @@ -35,4 +35,11 @@ return [ //后台列表默认一页显示数量 'ADMIN_LIST_DEFAULT' => 20, + + 'CORS_ORIGIN_WHITELIST' => [ + 'https://ljclf.dgyq-water.com', + 'http://10.44.8.11', + 'http://localhost:8000', + // 其他需要的前端域名 + ], ]; diff --git a/config/cookie.php b/config/cookie.php index f728024..e8a8808 100644 --- a/config/cookie.php +++ b/config/cookie.php @@ -10,9 +10,10 @@ return [ // cookie 有效域名 'domain' => '', // cookie 启用安全传输 - 'secure' => false, + 'secure' => true, // httponly设置 - 'httponly' => false, + 'httponly' => true, + 'samesite' => 'Lax', // 防 CSRF // 是否使用 setcookie 'setcookie' => true, ]; diff --git a/config/session.php b/config/session.php index c1ef6e1..7df7119 100644 --- a/config/session.php +++ b/config/session.php @@ -16,4 +16,7 @@ return [ 'expire' => 1440, // 前缀 'prefix' => '', + 'secure' => true, + 'httponly' => true, + 'samesite' => 'Lax', ]; diff --git a/route/apiRoute.php b/route/apiRoute.php index c1aff7e..bc8bdf7 100644 --- a/route/apiRoute.php +++ b/route/apiRoute.php @@ -17,12 +17,12 @@ Route::group('api', function() { Route::rule('InvoiceIssuance/getErrorInfo', 'api.InvoiceIssuance/getErrorInfo', 'get')->middleware(\app\middleware\WechatAuth::class); Route::rule('InvoiceIssuance/validateFeePay', 'api.InvoiceIssuance/validateFeePay', 'post')->middleware(\app\middleware\WechatAuth::class); // 测试FeeService 接口 - Route::rule('Test/index', 'api.Test/index', 'get'); - Route::rule('Test1/index', 'api.Test1/index', 'get'); - Route::rule('Test/index', 'api.Test/apiInvoiceIssuance', 'post'); - Route::rule('Test/saveSummaryReport', 'api.Test/saveSummaryReport', 'post'); - Route::rule('Test/apiFinalReport', 'api.Test/apiFinalReport', 'post'); - Route::rule('InvoiceIssuance/downFile', 'api.InvoiceIssuance/downFile', 'get'); +// Route::rule('Test/index', 'api.Test/index', 'get'); +// Route::rule('Test1/index', 'api.Test1/index', 'get'); +// Route::rule('Test/index', 'api.Test/apiInvoiceIssuance', 'post'); +// Route::rule('Test/saveSummaryReport', 'api.Test/saveSummaryReport', 'post'); +// Route::rule('Test/apiFinalReport', 'api.Test/apiFinalReport', 'post'); +// Route::rule('InvoiceIssuance/downFile', 'api.InvoiceIssuance/downFile', 'get'); //MISS路由定义 //Route::miss('api.Miss/index'); });//->middleware(app\middleware\ApiResponse::class)