WMS_PDA/core/utils/auth-guard.js

164 lines
3.8 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)
const currentRoute = getCurrentRoute()
if (!isLoginRoute(currentRoute)) {
await goLogin()
}
}