From c0281c0cc58e4bd346db68158a89a12191ff8d72 Mon Sep 17 00:00:00 2001 From: zhangheng Date: Thu, 30 Apr 2026 14:58:28 +0800 Subject: [PATCH] fix(project): sTATUS_CODE --- api/fetch.ts | 44 ++++++++++++++++++++++++++++- app/api/transfer/[path]/route.ts | 4 +-- components/label/utils/constants.ts | 1 + 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/api/fetch.ts b/api/fetch.ts index 2a4f405..f1d63b5 100644 --- a/api/fetch.ts +++ b/api/fetch.ts @@ -26,6 +26,7 @@ interface OptionsProps { method: string body?: BodyInit data?: any + responseType?: "blob" | "arraybuffer" | "stream" needCredentials?: boolean isUpload?: boolean isDownload?: boolean @@ -52,11 +53,23 @@ async function httpfetch(options: OptionsProps): Promise { }, } + const getResponseType = (requestOptions: OptionsProps) => { + return ( + requestOptions.responseType || + (requestOptions.headerAttr?.responseType as + | "blob" + | "arraybuffer" + | "stream" + | undefined) + ) + } + const createSsrRequest: ( options: OptionsProps, token?: string, refreshToken?: string ) => Request = (options) => { + const responseType = getResponseType(options) // 设置请求头 let myHeaders = new Headers() if (!options.isUpload && !options.isFormData && !options.notJson) { @@ -79,7 +92,13 @@ async function httpfetch(options: OptionsProps): Promise { cache: "default", body: options.isFormData ? options.body - : (JSON.stringify({ data: options.body, ...options }) as BodyInit), + : (JSON.stringify({ + data: options.body, + ...options, + options: { + responseType, + }, + }) as BodyInit), } const pathList = options.url.split("/") const request = new Request( @@ -108,6 +127,7 @@ async function httpfetch(options: OptionsProps): Promise { } if (options.headerAttr) { Object.entries(options.headerAttr).map(([key, value]) => { + if (key === "responseType") return myHeaders.append(key, value) }) } @@ -151,9 +171,12 @@ async function httpfetch(options: OptionsProps): Promise { return new Promise((resolved, rejected) => { fetch(request) .then(async (response) => { + const responseType = getResponseType(options) if (response.status === 200) { if (options.isDownload) { return response.blob() + } else if (responseType === "stream") { + return response } else { return response.headers .get("Content-Type") @@ -187,6 +210,8 @@ async function httpfetch(options: OptionsProps): Promise { }) const retry_request = createNewRequest(options, access_token) const retry_response = await fetch(retry_request) + const responseType = getResponseType(options) + if (responseType === "stream") return retry_response return options.isDownload ? retry_response.blob() : retry_response.json() @@ -257,6 +282,13 @@ async function httpfetch(options: OptionsProps): Promise { } }) .then(async (response) => { + if ( + getResponseType(options) === "stream" && + response instanceof Response + ) { + resolved(response as T) + return + } const code = response?.code const errInfo = response?.message || "" if (response) { @@ -279,6 +311,16 @@ async function httpfetch(options: OptionsProps): Promise { : retry_response.json() } catch (error) { rejected(error) + try { + await deleteCookie() + } catch (error) { + rejected(error) + } finally { + // 清空身份信息并跳转至登录页面 + removeUserInfo() + logout() + window.location.href = "/login" + } } } else if (status === 400) { const { message: errInfo } = response diff --git a/app/api/transfer/[path]/route.ts b/app/api/transfer/[path]/route.ts index b90e97e..4bbaba3 100644 --- a/app/api/transfer/[path]/route.ts +++ b/app/api/transfer/[path]/route.ts @@ -123,8 +123,8 @@ export async function POST(req: any) { if (!sessionResult.ok) { return attachServerTiming( NextResponse.json( - { message: sessionResult.message || "未获取到用户信息", code: 401 }, - { status: 401 } + { message: sessionResult.message || "未获取到用户信息", code: 406 }, + { status: 406 } ), { ...timing, diff --git a/components/label/utils/constants.ts b/components/label/utils/constants.ts index 45f3b6d..75a9599 100644 --- a/components/label/utils/constants.ts +++ b/components/label/utils/constants.ts @@ -39,6 +39,7 @@ export const STATUS_CODE = new Map([ [190, "Embedding生成中"], [191, "Embedding生成失败"], [199, "审核失败"], + [200, "已准备"], [201, "试标阶段"], // 试标中 [202, "试标阶段"], // 试标完成 [203, "试标阶段"], // 试标确认