fix(evaluation-groups): 修复 Authorization 头缺失问题
## 问题
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:
This commit is contained in:
@@ -1201,7 +1201,7 @@ export async function getEvaluationPointGroups(
|
||||
|
||||
const response = await apiRequest<EvaluationPointGroupListResponse>(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<EvaluationPointGroupListResponse>(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<EvaluationPointGroupResponse>(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<EvaluationPointGroupListResponse>(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}` } } : {})
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user