| | |
| | | if (res.data.code == 401 || res.data.code == 510 || res.data.code == 504 || res |
| | | .data |
| | | .code == 505) { |
| | | // uni.clearStorageSync() |
| | | // handleLogout('登录过期,请重新登录') |
| | | // uni.navigateTo({ |
| | | // url: '/pages/login/login' |
| | | // }) |
| | | // return |
| | | uni.clearStorageSync() |
| | | handleLogout('登录过期,请重新登录') |
| | | return |
| | | } |
| | | |
| | | if (res.data.code == 501) { |
| | |
| | | // 被冻结跳转到登录页并取消后续请求 |
| | | function handleLogout(str) { |
| | | let routeStr = getCurrentPages()[getCurrentPages().length - 1].route |
| | | // 登录成功后需要设置 showLogoutModal 为 1 |
| | | if (uni.getStorageSync('showLogoutModal') == 1) { |
| | | 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' |
| | | }) |
| | | // 无论 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() |
| | | } |
| | | } |
| | | }); |
| | | clearAllRequest() |
| | | } |
| | | |
| | | // 清除请求队列 |
| | |
| | | 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) |
| | | } |
| | | }) |
| | | }) |
| | | } |
| | | } |