import axios from 'axios';
var myaxios = {};
var aass = {};
myaxios.install = function(Vue){
var axios_obj = axios.create({
baseURL: 'http://dadeapi.xxll.top:9501',
})
function onGetCall(config) {
return config.method === 'get';
}
axios_obj.defaults.transformRequest = [function (data) {
let src = ''
for (let item in data) {
src += encodeURIComponent(item) + '=' + encodeURIComponent(data[item]) + '&'
}
return src
}]
//发送请求前拦载
axios_obj.interceptors.request.use(
config => {
var urlsa = config.url;
var t = Date.parse(new Date()) / 1000;//时间戳
var user = window.localStorage.getItem('user');
var token = window.localStorage.getItem('token');
if(urlsa.indexOf("?") != -1 ){
config.url = config.url + "&datest="+t+"&login_name="+user+"&token="+token
}else{
config.url = config.url + "?datest="+t+"&login_name="+user+"&token="+token
}
return config;
},
error => {
return Promise.reject(err);
}
);
//发送请求返回拦载
axios_obj.interceptors.response.use(function (response) {
// 对响应数据做点什么
var data = response.data;
if(data.code==206){
window.location.href = "/#/login";
}
return response;
},
function (error) {
// 对响应错误做点什么
//console.log(cwul);
return false;
}
);
Vue.prototype.$myhttp = axios_obj;
}
export default myaxios;
使用
this.$myhttp.post('Admin/Login/postLogin',this.data,{
'Content-Type': "application/x-www-form-urlencoded"
}).then(backdata=>{ // 异步执行成功后
this.$Message.success('保存成功');
})
this.$myhttp.get('Admin/Login/token',{
'Content-Type': 'application/json'
}).then(backdata=>{
let data = backdata.data;
this.data.token = data.data.token;
});
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)