beelink/src/api/base.ts

38 lines
923 B
TypeScript
Raw Normal View History

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-10 02:19:38 +00:00
// 泛型接口
export interface Get {
<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-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
})
const get: Get = async function (url: string,data?: unknown) {
2020-10-10 02:19:38 +00:00
const res = await axios.get(url,{params:data});
return res.data;
}
const post: Get = async function (url: string,data?: unknown) {
2020-10-10 02:19:38 +00:00
const res = await axios.post(url,data)
return res.data;
2020-09-24 07:33:12 +00:00
}
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,
setToken
2020-09-24 07:33:12 +00:00
}