在不单独拿出来封装的情况下全局使用$api

所有一下均可在man.js中写入;

const prePage = ()=>{
let pages = getCurrentPages();
let prePage = pages[pages.length - 2];
// #ifdef H5
return prePage;
// #endif
return prePage.$vm;
}

const msg = (title, duration=1500, mask=false, icon='none')=>{
//统一提示方便全局修改
if(Boolean(title) === false){
return;
}
uni.showToast({
title,
duration,
mask,
icon
});
}
const getReqXml = (obj)=>{

return new Promise(resolve=>{
// var baseUrl = 'http://xqt.imaj.top:8080';
var baseUrl = 'http://xiaoq.shgb.xyz:8080';
var accessToken = uni.getStorageSync("token");
if(obj.baseUrl){
baseUrl = obj.baseUrl;
}
uni.request({
url:baseUrl+obj.url,
method: obj.type?obj.type:'GET',
header:{
'content-type': 'application/x-www-form-urlencoded',
// "Access-Token":obj.reqJson.token,
token:accessToken
},
dataType:'json',
data:obj.reqJson,
changeOrigin: true, // 允许跨域
timeout:10000,
success:(res)=>{
if(res.data.code == 0){
resolve(res.data);
}else{
msg(res.data.message);
hideLoading();
}
},
fail:(e)=>{
console.log(e);
msg(e.errMsg);
hideLoading();
},
complete:()=>{
console.log("complete");
}
})
})
}

const getUserInfoXml = (obj)=>{
return new Promise(resolve=>{
// var baseUrl = 'http://xqt.imaj.top:8080';
var baseUrl = 'http://xiaoq.shgb.xyz:8080';
uni.request({
url:baseUrl+obj.url,
method: obj.type?obj.type:'GET',
dataType:'json',
data:obj.reqJson,
changeOrigin: true, // 允许跨域
timeout:10000,
success:(res)=>{
resolve(res);
},
fail:(e)=>{
console.log(e);
msg(e.errMsg);
hideLoading();
},
complete:()=>{
console.log("complete");
}
})
})
}

挂载在vue上:

Vue.prototype.$api = {prePage,msg,getReqXml,getUserInfoXml};

posted @ 2020-04-17 15:13  Robot666  阅读(403)  评论(3编辑  收藏  举报