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[] = [] 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() })