dataease-dm/core/core-frontend/src/permissionMobile.ts

45 lines
1.4 KiB
Java
Raw Normal View History

2024-01-17 17:26:19 +08:00
import router from './router/mobile'
import { useUserStoreWithOut } from '@/store/modules/user'
import { useNProgress } from '@/hooks/web/useNProgress'
import { usePageLoading } from '@/hooks/web/usePageLoading'
import { useCache } from '@/hooks/web/useCache'
import { getRoleRouters } from '@/api/common'
import { usePermissionStoreWithOut } from '@/store/modules/permission'
import { interactiveStoreWithOut } from '@/store/modules/interactive'
const permissionStore = usePermissionStoreWithOut()
const { wsCache } = useCache()
const userStore = useUserStoreWithOut()
const { start, done } = useNProgress()
const interactiveStore = interactiveStoreWithOut()
const { loadStart, loadDone } = usePageLoading()
router.beforeEach(async (to, _, next) => {
start()
loadStart()
if (wsCache.get('user.token')) {
if (!userStore.getUid) {
await userStore.setUser()
}
if (to.path === '/login') {
next({ path: '/index' })
} else {
const roleRouters = (await getRoleRouters()) || []
const routers: any[] = roleRouters as AppCustomRouteRecordRaw[]
routers.forEach(item => (item['top'] = true))
await permissionStore.generateRoutes(routers as AppCustomRouteRecordRaw[])
permissionStore.setIsAddRouters(true)
await interactiveStore.initInteractive(true)
next()
}
} else {
next('/login') // 否则全部重定向到登录页
}
})
router.afterEach(() => {
done()
loadDone()
})