beelink/src/api/base.ts
2020-10-15 19:23:34 +08:00

55 lines
1.3 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;
}
const put: Get = async function (url: string, data?: unknown){
const res = await axios.put(url, data)
return res.data;
}
function setToken(){
axios.defaults.headers.common['Authorization'] = "Bearer " + getValue("token");
}
export {
get,
post,
del,
put,
setToken
}