WMS_PDA/core/utils/auth-guard.js
2026-09-21 18:20:27 +08:00

171 lines
4.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { getToken, removeToken, isWorkbenchSession } from '@/core/utils/auth'
export const LOGIN_URL = '/pages/login/index'
export const HOME_URL = '/pages/index/index'
let redirecting = false
export function getCurrentRoute() {
const pages = getCurrentPages()
if (!pages || !pages.length) {
return ''
}
const current = pages[pages.length - 1]
return current?.route ? `/${current.route}` : ''
}
export function isLoginRoute(route = '') {
return route === LOGIN_URL
}
export function clearSession(store) {
removeToken()
if (!store) return
try {
store.commit('token', '')
} catch (e) {}
try {
store.commit('isLogin', false)
} catch (e) {}
try {
store.commit('userInfo', {})
} catch (e) {}
try {
store.commit('SET_PERMISSIONS', [])
} catch (e) {}
try {
store.commit('SET_USER', {})
} catch (e) {}
try {
store.commit('SET_ROLEID', 0)
} catch (e) {}
try {
store.commit('USER_DATA', {})
} catch (e) {}
try {
store.commit('AGENT_INFO', {})
} catch (e) {}
try {
store.commit('AUTH_TYPE', '')
} catch (e) {}
try {
store.commit('deviceInfo/CLEAR_DEVICE_INFO')
} catch (e) {}
}
function navigate(url, type) {
const currentRoute = getCurrentRoute()
if (redirecting || currentRoute === url) {
console.log('[auth-guard] 跳转已忽略:', {
url,
type,
redirecting,
currentRoute
})
return Promise.resolve()
}
redirecting = true
return new Promise(resolve => {
const done = () => {
setTimeout(() => {
redirecting = false
resolve()
}, 80)
}
if (type === 'switchTab') {
console.log('[auth-guard] 开始跳转:', { url, type })
uni.switchTab({
url,
success: () => {
console.log('[auth-guard] 跳转成功:', { url, type })
},
fail: error => {
console.log('[auth-guard] 跳转失败:', { url, type, error })
},
complete: done
}, true)
return
}
console.log('[auth-guard] 开始跳转:', { url, type })
uni.reLaunch({
url,
success: () => {
console.log('[auth-guard] 跳转成功:', { url, type })
},
fail: error => {
console.log('[auth-guard] 跳转失败:', { url, type, error })
},
complete: done
}, true)
})
}
export function goLogin() {
return navigate(LOGIN_URL, 'reLaunch')
}
export function goHome() {
return navigate(HOME_URL, 'switchTab')
}
export async function ensureEntry(store, forceValidate = false) {
const token = getToken()
const currentRoute = getCurrentRoute()
// 工作台会话:跳过用户态校验,不调用 user.info;保持当前打包模式页不跳首页
if (isWorkbenchSession()) {
return { loggedIn: true }
}
if (!token) {
if (!currentRoute || !isLoginRoute(currentRoute)) {
await goLogin()
}
return { loggedIn: false }
}
try {
if (forceValidate && store) {
await store.dispatch('getUserInfo', token)
}
if (!currentRoute || isLoginRoute(currentRoute)) {
await goHome()
}
return { loggedIn: true }
} catch (error) {
clearSession(store)
await goLogin()
return { loggedIn: false, expired: true, error }
}
}
export async function handleTokenExpired(store) {
clearSession(store)
// 工作台会话 token 失效:一并清除工作台登录态,避免登录页 onShow 检测到残留 workbenchToken 后自动重进打包模式造成死循环
try {
uni.removeStorageSync('workbenchToken')
uni.removeStorageSync('workbenchInfo')
uni.removeStorageSync('workbenchOperator')
uni.removeStorageSync('workbenchOperators')
} catch (e) {}
const currentRoute = getCurrentRoute()
if (!isLoginRoute(currentRoute)) {
await goLogin()
}
}