|
|
@@ -1,21 +1,50 @@
|
|
|
-import { API_BASE, TOKEN_KEY } from '@/config/api'
|
|
|
+import { API_BASE, TOKEN_KEY, REFRESH_TOKEN_KEY, USER_INFO_KEY } from '@/config/api'
|
|
|
|
|
|
let clearingAuth = false
|
|
|
|
|
|
async function onHttpUnauthorized() {
|
|
|
if (clearingAuth) return
|
|
|
clearingAuth = true
|
|
|
+ // 先同步清理本地登录态,避免 store 动态导入失败时仍被识别为已登录
|
|
|
+ uni.removeStorageSync(TOKEN_KEY)
|
|
|
+ uni.removeStorageSync(REFRESH_TOKEN_KEY)
|
|
|
+ uni.removeStorageSync(USER_INFO_KEY)
|
|
|
try {
|
|
|
const { useUserStore } = await import('@/stores/user')
|
|
|
useUserStore().clearLoginInfo()
|
|
|
} catch (error) {
|
|
|
// ignore
|
|
|
}
|
|
|
+ const pages = typeof getCurrentPages === 'function' ? getCurrentPages() : []
|
|
|
+ const currentRoute = pages.length ? pages[pages.length - 1].route : ''
|
|
|
+ if (currentRoute === 'pages/login/login') {
|
|
|
+ clearingAuth = false
|
|
|
+ return
|
|
|
+ }
|
|
|
uni.showToast({ title: '登录已失效,请重新登录', icon: 'none' })
|
|
|
- uni.reLaunch({ url: '/pages/login/login' })
|
|
|
+ uni.reLaunch({
|
|
|
+ url: '/pages/login/login',
|
|
|
+ fail: () => {
|
|
|
+ // 某些端在请求回调中 reLaunch 可能失败,使用 redirectTo 兜底
|
|
|
+ uni.redirectTo({ url: '/pages/login/login' })
|
|
|
+ },
|
|
|
+ complete: () => {
|
|
|
+ setTimeout(() => {
|
|
|
+ clearingAuth = false
|
|
|
+ }, 800)
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+function isBizUnauthorized(data) {
|
|
|
+ if (!data || typeof data !== 'object') return false
|
|
|
+ return data.code === 401
|
|
|
}
|
|
|
|
|
|
export function request(options) {
|
|
|
+ if (clearingAuth) {
|
|
|
+ return Promise.reject(new Error('Unauthorized'))
|
|
|
+ }
|
|
|
const rawToken = uni.getStorageSync(TOKEN_KEY)
|
|
|
const token = typeof rawToken === 'string' ? rawToken.trim() : ''
|
|
|
const rawUrl = options.url || ''
|
|
|
@@ -33,7 +62,7 @@ export function request(options) {
|
|
|
...(options.header || {})
|
|
|
},
|
|
|
success: (res) => {
|
|
|
- if (res.statusCode === 401) {
|
|
|
+ if (res.statusCode === 401 || isBizUnauthorized(res.data)) {
|
|
|
onHttpUnauthorized()
|
|
|
reject(new Error('Unauthorized'))
|
|
|
return
|