引用:
import Uniapp from '@/common/request.js'
request.js文件
import rootPath from './config.js'
function getParam(data){
var param = "";
for (var k in data) {
let value = data[k] !== undefined ? data[k] : ''
param += '&' + k + '=' + encodeURIComponent(value)
}
return param.substring(1)
}
class Uniapp {
static go(name,data){
if(data){
var url = "/pages/" + name + "/" + name+"?"+getParam(data);
console.log(url);
uni.navigateTo({ url: url })
}else{
uni.navigateTo({ url: "/pages/" + name + "/" + name })
}
}
static showMsg(msg){
uni.showToast({ title: msg,icon:"none"})
}
//有遮罩层,防止穿透,mask
static showLoadingMask(){
uni.showLoading({ title: '加载中',mask:true});
}
static showLoading(){
uni.showLoading({ title: '加载中'});
}
static hideLoading(){
uni.hideLoading();
}
static post(api, params) {
return new Promise((resolve, reject) => {
let opts = {
url: rootPath+api,
data: Object.assign({}, params),
method: 'POST',
header: { 'content-type': 'application/x-www-form-urlencoded', 'Accept': 'application/json'},
success(res){
if(res.statusCode=="404"){
uni.showToast({ title: "请求接口失败,请重新尝试", icon:"none"})
}else if(res.statusCode=="200"){
resolve(res);
}else{
}
},
fail(res){
uni.showToast({ title: "异常",icon:"none"})
reject(res)
}
}
uni.request(opts);
});
};
static post2(api, params) {
let token = uni.getStorageSync('token');
return new Promise((resolve, reject) => {
let opts = {
url: rootPath+api,
data: Object.assign({}, params),
method: 'POST',
header: { 'content-type': 'application/x-www-form-urlencoded', 'Accept': 'application/json','yg-token':token },
success: resolve,
fail: reject
}
wx.request(opts);
});
};
static get(api, params) {
return new Promise((resolve, reject) => {
let opts = {
url: rootPath+api,
data: Object.assign({}, params),
method: 'GET',
header: { 'content-type': 'application/x-www-form-urlencoded', 'Accept': 'application/json' },
success: resolve,
fail: reject
}
wx.request(opts);
});
};
};
export default Uniapp
网友评论