From 8f8def3a87bc12b0d840d04c5ddadaf29e887bb0 Mon Sep 17 00:00:00 2001 From: luorijun Date: Mon, 20 Apr 2026 15:32:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=BC=82=E5=B8=B8=E8=B7=B3?= =?UTF-8?q?=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/actions/base.ts | 86 ++++++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 41 deletions(-) diff --git a/src/actions/base.ts b/src/actions/base.ts index 8daa771..04d7336 100644 --- a/src/actions/base.ts +++ b/src/actions/base.ts @@ -2,6 +2,7 @@ import {API_BASE_URL, ApiResponse, CLIENT_ID, CLIENT_SECRET} from '@/lib/api' import {add, isBefore} from 'date-fns' import {cookies, headers} from 'next/headers' +import {redirect} from 'next/navigation' import {cache} from 'react' export type TokenResp = { @@ -106,7 +107,6 @@ const _callByUser = cache(async ( // ====================== async function call(url: string, body: RequestInit['body'], auth?: string): Promise> { - let response: Response try { const reqHeaders = await headers() const reqIP = reqHeaders.get('x-forwarded-for') @@ -118,55 +118,59 @@ async function call(url: string, body: RequestInit['body'], auth? if (reqIP) callHeaders['X-Forwarded-For'] = reqIP if (reqUA) callHeaders['User-Agent'] = reqUA - response = await fetch(url, { + const response = await fetch(url, { method: 'POST', headers: callHeaders, body, }) + + if (response.status === 401) { + return redirect('/login?redirect=' + encodeURIComponent(url.replace(API_BASE_URL, ''))) + } + + const type = response.headers.get('Content-Type') ?? 'text/plain' + if (type.indexOf('text/plain') !== -1) { + const text = await response.text() + if (!response.ok) { + console.log('后端请求失败', url, `status=${response.status}`, text) + return { + success: false, + status: response.status, + message: text || '请求失败', + } + } + + if (!!text?.trim()?.length) { + console.log('未处理的响应成功', `type=text`, `text=${text}`) + } + return { + success: true, + data: undefined as R, // 强转类型,考虑优化 + } + } + else if (type.indexOf('application/json') !== -1) { + const json = await response.json() + if (!response.ok) { + console.log('后端请求失败', url, `status=${response.status}`, json) + + return { + success: false, + status: response.status, + message: json.message || json.error_description || '请求失败', // 业务错误(message)或者 oauth 错误(error_description) + } + } + return { + success: true, + data: json, + } + } + + throw new Error(`无法解析响应数据,未处理的 Content-Type: ${type}`) } catch (e) { console.error('后端请求失败', url, (e as Error).message) throw new Error(`请求失败,网络错误`) } - - const type = response.headers.get('Content-Type') ?? 'text/plain' - if (type.indexOf('text/plain') !== -1) { - const text = await response.text() - if (!response.ok) { - console.log('后端请求失败', url, `status=${response.status}`, text) - return { - success: false, - status: response.status, - message: text || '请求失败', - } - } - - if (!!text?.trim()?.length) { - console.log('未处理的响应成功', `type=text`, `text=${text}`) - } - return { - success: true, - data: undefined as R, // 强转类型,考虑优化 - } - } - else if (type.indexOf('application/json') !== -1) { - const json = await response.json() - if (!response.ok) { - console.log('后端请求失败', url, `status=${response.status}`, json) - - return { - success: false, - status: response.status, - message: json.message || json.error_description || '请求失败', // 业务错误(message)或者 oauth 错误(error_description) - } - } - return { - success: true, - data: json, - } - } - - throw new Error(`无法解析响应数据,未处理的 Content-Type: ${type}`) } // 导出