2026-01-04 11:09:06 +08:00
|
|
|
|
import define from './define'
|
2026-01-25 20:22:58 +08:00
|
|
|
|
import { useLocale } from '@/locale/useLocale';
|
2026-01-04 11:09:06 +08:00
|
|
|
|
|
2026-01-25 20:22:58 +08:00
|
|
|
|
const { getBackLocale } = useLocale();
|
2026-01-19 17:34:15 +08:00
|
|
|
|
let host = define.baseURL
|
2026-01-25 20:22:58 +08:00
|
|
|
|
const defaultOpt = { load: true }
|
2026-01-04 11:09:06 +08:00
|
|
|
|
|
2026-01-25 20:22:58 +08:00
|
|
|
|
// ------------- token刷新核心配置 -------------
|
|
|
|
|
|
const whiteList = ['/login', '/system/auth/refresh-token']
|
|
|
|
|
|
let requestQueue = []
|
|
|
|
|
|
let isRefreshingToken = false
|
|
|
|
|
|
const isRelogin = { show: false }
|
2026-01-04 11:09:06 +08:00
|
|
|
|
|
2026-01-25 20:22:58 +08:00
|
|
|
|
// ------------- 核心request方法 -------------
|
2026-01-04 11:09:06 +08:00
|
|
|
|
function request(config) {
|
2026-01-25 20:22:58 +08:00
|
|
|
|
config.options = Object.assign(defaultOpt, config.options)
|
|
|
|
|
|
const token = uni.getStorageSync('token') || ''
|
|
|
|
|
|
const refreshToken = uni.getStorageSync('refreshToken') || ''
|
|
|
|
|
|
const tenantId = '1' || uni.getStorageSync('tenantId')
|
|
|
|
|
|
const systemCode = uni.getStorageSync('systemCode') || ''
|
|
|
|
|
|
const locale = getBackLocale()
|
|
|
|
|
|
|
|
|
|
|
|
// 构建请求头
|
|
|
|
|
|
let header = {
|
|
|
|
|
|
"accept": 'application/json, text/plain, */*',
|
|
|
|
|
|
"App-Code": systemCode,
|
|
|
|
|
|
"Content-Type": "application/json;charset=UTF-8",
|
|
|
|
|
|
"Jnpf-Origin": "app",
|
|
|
|
|
|
"Vue-Version": "3",
|
|
|
|
|
|
"Accept-Language": locale,
|
|
|
|
|
|
"tenant-id": tenantId,
|
|
|
|
|
|
...config.header
|
|
|
|
|
|
}
|
|
|
|
|
|
header['App-Code'] = encodeURIComponent(header['App-Code'])
|
|
|
|
|
|
if (token) header['Authorization'] = token
|
|
|
|
|
|
// let url = config.url.indexOf('http') > -1 ? config.url : host + config.url
|
|
|
|
|
|
let url = host + config.url
|
|
|
|
|
|
console.log(url,'url---')
|
2026-01-30 11:48:35 +08:00
|
|
|
|
|
|
|
|
|
|
// 显示加载中
|
2026-01-25 20:22:58 +08:00
|
|
|
|
if (config.options.load) {
|
|
|
|
|
|
uni.showLoading({ title: config.options.loadText || '正在加载' })
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-30 11:48:35 +08:00
|
|
|
|
// 返回Promise,核心改造逻辑
|
2026-01-25 20:22:58 +08:00
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
// 封装核心请求逻辑
|
|
|
|
|
|
const coreRequest = () => {
|
|
|
|
|
|
uni.request({
|
|
|
|
|
|
url: url,
|
|
|
|
|
|
data: config.data || {},
|
|
|
|
|
|
method: config.method || 'GET',
|
|
|
|
|
|
header: header,
|
|
|
|
|
|
timeout: define.timeout,
|
|
|
|
|
|
success: async (res) => {
|
|
|
|
|
|
uni.hideLoading();
|
|
|
|
|
|
if (res.statusCode === 200) {
|
|
|
|
|
|
if (res.data.code == 200 || res.data.code == 0) {
|
|
|
|
|
|
resolve(res.data)
|
|
|
|
|
|
} else if ([401, 600, 601, 602].includes(res.data.code)) {
|
|
|
|
|
|
// 白名单接口不处理
|
|
|
|
|
|
const isWhiteList = whiteList.some(v => config.url.includes(v))
|
|
|
|
|
|
if (isWhiteList) {
|
2026-01-30 11:48:35 +08:00
|
|
|
|
// 仅提示错误,不触发登出
|
|
|
|
|
|
ajaxError(res.data, false)
|
2026-01-25 20:22:58 +08:00
|
|
|
|
reject(res.data.msg)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-30 11:48:35 +08:00
|
|
|
|
// ------------- 刷新token逻辑 -------------
|
2026-01-25 20:22:58 +08:00
|
|
|
|
if (!refreshToken) {
|
2026-01-30 11:48:35 +08:00
|
|
|
|
// 无刷新token,触发登出
|
2026-01-25 20:22:58 +08:00
|
|
|
|
await handleAuthorized()
|
|
|
|
|
|
reject(res.data.msg)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-01-04 11:09:06 +08:00
|
|
|
|
|
2026-01-25 20:22:58 +08:00
|
|
|
|
if (isRefreshingToken) {
|
|
|
|
|
|
requestQueue.push(() => coreRequest())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-01-04 11:09:06 +08:00
|
|
|
|
|
2026-01-25 20:22:58 +08:00
|
|
|
|
isRefreshingToken = true
|
|
|
|
|
|
try {
|
2026-01-27 15:42:20 +08:00
|
|
|
|
// 拼接URL参数
|
2026-01-25 20:22:58 +08:00
|
|
|
|
const refreshUrl = `${host}/admin-api/system/auth/refresh-token?refreshToken=${encodeURIComponent(refreshToken)}`
|
2026-01-30 11:48:35 +08:00
|
|
|
|
// 调用刷新token接口
|
2026-01-25 20:22:58 +08:00
|
|
|
|
const refreshRes = await uni.request({
|
2026-01-30 11:48:35 +08:00
|
|
|
|
url: refreshUrl,
|
|
|
|
|
|
method: 'POST',
|
2026-01-25 20:22:58 +08:00
|
|
|
|
header: {
|
2026-01-30 11:48:35 +08:00
|
|
|
|
'tenant-id': tenantId,
|
2026-01-25 20:22:58 +08:00
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
|
|
},
|
2026-01-30 11:48:35 +08:00
|
|
|
|
data: {}
|
2026-01-25 20:22:58 +08:00
|
|
|
|
})
|
2026-01-30 11:48:35 +08:00
|
|
|
|
|
2026-01-25 20:22:58 +08:00
|
|
|
|
// 刷新成功处理
|
2026-01-30 11:48:35 +08:00
|
|
|
|
if (refreshRes.data && (refreshRes.data.code == 0 || refreshRes.data.code == 200)) {
|
2026-01-25 20:22:58 +08:00
|
|
|
|
const newTokenData = refreshRes.data.data
|
2026-01-27 15:42:20 +08:00
|
|
|
|
// 存储新token
|
|
|
|
|
|
uni.setStorageSync('token', newTokenData.accessToken)
|
2026-01-25 20:22:58 +08:00
|
|
|
|
uni.setStorageSync('refreshToken', newTokenData.refreshToken)
|
|
|
|
|
|
// 更新请求头
|
2026-01-27 15:42:20 +08:00
|
|
|
|
header['Authorization'] = newTokenData.accessToken
|
2026-01-25 20:22:58 +08:00
|
|
|
|
// 重试当前请求
|
|
|
|
|
|
coreRequest()
|
|
|
|
|
|
// 执行队列请求
|
|
|
|
|
|
requestQueue.forEach(cb => cb())
|
|
|
|
|
|
requestQueue = []
|
|
|
|
|
|
} else {
|
2026-01-30 11:48:35 +08:00
|
|
|
|
// 刷新token失败,触发登出
|
2026-01-25 20:22:58 +08:00
|
|
|
|
await handleAuthorized()
|
|
|
|
|
|
reject('刷新token失败:' + (refreshRes.data?.msg || '接口返回异常'))
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (err) {
|
2026-01-30 11:48:35 +08:00
|
|
|
|
// 刷新token异常,触发登出
|
2026-01-25 20:22:58 +08:00
|
|
|
|
await handleAuthorized()
|
|
|
|
|
|
reject('刷新token异常:' + err.errMsg)
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
isRefreshingToken = false
|
|
|
|
|
|
requestQueue = []
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2026-01-30 11:48:35 +08:00
|
|
|
|
// 普通业务错误,仅提示,不登出
|
|
|
|
|
|
ajaxError(res.data, false)
|
2026-01-25 20:22:58 +08:00
|
|
|
|
reject(res.data.msg)
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2026-01-30 11:48:35 +08:00
|
|
|
|
// HTTP状态码错误,仅提示,不登出
|
|
|
|
|
|
ajaxError(res.data, false)
|
2026-01-25 20:22:58 +08:00
|
|
|
|
reject(res.errMsg)
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
fail: (err) => {
|
|
|
|
|
|
uni.hideLoading();
|
2026-01-30 11:48:35 +08:00
|
|
|
|
// 网络错误,仅提示,不登出
|
2026-01-25 20:22:58 +08:00
|
|
|
|
uni.showToast({ title: '连接服务器失败', icon: 'none' })
|
|
|
|
|
|
reject(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
coreRequest()
|
|
|
|
|
|
})
|
2026-01-04 11:09:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-25 20:22:58 +08:00
|
|
|
|
// ------------- 辅助函数 -------------
|
2026-01-30 11:48:35 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 错误提示函数
|
|
|
|
|
|
* @param {Object} data 错误数据
|
|
|
|
|
|
* @param {Boolean} isAuthError 是否是认证错误(是否需要触发登出)
|
|
|
|
|
|
*/
|
|
|
|
|
|
function ajaxError(data, isAuthError = false) {
|
2026-01-25 20:22:58 +08:00
|
|
|
|
uni.showToast({
|
|
|
|
|
|
title: data.msg || '请求出错,请重试',
|
|
|
|
|
|
icon: 'none',
|
2026-01-30 11:48:35 +08:00
|
|
|
|
duration: 2000, // 延长提示时间,提升体验
|
2026-01-25 20:22:58 +08:00
|
|
|
|
complete() {
|
2026-01-30 11:48:35 +08:00
|
|
|
|
// 只有明确的认证错误且不在刷新token过程中,才触发登出
|
|
|
|
|
|
if (isAuthError && [600, 601, 602, 401].includes(data.code) && !isRefreshingToken) {
|
2026-01-25 20:22:58 +08:00
|
|
|
|
setTimeout(() => handleAuthorized(), 1500)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-30 11:48:35 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 处理登录过期逻辑(仅token失效时调用)
|
|
|
|
|
|
*/
|
2026-01-25 20:22:58 +08:00
|
|
|
|
async function handleAuthorized() {
|
2026-01-30 11:48:35 +08:00
|
|
|
|
// 防止重复弹出登录弹窗
|
2026-01-25 20:22:58 +08:00
|
|
|
|
if (!isRelogin.show) {
|
|
|
|
|
|
isRelogin.show = true
|
2026-01-30 11:48:35 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const res = await uni.showModal({
|
|
|
|
|
|
title: '登录过期',
|
|
|
|
|
|
content: '您的登录已过期,请重新登录',
|
|
|
|
|
|
showCancelButton: false,
|
|
|
|
|
|
confirmText: '重新登录'
|
|
|
|
|
|
})
|
|
|
|
|
|
if (res.confirm) {
|
|
|
|
|
|
// 清除登录相关缓存
|
|
|
|
|
|
uni.removeStorageSync('token')
|
|
|
|
|
|
uni.removeStorageSync('refreshToken')
|
|
|
|
|
|
uni.removeStorageSync('cid')
|
|
|
|
|
|
uni.removeStorageSync('userInfo')
|
|
|
|
|
|
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
|
2026-01-25 20:22:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-01-04 11:09:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default request
|