From da7e565bbbedb3443ced527a222c97a880b67f8b Mon Sep 17 00:00:00 2001 From: Wenyan Date: Wed, 26 Nov 2025 10:37:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(evaluation-groups):=20=E4=BF=AE=E5=A4=8D=20?= =?UTF-8?q?Authorization=20=E5=A4=B4=E7=BC=BA=E5=A4=B1=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 问题 GET 和 DELETE 请求在 token 为 undefined 时,传递空对象 `{}` 作为 headers,导致 axios-client 拦截器无法自动添加 Authorization 头。 ## 根本原因 ```typescript // ❌ 错误写法 headers: token ? { 'Authorization': `Bearer ${token}` } : {} ``` 当 token 为 undefined 时,传递的是空对象 `{}`,axios-client 认为已经提供了 headers,就跳过拦截器。 ## 修复方案 ```typescript // ✅ 正确写法 ...(token ? { headers: { 'Authorization': `Bearer ${token}` } } : {}) ``` 当 token 为 undefined 时,完全不传 headers 参数,让 axios-client 拦截器自动添加。 ## 修复的函数 1. ✅ getEvaluationPointGroups (GET) 2. ✅ getAllEvaluationPointGroups (GET) 3. ✅ getEvaluationPointGroup (GET) 4. ✅ getEvaluationPointGroupChildren (GET) 5. ✅ deleteEvaluationPointGroup (DELETE) ## 未修复的函数(无需修复) - createEvaluationPointGroup (POST) - headers 总是包含 Content-Type - updateEvaluationPointGroup (PUT) - headers 总是包含 Content-Type - batchUpdateEvaluationPointGroupStatus (PATCH) - headers 总是包含 Content-Type - batchDeleteEvaluationPointGroups (DELETE) - headers 总是包含 Content-Type POST/PUT/PATCH 请求因为总是需要设置 Content-Type,所以 headers 对象总是存在,拦截器会正常工作。 ## 影响 修复后,即使 token 参数为 undefined,axios-client 拦截器也能正常添加 Authorization 头。 ## 文件 - app/api/evaluation_points/rule-groups.ts ## 相关日志 解决了控制台警告: ⚠️ [apiRequest] 请求缺少 Authorization 头!headers: --- app/api/evaluation_points/rule-groups.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/api/evaluation_points/rule-groups.ts b/app/api/evaluation_points/rule-groups.ts index b243be9..19e02f3 100644 --- a/app/api/evaluation_points/rule-groups.ts +++ b/app/api/evaluation_points/rule-groups.ts @@ -1201,7 +1201,7 @@ export async function getEvaluationPointGroups( const response = await apiRequest(url, { method: 'GET', - headers: token ? { 'Authorization': `Bearer ${token}` } : {} + ...(token ? { headers: { 'Authorization': `Bearer ${token}` } } : {}) }); // 🔍 调试:打印完整响应 @@ -1260,7 +1260,7 @@ export async function getAllEvaluationPointGroups( const response = await apiRequest(url, { method: 'GET', - headers: token ? { 'Authorization': `Bearer ${token}` } : {} + ...(token ? { headers: { 'Authorization': `Bearer ${token}` } } : {}) }); if (response.error) { @@ -1302,7 +1302,7 @@ export async function getEvaluationPointGroup( const response = await apiRequest(url, { method: 'GET', - headers: token ? { 'Authorization': `Bearer ${token}` } : {} + ...(token ? { headers: { 'Authorization': `Bearer ${token}` } } : {}) }); if (response.error) { @@ -1352,7 +1352,7 @@ export async function getEvaluationPointGroupChildren( const response = await apiRequest(url, { method: 'GET', - headers: token ? { 'Authorization': `Bearer ${token}` } : {} + ...(token ? { headers: { 'Authorization': `Bearer ${token}` } } : {}) }); if (response.error) { @@ -1511,7 +1511,7 @@ export async function deleteEvaluationPointGroup( `/api/v3/evaluation-point-groups/${id}`, { method: 'DELETE', - headers: token ? { 'Authorization': `Bearer ${token}` } : {} + ...(token ? { headers: { 'Authorization': `Bearer ${token}` } } : {}) } );