新版本官网优化完成
This commit is contained in:
190
utils/request.ts
190
utils/request.ts
@@ -1,88 +1,108 @@
|
||||
/**
|
||||
* axios 实例
|
||||
*/
|
||||
import axios from 'axios';
|
||||
import type { AxiosResponse } from 'axios';
|
||||
import {API_BASE_URL, TOKEN_HEADER_NAME, LAYOUT_PATH, TOKEN_STORE_NAME} from '@/config';
|
||||
import type { ApiResult } from '@/api';
|
||||
import { getHostname, getTenantId } from '@/utils/domain';
|
||||
import type { UseFetchOptions } from '#app';
|
||||
import {useToken} from "~/composables/configState";
|
||||
import {getTenantId} from "~/utils/domain";
|
||||
|
||||
const service = axios.create({
|
||||
baseURL: API_BASE_URL
|
||||
});
|
||||
export const request = <T>(url:string, options?: UseFetchOptions<T, unknown>) => {
|
||||
const nuxtApp = useNuxtApp()
|
||||
const config = useRuntimeConfig()
|
||||
const TenantId = localStorage.getItem('TenantId') || `${config.public.tenantId}`;
|
||||
return useFetch<T>(url,{
|
||||
baseURL: config.public.apiBaseUrl,
|
||||
onRequest({ options }: any){
|
||||
let token = ''
|
||||
if(import.meta.client){
|
||||
token = useToken().value
|
||||
}
|
||||
options.headers = {
|
||||
TenantId,
|
||||
'Authorization': `${token}`,
|
||||
...options.headers
|
||||
}
|
||||
},
|
||||
onResponse({ response }: any){
|
||||
// 200xx 以内错误
|
||||
if(response.status >= 200 && response.status < 300){
|
||||
if(response._data.code !== 0){
|
||||
console.log('>>>>200xx 以内错误')
|
||||
if(import.meta.client){
|
||||
ElMessage.error(response._data.message)
|
||||
nuxtApp.runWithContext(() => {
|
||||
// navigateTo({
|
||||
// path: '/404',
|
||||
// query: {
|
||||
// code: response.status,
|
||||
// message: response._data.message
|
||||
// }
|
||||
// })
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
onResponseError({ response }: any){
|
||||
// 200xx 以外错误
|
||||
if(import.meta.client){
|
||||
console.log('200xx 以外错误',response)
|
||||
ElMessage.error(isArray(response._data.data.message) ? response._data.data.message[0] : response._data.data.message);
|
||||
}
|
||||
},
|
||||
...options
|
||||
} as any)
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加请求拦截器
|
||||
*/
|
||||
service.interceptors.request.use(
|
||||
(config) => {
|
||||
const TENANT_ID = localStorage.getItem('TenantId') || 5;
|
||||
const token = localStorage.getItem(TOKEN_STORE_NAME);
|
||||
// 添加 token 到 header
|
||||
if (token && config.headers) {
|
||||
config.headers.common[TOKEN_HEADER_NAME] = token;
|
||||
}
|
||||
// 获取租户ID
|
||||
if (config.headers) {
|
||||
// 附加企业ID
|
||||
const companyId = localStorage.getItem('CompanyId');
|
||||
if (companyId) {
|
||||
config.headers.common['CompanyId'] = companyId;
|
||||
}
|
||||
// 通过网站域名获取租户ID
|
||||
if (getHostname()) {
|
||||
config.headers.common['Domain'] = getHostname();
|
||||
}
|
||||
// 解析二级域名获取租户ID
|
||||
if (getTenantId()) {
|
||||
config.headers.common['TenantId'] = getTenantId();
|
||||
return config;
|
||||
}
|
||||
if (TENANT_ID) {
|
||||
config.headers.common['TenantId'] = TENANT_ID;
|
||||
return config;
|
||||
}
|
||||
}
|
||||
return config;
|
||||
},
|
||||
(error) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
export const get = <T>(url: string,options?: UseFetchOptions<T, unknown>) => {
|
||||
return <T>new Promise((resolve,reject) => {
|
||||
request(url,{
|
||||
method: 'get',
|
||||
...options
|
||||
}).then(res => {
|
||||
resolve(res.data.value)
|
||||
}).catch(err => {
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
export const post = <T>(url:string,data?: any) => {
|
||||
return <T>new Promise((resolve,reject) => {
|
||||
request(url,{
|
||||
method: 'post',
|
||||
body: data
|
||||
}).then(res => {
|
||||
resolve(res.data.value)
|
||||
}).catch(err => {
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
export const put = <T>(url:string,data?: any) => {
|
||||
return <T>new Promise((resolve,reject) => {
|
||||
request(url,{
|
||||
method: 'put',
|
||||
body: data
|
||||
}).then(res => {
|
||||
resolve(res.data.value)
|
||||
}).catch(err => {
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
export const del = <T>(url:string,data?: any) => {
|
||||
return <T>new Promise((resolve,reject) => {
|
||||
request(url,{
|
||||
method: 'delete',
|
||||
...data
|
||||
}).then(res => {
|
||||
resolve(res.data.value)
|
||||
}).catch(err => {
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加响应拦截器
|
||||
*/
|
||||
service.interceptors.response.use(
|
||||
(res: AxiosResponse<ApiResult<unknown>>) => {
|
||||
// 登录过期处理
|
||||
if (res.data?.code === 401) {
|
||||
// const currentPath = unref(router.currentRoute).path;
|
||||
// if (currentPath == LAYOUT_PATH) {
|
||||
// logout(true);
|
||||
// } else {
|
||||
// Modal.destroyAll();
|
||||
// Modal.info({
|
||||
// title: '系统提示',
|
||||
// content: '登录状态已过期, 请退出重新登录!',
|
||||
// okText: '重新登录',
|
||||
// onOk: () => {
|
||||
// logout(false, currentPath);
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
// return Promise.reject(new Error(res.data.message));
|
||||
}
|
||||
// token 自动续期
|
||||
const token = res.headers[TOKEN_HEADER_NAME.toLowerCase()];
|
||||
if (token) {
|
||||
localStorage.setItem(TOKEN_STORE_NAME, token);
|
||||
}
|
||||
return res;
|
||||
},
|
||||
(error) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
export default service;
|
||||
export default {
|
||||
request,
|
||||
get,
|
||||
post,
|
||||
put,
|
||||
del
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user