| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- import router from './router'
- import type { RouteRecordRaw } from 'vue-router'
- import { isRelogin } from '@/config/axios/service'
- import { getAccessToken, removeToken } from '@/utils/auth'
- import { useTitle } from '@/hooks/web/useTitle'
- import { useNProgress } from '@/hooks/web/useNProgress'
- import { usePageLoading } from '@/hooks/web/usePageLoading'
- import { useDictStoreWithOut } from '@/store/modules/dict'
- import { useUserStoreWithOut } from '@/store/modules/user'
- import { usePermissionStoreWithOut } from '@/store/modules/permission'
- import * as authUtil from '@/utils/auth'
- import { useUserStore } from '@/store/modules/user'
- import { useSettingStore } from '@/store/modules/setting'
- const { start, done } = useNProgress()
- const { loadStart, loadDone } = usePageLoading()
- const userStore = useUserStore()
- const settingStore = useSettingStore()
- const parseURL = (
- url: string | null | undefined
- ): { basePath: string; paramsObject: { [key: string]: string } } => {
- if (url == null) {
- return { basePath: '', paramsObject: {} }
- }
- const questionMarkIndex = url.indexOf('?')
- let basePath = url
- const paramsObject: { [key: string]: string } = {}
- if (questionMarkIndex !== -1) {
- basePath = url.substring(0, questionMarkIndex)
- const queryString = url.substring(questionMarkIndex + 1)
- const searchParams = new URLSearchParams(queryString)
- searchParams.forEach((value, key) => {
- paramsObject[key] = value
- })
- }
- return { basePath, paramsObject }
- }
- const whiteList = [
- '/login',
- '/social-login',
- '/auth-redirect',
- '/bind',
- '/register',
- '/oauthLogin/gitee'
- ]
- const handleDynamicRoutes = async (from: any, to: any, next: any, fromH5?: boolean) => {
- const dictStore = useDictStoreWithOut()
- const userStoreOut = useUserStoreWithOut()
- const permissionStore = usePermissionStoreWithOut()
- const tasks: Promise<any>[] = []
- if (!dictStore.getIsSetDict) {
- tasks.push(dictStore.setDictMap())
- }
- if (!settingStore.getIsLoaded) {
- tasks.push(settingStore.getParamsConfig())
- }
- isRelogin.show = true
- tasks.push(userStoreOut.setTenantPackages())
- await Promise.all(tasks)
- await userStoreOut.setUserInfoAction(fromH5)
- isRelogin.show = false
- await permissionStore.generateRoutes()
- permissionStore.getAddRouters.forEach((route) => {
- router.addRoute(route as unknown as RouteRecordRaw)
- })
- const redirectPath = from.query.redirect || to.path
- const redirect = decodeURIComponent(redirectPath as string)
- const { paramsObject: query } = parseURL(redirect)
- const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect, query }
- next(nextData)
- }
- router.beforeEach(async (to, from, next) => {
- start()
- loadStart()
- if (getAccessToken()) {
- if (to.path === '/login') {
- if(userStore.getIsBusiness){
- next({path: '/home/data_index'})
- }else{
- next({ path: '/' })
- }
- } else {
- if(to.query && to.query.from && to.query.from === 'h5'){
- const data = JSON.parse(to.query.token)
- const tenantId = Number(data.tenantId)
- authUtil.setTenantId(tenantId)
- authUtil.setLoginForm({
- tenantName: data.tenantName,
- username: data.username
- })
- authUtil.setToken(data)
- }
- const dictStore = useDictStoreWithOut()
- const userStoreOut = useUserStoreWithOut()
- if (!dictStore.getIsSetDict || !userStoreOut.getIsSetUser || !settingStore.getIsLoaded) {
- await handleDynamicRoutes(from, to, next, to.query?.from as string | undefined)
- } else {
- next()
- }
- }
- } else {
- if (whiteList.indexOf(to.path) !== -1) {
- next()
- } else {
- if(to.query && to.query.from && to.query.from === 'h5'){
- const data = JSON.parse(to.query.token)
- const tenantId = Number(data.tenantId)
- authUtil.setTenantId(tenantId)
- authUtil.setLoginForm({
- tenantName: data.tenantName,
- username: data.username
- })
- authUtil.setToken(data)
- await handleDynamicRoutes(from, to, next, 'h5')
- }else{
- next(`/login?redirect=${to.fullPath}`)
- }
- }
- }
- })
- router.afterEach((to) => {
- useTitle(to?.meta?.title as string)
- done()
- loadDone()
- })
|