// 全局请求封装
export default (url, method = 'get', params = {}) => {
const token = uni.getStorageSync('storage_key').token || ''; //获取token
const defaultUrl = 'https://lingzhuang.gengduoke.com'
uni.showLoading({
title: "加载中"
});
return new Promise((resolve, reject) => {
uni.request({
url: defaultUrl + url,
method: method,
header: {
'X-Token': token
},
data: {
...params
},
success(res) {
console.log('--', res)
if (res.data.msg === "登录信息已失效,请重新登录") {
uni.showModal({
title: '错误',
content: res.data.msg,
success: function(res) {
if (res.confirm) {
uni.removeStorageSync('storage_key');
uni.navigateTo({
url: '/pages/login/login'
});
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
}
resolve(res);
},
fail(err) {
reject(err);
},
complete() {
uni.hideLoading();
}
});
});
};