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 =
|
'Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjU2MDYyLCJ0eXBlIjoxLCJleHAiOjE3NDE3MDU5MTgsImNyZWF0ZWQiOjE3NDA0MDk5MTg3MDF9.k9ck3gWfVElSCl1uUI_5fZ3lEbFgtbQs_OaJku4I4bXoUO77kUnHsghkPBFdRknFHa49fTdAEdHn0wsb_XN5tg'
|
//党员 eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjE4OTI0ODk0OTc5Nzk5NDA4NjUsInR5cGUiOjEsImV4cCI6MTc0MDQ2NjQ3MCwiY3JlYXRlZCI6MTc0MDM4MDA3MDQzN30.oOlB8GBE50WmV4Kw-Uuy33rQpQ-homoJQ61QXs34rkQIr-jFlAn9dUOgebitI9bz5PUaFImSldhY6vokRjmYlg
|
// 社区 eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjE1OTAxNzYwNTgxNTY1NDgwOTgsInR5cGUiOjEsImV4cCI6MTc0MTcwNTE1OSwiY3JlYXRlZCI6MTc0MDQwOTE1OTEwMH0.AmeZFq2Pj2y2bRF1AolsRWHA4ehobBdx1-LtQHIJzub8WjEh_TxZ-PTEI7uiujlLWKhJE07PCVCggAdeuF3UIA
|
// 市 eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjE4OTI0MjE4MjgwODU4Mzc4MjYsInR5cGUiOjEsImV4cCI6MTc0MTY5ODg0OCwiY3JlYXRlZCI6MTc0MDQwMjg0ODMzN30.7IYRD37yTkhqcnKyXWcqKk_iTisMp3ar_ByfuVR7Go9rK8ZnGJrlwC3z4NF_ly7IIoBHgE2E4IAvfm3ccam8wg
|
//街道 eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjY2OTc5LCJ0eXBlIjoxLCJleHAiOjE3NDE3MDU4NDMsImNyZWF0ZWQiOjE3NDA0MDk4NDMyNTZ9.MOPT3X3Cw9AUmeuIpqDA1a1xZd7V85NSraJddxCrPHvOkaZ8dMI-aUB9gqninzqsMzwmMx6phbD8aBpTlYcWwA
|
// 县级 eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjU2MDYyLCJ0eXBlIjoxLCJleHAiOjE3NDE3MDU5MTgsImNyZWF0ZWQiOjE3NDA0MDk5MTg3MDF9.k9ck3gWfVElSCl1uUI_5fZ3lEbFgtbQs_OaJku4I4bXoUO77kUnHsghkPBFdRknFHa49fTdAEdHn0wsb_XN5tg
|
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),
|
}
|