vue封装ajax请求
// 用来发请求的库
import axios from 'axios';
import { Toast } from 'vant';
import {
Loading
} from 'vant';
// axios降级请求,处理跨域一个请求发两次的问题
//axios.interceptors.request.use(function (config) {
// config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
// if (config.method === 'post') {
// config.data = qs.stringify({ ...config.data })
// }
//},
//function (error) {
// loadinginstace.close();
// return Promise.reject(error);
//})
export const test = () => {
console.log('test');
}
/**
* url
* data 请求参数
* type 请求类型
*/
export const doajax = (url, data, type) => {
let config;
if (type === 'get') {
config = {
method: type,
params: data,
}
} else {
config = {
method: type,
data,
}
}
console.log('config',config);
return axios(url, config);
}
export const ajax = (url, data, suc) => {
doajax (url, data, 'get').then((res) => {
let {
status,
msg
} = res.data;
if (status === 0) {
suc(res.data.data);
} else {
alert(msg);
}
});
}
export const post = (url, data, suc) => {
doajax (url, data, 'post').then((res) => {
let {
status,
msg
} = res.data;
if (status === 0) {
suc(res.data.data);
} else {
alert(msg);
}
});
}
export const loadingBegin=()=>{
Toast.loading({
mask:true,//是否显示蒙层
duration:0,//持续展示toast
forbidClick:true,//禁用背景点击
loadingType:"spinner",
message:"努力加载中..."
});
}
export const loadingFinish=()=>{
Toast.clear();
}