const GET = 'GET';
|
const POST = 'POST';
|
const PUT = 'PUT';
|
const FORM = 'FORM';
|
const DELETE = 'DELETE';
|
import config from '@/config/index.js'
|
import { clearJPushAliasBinding } from '@/utils/push.js'
|
const baseURL = config.BASE_URL;
|
|
const waitingList = [] //等待队列
|
const excutingList = [] //执行队列
|
let appAuthRedirecting = false
|
|
/**
|
* 判断当前响应码是否属于成功态。
|
*/
|
function isSuccessCode(code) {
|
return code === 0 || code === 200
|
}
|
|
/**
|
* 从响应体中提取可展示的错误文案。
|
*/
|
function resolveMessage(payload) {
|
if (!payload) {
|
return '服务器错误'
|
}
|
if (typeof payload.data === 'string' && payload.data) {
|
return payload.data
|
}
|
return payload.msg || payload.message || '服务器错误'
|
}
|
|
/**
|
* 统一发起请求,并兼容当前仓库 `code=0` 的后端成功协议。
|
*/
|
function request(method, url, data, type, lodingFlag = true) {
|
if (lodingFlag) {
|
uni.showLoading({
|
mask: true,
|
})
|
}
|
return new Promise(function(resolve, reject) {
|
let token = uni.getStorageSync('token')
|
let header = {
|
'content-type': type ? 'application/x-www-form-urlencoded;charset=UTF-8' : 'application/json',
|
'Authorization': 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()
|
}
|
const payload = res.data || {}
|
if (isSuccessCode(payload.code)) {
|
resolve(payload);
|
return
|
}
|
if (payload.code == 502) {
|
resolve(payload);
|
uni.showToast({
|
title: resolveMessage(payload),
|
duration: 2000,
|
icon: 'none',
|
mask: true,
|
})
|
return
|
}
|
if (payload.code == 1 || payload.code == 500) {
|
uni.showToast({
|
title: resolveMessage(payload),
|
duration: 2000,
|
icon: 'none',
|
mask: true,
|
})
|
resolve(payload)
|
return
|
}
|
if (payload.code == 401 || payload.code == 510 || payload.code == 504 || payload.code == 505) {
|
handleUnauthorized(payload)
|
resolve(payload)
|
return
|
}
|
|
if (payload.code == 501) {
|
uni.showToast({
|
title: resolveMessage(payload),
|
duration: 2000,
|
icon: 'none',
|
mask: true,
|
})
|
resolve(payload)
|
return
|
}
|
if (payload.code == 506) {
|
resolve(payload);
|
return
|
}
|
|
uni.showToast({
|
title: resolveMessage(payload),
|
duration: 2000,
|
icon: 'none',
|
mask: true,
|
})
|
resolve(payload)
|
},
|
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)
|
})
|
|
|
}
|
|
/**
|
* 收到未登录响应时清理 APP 登录态并跳转登录页。
|
*/
|
function handleUnauthorized(payload) {
|
const routeStr = getCurrentRoute()
|
clearLoginSession()
|
clearAllRequest()
|
if (routeStr === 'pages/login/login' || appAuthRedirecting) {
|
return
|
}
|
appAuthRedirecting = true
|
uni.showToast({
|
title: resolveMessage(payload) || '请先登录',
|
duration: 1500,
|
icon: 'none',
|
mask: true,
|
})
|
setTimeout(() => {
|
uni.reLaunch({
|
url: '/pages/login/login'
|
})
|
setTimeout(() => {
|
appAuthRedirecting = false
|
}, 300)
|
}, 150)
|
}
|
|
/**
|
* 清除 APP 登录态缓存。
|
*/
|
function clearLoginSession() {
|
uni.removeStorageSync('token')
|
uni.removeStorageSync('appUserInfo')
|
uni.removeStorageSync('loginRedirectMeta')
|
uni.setStorageSync('showLogoutModal', 0)
|
clearJPushAliasBinding()
|
}
|
|
/**
|
* 获取当前页面路由,便于避免在登录页重复跳转。
|
*/
|
function getCurrentRoute() {
|
const currentPages = getCurrentPages()
|
if (!currentPages || currentPages.length === 0) {
|
return ''
|
}
|
return currentPages[currentPages.length - 1].route || ''
|
}
|
|
/**
|
* 清除请求队列。
|
*/
|
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
|
}
|
}
|
|
/**
|
* 发起 GET 请求。
|
*/
|
function get(url, data, type = false, lodingFlag = true) {
|
return request(GET, url, data, type, lodingFlag)
|
}
|
|
/**
|
* 发起 POST 请求。
|
*/
|
function post(url, data, type = false, lodingFlag = true) {
|
return request(POST, url, data, type, lodingFlag)
|
}
|
|
/**
|
* 发起 PUT 请求。
|
*/
|
function put(url, data, type = false, lodingFlag = true) {
|
return request(PUT, url, data, type, lodingFlag)
|
}
|
|
/**
|
* 发起表单编码请求。
|
*/
|
function form(url, data, type = false, lodingFlag = true) {
|
return request(FORM, url, data, type, lodingFlag)
|
}
|
|
/**
|
* 发起 DELETE 请求。
|
*/
|
function remove(url, data, type = false, lodingFlag = true) {
|
return request(DELETE, url, data, type, lodingFlag)
|
}
|
|
export default {
|
get,
|
post,
|
put,
|
FORM: form,
|
delete: remove,
|
}
|