import axios from 'axios';
import type {
AxiosRequestConfig,
AxiosResponse,
} from 'axios';
import {
setToken,
getToken,
loginOut,
} from '@/core/auth';
import {
updataTokenAPI
} from '@/api/admin/auth'
import {
ElLoading,
} from 'element-plus'
import { ElMessage } from 'element-plus'
axios.defaults.timeout = 100000;
axios.defaults.headers.post['Content-Type'] = 'application/json';
axios.defaults.headers.get['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.baseURL = import.meta.env.VITE_APP_BASE_URL;
const instance = axios.create();
interface YXRequestConfig extends AxiosRequestConfig {
headers?: any
}
export interface RequestConfig {
timeout?: number;
then?: boolean;
hideLoading?: boolean,
request?: AxiosRequestConfig,
}
let timer: number;
let loadingInstance1: ReturnType<typeof ElLoading.service> | undefined;
function showLoading() {
clearTimeout(timer);
if (loadingInstance1) return;
loadingInstance1 = ElLoading.service({
fullscreen: true,
lock: true,
text: '加载中...',
background: 'rgba(0,0,0,.6)',
})
}
function hideLoading() {
clearTimeout(timer);
if (loadingInstance1) {
timer = window.setTimeout(() => {
loadingInstance1?.close();
loadingInstance1 = undefined;
}, 200)
}
}
instance.interceptors.request.use(function (config) {
let token = getToken();
if (token) {
config.headers['Authorization'] = token;
}
return config;
})
instance.interceptors.response.use(function (response: AxiosResponse) {
hideLoading();
if (response.headers.Authorization) {
setToken(response.headers.Authorization)
}
return response;
}, async (error) => {
hideLoading();
if (error.response && error.response.status === 401) {
let username = localStorage.getItem('loginUserName')
const { data: res } = await updataTokenAPI({
username
})
setToken(res);
error.config.headers.Authorization = res
return axios(error.config)
} else if (error.response.status === 500 && error.config.url === '/auth/user/refresh_token') {
ElMessage.error('身份已过期!');
loginOut();
}
return error
})
let REFRESH_COUNT = 0;
async function toRequest<T = any>(params: AxiosRequestConfig, config: RequestConfig = {}): Promise<ResponseResult<T>> {
if (!config.hideLoading) {
showLoading();
}
let request = config.request || {};
try {
let result = await instance({
...params,
...request,
timeout: config.timeout,
});
let data: ResponseResult<T> = result.data;
if (!(data instanceof Blob)) {
if (data.code === 40002) {
loginOut();
}
if (data.code === 10000) {
return Promise.reject();
}
if (data.code !== SUCCESSCODE && !config.then) {
ElMessage.error(data.message);
return Promise.reject(data);
}
}
return data;
} catch (e) {
return Promise.reject(e);
}
}
export function get<T = any>(url: string, data: object = {}, config?: RequestConfig): Promise<ResponseResult<T>> {
return toRequest(
{
url,
method: 'get',
params: data,
},
config,
)
}
export function post<T = any>(url: string, data: object = {}, config?: RequestConfig): Promise<ResponseResult<T>> {
return toRequest<T>(
{
url,
method: 'post',
data: data,
},
config,
)
}
export function downxlsx<T = any>(url: string, data: object = {}, config?: RequestConfig): Promise<ResponseResult<T>> {
return toRequest<T>(
{
url,
method: 'post',
data: data,
},
config,
)
}
export function upload<T = any>(url: string, data: any, config?: RequestConfig): Promise<ResponseResult<T>> {
return toRequest<T>(
{
url,
method: 'post',
data: data,
headers: {
'Content-Type': 'multipart/form-data',
},
},
config,
)
}
export const blobParams = {
request: {
responseType: 'blob',
}
} as const;