2024-08-07 12:00:54 +00:00
|
|
|
import { useRouter } from 'vue-router';
|
2024-08-16 13:31:01 +00:00
|
|
|
|
2024-08-07 12:00:54 +00:00
|
|
|
import Request from '@/service/request/index.js'
|
2024-08-09 05:40:02 +00:00
|
|
|
import {message} from "@/utils/message.js";
|
2024-08-07 12:00:54 +00:00
|
|
|
const request = new Request({
|
|
|
|
baseURL: import.meta.env.VITE_BASEURL,
|
|
|
|
timeout: 1000 * 60 * 5,
|
|
|
|
interceptors: {
|
|
|
|
//实例的请求拦截器
|
|
|
|
requestInterceptors: (config) => {
|
|
|
|
const token=localStorage.getItem('token')
|
2024-08-16 13:31:01 +00:00
|
|
|
|
2024-08-07 12:00:54 +00:00
|
|
|
config.headers['Content-Type'] = config.method === 'get' ?
|
|
|
|
'application/x-www-form-urlencoded' :
|
2024-08-09 02:04:22 +00:00
|
|
|
'application/json'
|
2024-08-07 12:00:54 +00:00
|
|
|
if (config.isFormData) {
|
|
|
|
config.headers['Content-Type'] = 'multipart/form-data';
|
|
|
|
config.headers['Authorization'] = token
|
2024-08-16 13:59:17 +00:00
|
|
|
config.headers['tokenC'] ='1234567'
|
2024-08-07 12:00:54 +00:00
|
|
|
} else {
|
|
|
|
config.headers['Authorization'] = token
|
2024-08-16 13:59:17 +00:00
|
|
|
console.log('JSON.parse(localStorage.getItem(\'voteToken\'))?.authorization',JSON.parse(localStorage.getItem('voteToken'))?.authorization)
|
|
|
|
config.headers['tokenC'] =JSON.parse(localStorage.getItem('voteToken'))?.authorization
|
2024-08-07 12:00:54 +00:00
|
|
|
}
|
|
|
|
return config;
|
|
|
|
},
|
|
|
|
//实例的响应拦截器
|
|
|
|
responseInterceptors: async (response) => {
|
2024-08-16 09:03:01 +00:00
|
|
|
|
2024-08-09 05:40:02 +00:00
|
|
|
if (response.data.status===1){
|
2024-08-14 08:02:15 +00:00
|
|
|
message.warning(response.data.msg||response.data.err)
|
2024-08-09 05:40:02 +00:00
|
|
|
}
|
2024-08-07 12:00:54 +00:00
|
|
|
if ([200, 201, 204].includes(response.status)) {
|
|
|
|
return response.config.responseType === 'blob' ? response : response;
|
|
|
|
} else {
|
|
|
|
return Promise.reject(new Error(response.data.msg || 'An error occurred.'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const fontRequest = (config) => {
|
|
|
|
if (['get', 'GET'].includes(config.method)) {
|
|
|
|
config.params = config.data;
|
|
|
|
}
|
|
|
|
return request.request(config);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default fontRequest;
|