//网络请求
networkRequest(param) {
let that = this;
const token = uni.getStorageSync('token');
const header = param.header || {
appId: appId,
'content-type': 'application/json'
}
if (token != '') {
header['token'] = token;
}
if (param.method) {
param.method = param.method.toUpperCase(); //小写改为大写
}
//网络请求
uni.request({
url: apiUrl + param.url,
method: param.method || "GET",
header: header,
data: param.data || {},
success: res => {
uni.hideLoading();
if (res.data.code == 401) {
uni.removeStorageSync('token');
//token失效
if (that.isGoLogin) {
// #ifdef MP-WEIXIN
uni.showLoading({
title: '重新登录...',
mask: true
});
that._mp_wx_login('/account/getwxphone', function(res) {
that.showToast("登录成功");
uni.setStorage({
key: 'token',
data: res.token
});
that.isGoLogin = true;
let page = getCurrentPages().pop();
if (!page) return;
page.onLoad(page.options); //如果页面存在,则重新刷新页面
page.onShow(); //如果页面存在,则重新刷新页面
});
// #endif
// #ifdef H5
uni.navigateTo({
url: "/pages/me/login/login?url=" + encodeURIComponent(window.location.href)
});
// #endif
}
that.isGoLogin = false;
return false;
}
if (res.data.code != 200) {
this.showToast(res.data.msg);
typeof param.error == "function" && param.error(res.data);
return false;
}
typeof param.success == "function" && param.success(res.data.data);
},
fail: (err) => {
uni.hideLoading();
this.showToast(err.errMsg);
},
complete: (res) => {
typeof param.complete == "function" && param.complete(res.data);
}
});
},