2023-10-14 19:54:45 +08:00
|
|
|
|
|
|
|
const Url = 'https://chat.lihaink.cn'
|
|
|
|
|
|
|
|
function toLogin() {
|
|
|
|
uni.showToast({
|
|
|
|
title: '请先登录',
|
|
|
|
icon: 'none',
|
|
|
|
duration: 1000
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function baseRequestTwo(url, method, data, {
|
|
|
|
noAuth = false,
|
|
|
|
noVerify = false,
|
|
|
|
onReLogin = false
|
|
|
|
}) {
|
|
|
|
|
|
|
|
return new Promise((reslove, reject) => {
|
2023-11-18 11:43:30 +08:00
|
|
|
uni.showLoading({
|
|
|
|
title: '加载中'
|
|
|
|
})
|
2023-10-14 19:54:45 +08:00
|
|
|
uni.request({
|
|
|
|
// url: Url + '/api/v1' + url,
|
|
|
|
url: Url + url,
|
|
|
|
method: method || 'GET',
|
|
|
|
data: method != 'GET' ? data || {} : {},
|
|
|
|
params: method == 'GET' ? data : {},
|
|
|
|
success: (res) => {
|
2023-11-18 11:43:30 +08:00
|
|
|
uni.hideLoading()
|
2023-10-14 19:54:45 +08:00
|
|
|
if (noVerify)
|
|
|
|
reslove(res.data);
|
|
|
|
else if (res.data.code == -1) {
|
|
|
|
reject(res.data);
|
|
|
|
} else if (res.data.code == 0) {
|
|
|
|
reslove(res.data);
|
|
|
|
} else if (res.data.code == 1) {
|
|
|
|
reslove(res.data);
|
|
|
|
} else if (res.data.code == 200) {
|
|
|
|
reslove(res.data.data);
|
|
|
|
} else if ([410000, 410001, 410002, 40000].indexOf(res.data.code) !== -1) {
|
|
|
|
toLogin();
|
|
|
|
reject(res.data);
|
|
|
|
} else if (res.data.code == 501) {
|
|
|
|
reject(res.data);
|
|
|
|
} else {
|
|
|
|
uni.showToast({
|
|
|
|
title: res.data.msg || '请检查网络',
|
|
|
|
icon: 'none'
|
|
|
|
})
|
|
|
|
reject(res.data.msg || '请检查网络');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
fail: (message) => {
|
|
|
|
// uni.hideLoading()
|
2023-11-18 11:43:30 +08:00
|
|
|
uni.showModal({
|
|
|
|
title: '请求错误',
|
|
|
|
content: JSON.stringify(message)
|
2023-10-14 19:54:45 +08:00
|
|
|
})
|
|
|
|
reject('请求失败');
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const oahttp = {};
|
|
|
|
|
|
|
|
['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
|
|
|
|
oahttp[method] = (api, data, opt) => baseRequestTwo(api, method, data, opt || {})
|
|
|
|
});
|
|
|
|
|
|
|
|
export default oahttp;
|