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 = [] //执行队列 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() } if (res.data.code == 200 || res.data.code == 0) { resolve(res.data); uni.hideLoading() return } if (res.data.code == 502) { resolve(res.data); uni.showToast({ title: res.data.msg, duration: 2000, icon: 'none', mask: true, }) uni.hideLoading() return } if (res.data.code == 1 || res.data.code == 500) { uni.showToast({ title: res.data.data || res.data.msg, duration: 2000, icon: 'none', mask: true, }) uni.hideLoading() return } if (res.data.code == 401 || res.data.code == 510 || res.data.code == 504 || res .data .code == 505) { uni.clearStorageSync() handleLogout('登录过期,请重新登录') return } if (res.data.code == 501) { uni.showToast({ title: res.data.msg, duration: 2000, icon: 'none', mask: true, }) uni.hideLoading() return } if (res.data.code == 506) { resolve(res.data); uni.hideLoading() return } uni.showToast({ title: res.data.msg || '服务器错误', duration: 2000, icon: 'none', mask: true, }) uni.hideLoading() }, 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 // 无论 showLogoutModal 是多少,遇到 401 都要弹窗并强制退出 uni.setStorageSync('showLogoutModal', 0) uni.showModal({ title: '提示', content: str, showCancel: false, success: function(res) { if (res.confirm) { if (routeStr == 'pages/login/login') { return } uni.reLaunch({ url: '/pages/login/login' }) } } }); 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), upload: (url, filePath, name = 'file', formData = {}) => { uni.showLoading({ mask: true }) return new Promise((resolve, reject) => { let token = uni.getStorageSync('token') uni.uploadFile({ url: baseURL + url, filePath: filePath, name: name, formData: formData, header: { 'Authorization': token, 'lang': uni.getStorageSync('locale') === 'zh-Hans' ? 'zh_CN' : 'zh-tw', }, success: (res) => { uni.hideLoading() try { const data = JSON.parse(res.data) if (data.code == 200 || data.code == 0) { resolve(data) } else { uni.showToast({ title: data.msg || '上传失败', icon: 'none' }) reject(data) } } catch (e) { reject(e) } }, fail: (err) => { uni.hideLoading() reject(err) } }) }) } }