Skip to content

Commit 4e12eb8

Browse files
committed
修复一些警告
1 parent a13681d commit 4e12eb8

6 files changed

Lines changed: 23 additions & 15701 deletions

File tree

app/api/chat/route.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,24 @@ export async function POST(req: Request) {
4040
const timeoutId = setTimeout(() => controller.abort(), 5000); // 5秒超时
4141

4242
// 原封不动把前端的参数丢给 Java
43-
const proxyRes = await fetch(`${backendUrl}/openai/responses/stream`, {
44-
method: "POST",
45-
headers: {
46-
"Content-Type": "application/json",
47-
"x-satoken": req.headers.get("x-satoken") || "",
48-
},
49-
body: await proxyReq.text(),
50-
signal: controller.signal,
51-
});
52-
53-
clearTimeout(timeoutId);
43+
let proxyRes: Response;
44+
try {
45+
proxyRes = await fetch(`${backendUrl}/openai/responses/stream`, {
46+
method: "POST",
47+
headers: {
48+
"Content-Type": "application/json",
49+
// 浏览器侧用 x-satoken 传递 token,转发给后端时改回后端期望的 satoken
50+
...(req.headers.get("x-satoken")
51+
? { satoken: req.headers.get("x-satoken")! }
52+
: {}),
53+
},
54+
body: await proxyReq.text(),
55+
signal: controller.signal,
56+
});
57+
} finally {
58+
// 无论成功还是抛出(网络错误/超时中断),都清除定时器
59+
clearTimeout(timeoutId);
60+
}
5461

5562
// 如果 Java 后端返回成功,则直接把它的流传回浏览器,提前结束
5663
if (proxyRes.ok && proxyRes.body) {

app/api/upload/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ interface UploadRequest {
3636
*/
3737
export async function POST(request: NextRequest) {
3838
try {
39-
// 从请求头读取 satoken,转发给后端验证
40-
const token = request.headers.get("satoken");
39+
// 从请求头读取 x-satoken(客户端侧统一约定),转发后端时改为 satoken
40+
const token = request.headers.get("x-satoken");
4141
if (!token) {
4242
return NextResponse.json({ error: "未授权访问" }, { status: 401 });
4343
}

app/editor/EditorPageClient.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ export function EditorPageClient({ user }: EditorPageClientProps) {
8282
file: File,
8383
articleSlug: string,
8484
): Promise<{ blobUrl: string; publicUrl: string }> => {
85-
// 1. 获取预签名 URL(带 satoken 请求头,供服务端验证身份)
85+
// 1. 获取预签名 URL(带 x-satoken 请求头,供服务端验证身份)
8686
const token = localStorage.getItem("satoken") ?? "";
8787
const response = await fetch("/api/upload", {
8888
method: "POST",
8989
headers: {
9090
"Content-Type": "application/json",
91-
satoken: token,
91+
"x-satoken": token,
9292
},
9393
body: JSON.stringify({
9494
filename: file.name,

lib/server-auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* 服务端身份验证工具函数(仅用于 Next.js API Route / Server Component)
33
*
4-
* 通过 x-satoken 请求头调用后端 /auth/me 验证身份,
4+
* 从入参请求的 x-satoken 头读取 token,以 satoken 头转发给后端 /auth/me 验证身份,
55
* 返回 user_accounts.id(BigInt),匿名或 token 无效时返回 null。
66
*
77
* 使用方:app/api/chat/route.ts、app/api/analytics/route.ts

0 commit comments

Comments
 (0)