You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
1.9 KiB
JavaScript

import host from "./host.js"
const header = {}
var flag = false
const request = (url = '', method = 'post', data = {}) => {
header['content-type'] = 'application/json;charset=UTF-8'
if (uni.getStorageSync('userInfo').token != undefined) {
header['token'] = uni.getStorageSync('userInfo').token
}
if (flag == false) {
uni.showLoading({
title: '加载中'
});
return new Promise((resolve, reject) => {
uni.request({
method,
url: host + url,
data: data,
withCredentials: true,
header: header,
dataType: 'json',
}).then((response) => {
uni.hideLoading();
if (response[1].data.code != undefined) {
if (response[1].data.code == '510002') {
flag = true
uni.showModal({
title: '提示',
content: '没有登录',
success: function(res) {
if (res.confirm) {
uni.reLaunch({
url: '/pages/login/login'
})
uni.removeStorageSync("userInfo")
}
}
})
setTimeout(function() {
flag = false
}, 10);
} else if (response[1].data.code == '502001') {
flag = true
uni.showModal({
title: '提示',
content: response[1].data.message,
showCancel: false,
success: function(res) {
console.log('flag',flag);
if (res.confirm) {
uni.reLaunch({
url: '/pages/login/login'
})
uni.removeStorageSync("userInfo")
}
},
})
setTimeout(function() {
flag = false
}, 500);
} else {
}
} else {
}
let [error, res] = response;
// 登录过期
resolve(res.data);
}).catch((error) => {
uni.hideLoading();
uni.showModal({
title: '提示',
content: '连接服务器失败',
success: function(res) {
if (res.confirm) {
uni.redirectTo({
url: '/pages/login/login'
})
}
}
})
});
});
}
}
export default request