2025-06-17 22:37:37 +08:00
|
|
|
|
import type { HookFetchPlugin } from 'hook-fetch';
|
|
|
|
|
|
import { ElMessage } from 'element-plus';
|
|
|
|
|
|
import hookFetch from 'hook-fetch';
|
|
|
|
|
|
import { sseTextDecoderPlugin } from 'hook-fetch/plugins';
|
|
|
|
|
|
import router from '@/routers';
|
|
|
|
|
|
import { useUserStore } from '@/stores';
|
|
|
|
|
|
|
|
|
|
|
|
interface BaseResponse {
|
|
|
|
|
|
code: number;
|
|
|
|
|
|
data: never;
|
|
|
|
|
|
msg: string;
|
|
|
|
|
|
rows: never;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-19 23:45:22 +08:00
|
|
|
|
// 修改 BaseResponse 类型,使其兼容裸数组
|
|
|
|
|
|
type BaseResponse<T = any> =
|
|
|
|
|
|
| { code: number; data: T; msg: string } // 标准格式
|
|
|
|
|
|
| T[]; // 裸数组格式
|
|
|
|
|
|
|
2025-06-17 22:37:37 +08:00
|
|
|
|
export const request = hookFetch.create<BaseResponse, 'data' | 'rows'>({
|
2025-06-19 23:45:22 +08:00
|
|
|
|
// baseURL: import.meta.env.VITE_API_URL,
|
|
|
|
|
|
baseURL: '', // 留空或使用'/'
|
2025-06-17 22:37:37 +08:00
|
|
|
|
headers: {
|
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
|
},
|
2025-06-19 23:45:22 +08:00
|
|
|
|
plugins: [
|
|
|
|
|
|
sseTextDecoderPlugin({ json: true, prefix: 'data:' }),
|
|
|
|
|
|
{
|
|
|
|
|
|
name: 'adapt-array-response',
|
|
|
|
|
|
afterResponse: async (response) => {
|
|
|
|
|
|
// 如果是数组格式,手动包裹成 { data: [...] }
|
|
|
|
|
|
if (Array.isArray(response.result)) {
|
|
|
|
|
|
return {
|
|
|
|
|
|
...response,
|
|
|
|
|
|
result: {
|
|
|
|
|
|
code: 200,
|
|
|
|
|
|
msg: 'success',
|
|
|
|
|
|
data: response.result,
|
|
|
|
|
|
},
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
return response;
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
],
|
2025-06-17 22:37:37 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
function jwtPlugin(): HookFetchPlugin<BaseResponse> {
|
|
|
|
|
|
const userStore = useUserStore();
|
|
|
|
|
|
return {
|
|
|
|
|
|
name: 'jwt',
|
|
|
|
|
|
beforeRequest: async (config) => {
|
|
|
|
|
|
config.headers = new Headers(config.headers);
|
|
|
|
|
|
config.headers.set('authorization', `Bearer ${userStore.token}`);
|
|
|
|
|
|
return config;
|
|
|
|
|
|
},
|
|
|
|
|
|
afterResponse: async (response) => {
|
|
|
|
|
|
// console.log(response);
|
|
|
|
|
|
if (response.result?.code === 200) {
|
|
|
|
|
|
return response;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 处理403逻辑
|
|
|
|
|
|
if (response.result?.code === 403) {
|
|
|
|
|
|
// 跳转到403页面(确保路由已配置)
|
|
|
|
|
|
router.replace({
|
|
|
|
|
|
name: '403',
|
|
|
|
|
|
});
|
|
|
|
|
|
ElMessage.error(response.result?.msg);
|
|
|
|
|
|
return Promise.reject(response);
|
|
|
|
|
|
}
|
|
|
|
|
|
// 处理401逻辑
|
|
|
|
|
|
if (response.result?.code === 401) {
|
|
|
|
|
|
// 如果没有权限,退出,且弹框提示登录
|
|
|
|
|
|
userStore.logout();
|
|
|
|
|
|
userStore.openLoginDialog();
|
|
|
|
|
|
}
|
|
|
|
|
|
ElMessage.error(response.result?.msg);
|
|
|
|
|
|
return Promise.reject(response);
|
|
|
|
|
|
},
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
request.use(jwtPlugin());
|
|
|
|
|
|
|
|
|
|
|
|
export const post = request.post;
|
|
|
|
|
|
|
|
|
|
|
|
export const get = request.get;
|
|
|
|
|
|
|
|
|
|
|
|
export const put = request.put;
|
|
|
|
|
|
|
|
|
|
|
|
export const del = request.delete;
|
|
|
|
|
|
|
|
|
|
|
|
export default request;
|