const GET = 'GET';
|
const POST = 'POST';
|
const PUT = 'PUT';
|
const FORM = 'FORM';
|
const DELETE = 'DELETE';
|
import config from '@/config/index.js'
|
const baseURL = config.BASE_URL;
|
|
const waitingList = [] //等待队列
|
const excutingList = [] //执行队列
|
let showLogoutModal = true //是否显示冻结弹窗
|
|
function request(method, url, data, type, lodingFlag) {
|
if (lodingFlag) {
|
uni.showLoading({
|
mask: true,
|
})
|
}
|
|
return new Promise(function(resolve, reject) {
|
|
let token = uni.getStorageSync('token') ||
|
'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjE4OTI0ODk0OTc5Nzk5NDA4NjUsInR5cGUiOjEsImV4cCI6MTc0MTY3MTI5OSwiY3JlYXRlZCI6MTc0MDM3NTI5OTQwNn0.39bIG5k-yClEWnUXvA63rjS631pj6fykU-J6vrC5u3HczL8NSwT_3pdVkj3hr4qikCrE7i9hmNkU_aLch1sw8A'
|
let header = {
|
'content-type': type ? 'application/x-www-form-urlencoded;charset=UTF-8' : 'application/json',
|
'Authorization': 'Bearer ' + token,
|
'lang': uni.getStorageSync('locale') === 'zh-Hans' ? 'zh_CN' : 'zh-tw',
|
}
|
const requestTask = uni.request({
|
url: baseURL + url,
|
method: method,
|
data: data,
|
header: header,
|
success: (res) => {
|
//判断状态码
|
if (lodingFlag) {
|
uni.hideLoading()
|
}
|
if (res.data.code == 200) {
|
resolve(res.data);
|
return
|
}
|
if (res.data.code == 502) {
|
resolve(res.data);
|
uni.showToast({
|
title: res.data.msg,
|
duration: 2000,
|
icon: 'none',
|
mask: true,
|
})
|
return
|
}
|
if (res.data.code == 500 && url.includes('/applet/user/check')) {
|
resolve(res.data);
|
|
return
|
}
|
if (res.data.code == 1 || res.data.code == 500) {
|
uni.showToast({
|
title: res.data.msg || res.data.data || '服务器错误',
|
duration: 2000,
|
icon: 'none',
|
mask: true,
|
})
|
return
|
}
|
if (res.data.code == 401 || res.data.code == 510 || res.data.code == 504 || res.data
|
.code == 505) {
|
uni.removeStorageSync('token')
|
if (res.data.code == 504) {
|
uni.showToast({
|
title: res.data.msg,
|
duration: 2000,
|
icon: 'none',
|
mask: true,
|
})
|
} else if (res.data.code == 505) {
|
handleLogout('当前登录账号在其他设备登录')
|
} else {
|
handleLogout('登录失效,请重新登录')
|
}
|
return
|
}
|
|
if (res.data.code == 501) {
|
uni.showToast({
|
title: res.data.msg,
|
duration: 2000,
|
icon: 'none',
|
mask: true,
|
})
|
return
|
}
|
if (res.data.code == 506) {
|
resolve(res.data);
|
return
|
}
|
uni.showToast({
|
title: res.data.msg || '服务器错误',
|
duration: 2000,
|
icon: 'none',
|
mask: true,
|
})
|
|
},
|
fail(err) {
|
if (lodingFlag) {
|
uni.hideLoading()
|
}
|
reject(err)
|
},
|
complete: () => {
|
const excutingIndex = excutingList.findIndex(item => item === requestTask)
|
excutingList.splice(excutingIndex, 1)
|
const [apiFn] = waitingList
|
if (excutingList.length === 0 && typeof apiFn === 'function') {
|
apiFn()
|
waitingList.splice(0, 1)
|
}
|
},
|
})
|
excutingList.push(requestTask)
|
})
|
}
|
|
// 被冻结跳转到登录页并取消后续请求
|
function handleLogout(str) {
|
let routeStr = getCurrentPages()[getCurrentPages().length - 1].route
|
if (excutingList.length >= 1) {
|
showLogoutModal = true
|
}
|
if (showLogoutModal) {
|
showLogoutModal = false
|
uni.showModal({
|
title: '提示',
|
content: str,
|
showCancel: false,
|
success: function(res) {
|
if (res.confirm) {
|
// if (routeStr == 'pages/login/index') {
|
// uni.reLaunch({
|
// url: '/pages/login/index'
|
// })
|
// return
|
// }
|
// uni.reLaunch({
|
// url: '/pages/login/index'
|
// })
|
}
|
}
|
});
|
clearAllRequest()
|
}
|
}
|
|
// 清除请求队列
|
function clearAllRequest() {
|
if (excutingList.length > 0) {
|
for (var i = 0; i < excutingList.length; i++) {
|
const item = excutingList[i]
|
if (item && item.abort) {
|
item.abort()
|
}
|
}
|
excutingList.length = 0
|
waitingList.length = 0
|
}
|
}
|
|
export default {
|
get: (url, data, type = false, lodingFlag = true) => request(GET, url, data, type, lodingFlag),
|
post: (url, data, type = false, lodingFlag = true) => request(POST, url, data, type, lodingFlag),
|
put: (url, data, type = false, lodingFlag = true) => request(PUT, url, data, type, lodingFlag),
|
FORM: (url, data, type = false, lodingFlag = true) => request(FORM, url, data, type, lodingFlag),
|
delete: (url, data, type = false, lodingFlag = true) => request(DELETE, url, data, type, lodingFlag),
|
}
|