2020-09-24 07:33:12 +00:00
|
|
|
import axios from '../config/axiosConfig'
|
|
|
|
|
2020-10-10 02:19:38 +00:00
|
|
|
import { AxiosRequestConfig, CustomSuccessData } from 'axios';
|
|
|
|
import { getValue } from '@/utils/common';
|
2020-10-12 02:50:58 +00:00
|
|
|
import { message } from 'ant-design-vue';
|
2020-10-14 08:21:40 +00:00
|
|
|
import router from '@/router';
|
2020-10-10 02:19:38 +00:00
|
|
|
|
|
|
|
// 泛型接口
|
|
|
|
export interface Get {
|
2020-10-13 01:30:06 +00:00
|
|
|
<T>(url: string, params?: unknown, config?: AxiosRequestConfig): Promise<CustomSuccessData<T>>;
|
2020-10-10 02:19:38 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 07:33:12 +00:00
|
|
|
axios.interceptors.response.use((response)=>{
|
2020-10-15 11:23:34 +00:00
|
|
|
// console.log(response)
|
2020-10-14 08:21:40 +00:00
|
|
|
if(response.data.code == 1001){
|
|
|
|
router.push("/")
|
|
|
|
}
|
2020-10-10 02:19:38 +00:00
|
|
|
return response;
|
2020-09-24 07:33:12 +00:00
|
|
|
},(error)=>{
|
2020-10-12 02:50:58 +00:00
|
|
|
message.error(error.response.data.message)
|
|
|
|
return Promise.reject(error)
|
2020-09-24 07:33:12 +00:00
|
|
|
})
|
|
|
|
|
2020-10-14 07:33:46 +00:00
|
|
|
const get: Get = async function (url: string, data?: unknown) {
|
|
|
|
const res = await axios.get(url, {params:data});
|
2020-10-10 02:19:38 +00:00
|
|
|
return res.data;
|
|
|
|
}
|
|
|
|
|
2020-10-14 07:33:46 +00:00
|
|
|
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})
|
2020-10-10 02:19:38 +00:00
|
|
|
return res.data;
|
2020-09-24 07:33:12 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 01:21:00 +00:00
|
|
|
const put: Get = async function (url: string, data?: unknown){
|
2020-10-15 06:51:34 +00:00
|
|
|
const res = await axios.put(url, data)
|
2020-10-15 01:21:00 +00:00
|
|
|
return res.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-10 02:19:38 +00:00
|
|
|
function setToken(){
|
|
|
|
axios.defaults.headers.common['Authorization'] = "Bearer " + getValue("token");
|
2020-09-24 07:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
get,
|
2020-10-10 02:19:38 +00:00
|
|
|
post,
|
2020-10-14 07:33:46 +00:00
|
|
|
del,
|
2020-10-15 01:21:00 +00:00
|
|
|
put,
|
2020-10-10 02:19:38 +00:00
|
|
|
setToken
|
2020-09-24 07:33:12 +00:00
|
|
|
}
|