feat: 修改接口报错登出问题
This commit is contained in:
@@ -36,12 +36,13 @@ function request(config) {
|
||||
// let url = config.url.indexOf('http') > -1 ? config.url : host + config.url
|
||||
let url = host + config.url
|
||||
console.log(url,'url---')
|
||||
// 3. 显示加载中
|
||||
|
||||
// 显示加载中
|
||||
if (config.options.load) {
|
||||
uni.showLoading({ title: config.options.loadText || '正在加载' })
|
||||
}
|
||||
|
||||
// 4. 返回Promise,核心改造逻辑
|
||||
// 返回Promise,核心改造逻辑
|
||||
return new Promise((resolve, reject) => {
|
||||
// 封装核心请求逻辑
|
||||
const coreRequest = () => {
|
||||
@@ -60,13 +61,15 @@ function request(config) {
|
||||
// 白名单接口不处理
|
||||
const isWhiteList = whiteList.some(v => config.url.includes(v))
|
||||
if (isWhiteList) {
|
||||
ajaxError(res.data)
|
||||
// 仅提示错误,不触发登出
|
||||
ajaxError(res.data, false)
|
||||
reject(res.data.msg)
|
||||
return
|
||||
}
|
||||
|
||||
// ------------- 刷新token请求改为问号拼接参数 -------------
|
||||
// ------------- 刷新token逻辑 -------------
|
||||
if (!refreshToken) {
|
||||
// 无刷新token,触发登出
|
||||
await handleAuthorized()
|
||||
reject(res.data.msg)
|
||||
return
|
||||
@@ -81,18 +84,19 @@ function request(config) {
|
||||
try {
|
||||
// 拼接URL参数
|
||||
const refreshUrl = `${host}/admin-api/system/auth/refresh-token?refreshToken=${encodeURIComponent(refreshToken)}`
|
||||
// 调用刷新token接口(POST方法 + URL参数,无请求体)
|
||||
// 调用刷新token接口
|
||||
const refreshRes = await uni.request({
|
||||
url: refreshUrl, // 带参数的URL
|
||||
method: 'POST', // 保持POST方法
|
||||
url: refreshUrl,
|
||||
method: 'POST',
|
||||
header: {
|
||||
'tenant-id': tenantId, // 携带租户ID
|
||||
'tenant-id': tenantId,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
data: {} // 无请求体,参数全在URL上
|
||||
data: {}
|
||||
})
|
||||
|
||||
// 刷新成功处理
|
||||
if (refreshRes.data.code == 0) {
|
||||
if (refreshRes.data && (refreshRes.data.code == 0 || refreshRes.data.code == 200)) {
|
||||
const newTokenData = refreshRes.data.data
|
||||
// 存储新token
|
||||
uni.setStorageSync('token', newTokenData.accessToken)
|
||||
@@ -105,10 +109,12 @@ function request(config) {
|
||||
requestQueue.forEach(cb => cb())
|
||||
requestQueue = []
|
||||
} else {
|
||||
// 刷新token失败,触发登出
|
||||
await handleAuthorized()
|
||||
reject('刷新token失败:' + (refreshRes.data?.msg || '接口返回异常'))
|
||||
}
|
||||
} catch (err) {
|
||||
// 刷新token异常,触发登出
|
||||
await handleAuthorized()
|
||||
reject('刷新token异常:' + err.errMsg)
|
||||
} finally {
|
||||
@@ -116,16 +122,19 @@ function request(config) {
|
||||
requestQueue = []
|
||||
}
|
||||
} else {
|
||||
ajaxError(res.data)
|
||||
// 普通业务错误,仅提示,不登出
|
||||
ajaxError(res.data, false)
|
||||
reject(res.data.msg)
|
||||
}
|
||||
} else {
|
||||
ajaxError(res.data)
|
||||
// HTTP状态码错误,仅提示,不登出
|
||||
ajaxError(res.data, false)
|
||||
reject(res.errMsg)
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
uni.hideLoading();
|
||||
// 网络错误,仅提示,不登出
|
||||
uni.showToast({ title: '连接服务器失败', icon: 'none' })
|
||||
reject(err)
|
||||
}
|
||||
@@ -137,21 +146,33 @@ function request(config) {
|
||||
}
|
||||
|
||||
// ------------- 辅助函数 -------------
|
||||
function ajaxError(data) {
|
||||
/**
|
||||
* 错误提示函数
|
||||
* @param {Object} data 错误数据
|
||||
* @param {Boolean} isAuthError 是否是认证错误(是否需要触发登出)
|
||||
*/
|
||||
function ajaxError(data, isAuthError = false) {
|
||||
uni.showToast({
|
||||
title: data.msg || '请求出错,请重试',
|
||||
icon: 'none',
|
||||
duration: 2000, // 延长提示时间,提升体验
|
||||
complete() {
|
||||
if ([600, 601, 602, 401].includes(data.code) && !isRefreshingToken) {
|
||||
// 只有明确的认证错误且不在刷新token过程中,才触发登出
|
||||
if (isAuthError && [600, 601, 602, 401].includes(data.code) && !isRefreshingToken) {
|
||||
setTimeout(() => handleAuthorized(), 1500)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理登录过期逻辑(仅token失效时调用)
|
||||
*/
|
||||
async function handleAuthorized() {
|
||||
// 防止重复弹出登录弹窗
|
||||
if (!isRelogin.show) {
|
||||
isRelogin.show = true
|
||||
try {
|
||||
const res = await uni.showModal({
|
||||
title: '登录过期',
|
||||
content: '您的登录已过期,请重新登录',
|
||||
@@ -159,7 +180,7 @@ async function handleAuthorized() {
|
||||
confirmText: '重新登录'
|
||||
})
|
||||
if (res.confirm) {
|
||||
// 清除缓存(建议用项目封装的removeToken方法)
|
||||
// 清除登录相关缓存
|
||||
uni.removeStorageSync('token')
|
||||
uni.removeStorageSync('refreshToken')
|
||||
uni.removeStorageSync('cid')
|
||||
@@ -167,10 +188,16 @@ async function handleAuthorized() {
|
||||
uni.removeStorageSync('permissionList')
|
||||
uni.removeStorageSync('sysVersion')
|
||||
uni.removeStorageSync('dynamicModelExtra')
|
||||
// 跳转到登录页
|
||||
uni.reLaunch({ url: '/pages/login/index' })
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('处理登录过期异常:', err)
|
||||
} finally {
|
||||
isRefreshingToken = false
|
||||
isRelogin.show = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default request
|
||||
Reference in New Issue
Block a user