48 lines
1.2 KiB
TypeScript
48 lines
1.2 KiB
TypeScript
import axios from '../config/axiosConfig'
|
|
|
|
import { AxiosRequestConfig, CustomSuccessData } from 'axios';
|
|
import { getValue } from '@/utils/common';
|
|
import { message } from 'ant-design-vue';
|
|
import router from '@/router';
|
|
|
|
// 泛型接口
|
|
export interface Get {
|
|
<T>(url: string, params?: unknown, config?: AxiosRequestConfig): Promise<CustomSuccessData<T>>;
|
|
}
|
|
|
|
axios.interceptors.response.use((response)=>{
|
|
console.log(response)
|
|
if(response.data.code == 1001){
|
|
router.push("/")
|
|
}
|
|
return response;
|
|
},(error)=>{
|
|
message.error(error.response.data.message)
|
|
return Promise.reject(error)
|
|
})
|
|
|
|
const get: Get = async function (url: string, data?: unknown) {
|
|
const res = await axios.get(url, {params:data});
|
|
return res.data;
|
|
}
|
|
|
|
const post: Get = async function (url: string, data?: unknown) {
|
|
const res = await axios.post(url, data)
|
|
return res.data;
|
|
}
|
|
|
|
const del: Get = async function (url: string, data?: unknown){
|
|
const res = await axios.delete(url, {params:data})
|
|
return res.data;
|
|
}
|
|
|
|
function setToken(){
|
|
axios.defaults.headers.common['Authorization'] = "Bearer " + getValue("token");
|
|
}
|
|
|
|
export {
|
|
get,
|
|
post,
|
|
del,
|
|
setToken
|
|
} |