feat: ai-hub与bbs单点登录联通
This commit is contained in:
@@ -4,20 +4,21 @@ import hookFetch from 'hook-fetch';
|
||||
import { sseTextDecoderPlugin } from 'hook-fetch/plugins';
|
||||
import router from '@/routers';
|
||||
import { useUserStore } from '@/stores';
|
||||
|
||||
interface BaseResponse {
|
||||
// 标准响应格式
|
||||
// 标准响应格式
|
||||
interface BaseResponse<T = any> {
|
||||
code: number;
|
||||
data: never;
|
||||
data: T;
|
||||
msg: string;
|
||||
rows: never;
|
||||
}
|
||||
|
||||
// 修改 BaseResponse 类型,使其兼容裸数组
|
||||
type BaseResponse<T = any> =
|
||||
| { code: number; data: T; msg: string } // 标准格式
|
||||
| T[]; // 裸数组格式
|
||||
|
||||
export const request = hookFetch.create<BaseResponse, 'data' | 'rows'>({
|
||||
// 扩展请求函数类型声明
|
||||
declare module 'hook-fetch' {
|
||||
interface HookFetchDefaults {
|
||||
// 允许响应是裸数据(自动会被插件包装)
|
||||
response: any;
|
||||
}
|
||||
}
|
||||
export const request = hookFetch.create<BaseResponse>({
|
||||
// baseURL: import.meta.env.VITE_API_URL,
|
||||
baseURL: '', // 留空或使用'/'
|
||||
headers: {
|
||||
@@ -28,18 +29,20 @@ export const request = hookFetch.create<BaseResponse, 'data' | 'rows'>({
|
||||
{
|
||||
name: 'adapt-array-response',
|
||||
afterResponse: async (response) => {
|
||||
// 如果是数组格式,手动包裹成 { data: [...] }
|
||||
if (Array.isArray(response.result)) {
|
||||
return {
|
||||
...response,
|
||||
result: {
|
||||
code: 200,
|
||||
msg: 'success',
|
||||
data: response.result,
|
||||
},
|
||||
};
|
||||
// 已经是标准格式(包含 code 字段)
|
||||
if (typeof response.result?.code === 'number') {
|
||||
return response;
|
||||
}
|
||||
return response;
|
||||
|
||||
// 非标准格式 → 包装为标准格式
|
||||
return {
|
||||
...response,
|
||||
result: {
|
||||
code: 200, // 默认成功码
|
||||
data: response.result, // 原始数据放入 data
|
||||
msg: 'success', // 默认消息
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user