修改了api集中化 搞定了vuex
This commit is contained in:
@@ -1,20 +1,35 @@
|
||||
import axios from '../config/axiosConfig'
|
||||
|
||||
import { AxiosRequestConfig, CustomSuccessData } from 'axios';
|
||||
import { getValue } from '@/utils/common';
|
||||
|
||||
// 泛型接口
|
||||
export interface Get {
|
||||
<T>(url: string, params?: object, config?: AxiosRequestConfig): Promise<CustomSuccessData<T>>;
|
||||
}
|
||||
|
||||
axios.interceptors.response.use((response)=>{
|
||||
return response.data;
|
||||
return response;
|
||||
},(error)=>{
|
||||
return error;
|
||||
})
|
||||
|
||||
function get(url: string,data?: object) {
|
||||
return axios.get(url,{params:data})
|
||||
const get: Get = async function (url: string,data?: object) {
|
||||
const res = await axios.get(url,{params:data});
|
||||
return res.data;
|
||||
}
|
||||
|
||||
function post(url: string,data?: object) {
|
||||
return axios.post(url,data)
|
||||
const post: Get = async function (url: string,data?: object) {
|
||||
const res = await axios.post(url,data)
|
||||
return res.data;
|
||||
}
|
||||
|
||||
function setToken(){
|
||||
axios.defaults.headers.common['Authorization'] = "Bearer " + getValue("token");
|
||||
}
|
||||
|
||||
export {
|
||||
get,
|
||||
post
|
||||
post,
|
||||
setToken
|
||||
}
|
||||
Reference in New Issue
Block a user