import {createRouter, createWebHistory} from 'vue-router' import staticRouter from './modules/static' import webRouter from './modules/web' import manageRouter from './modules/manage' import Temp404 from '@/views/global/temp/404.vue' import {ElLoading, ElMessage} from "element-plus"; import {useAppStore, useThemeStore} from "@/stores"; import {toLogin} from "@/utils/permissions"; import {getCurrentUser} from "@/api/modules/manage/user"; import {sysThemeGetThemeByThemeType} from "@/api/modules/manage/theme"; const routes = [ ...staticRouter, { path: '/:pathMatch(.*)*', name: 'NotFound', component: Temp404 }, { path: '', redirect: '/web' }, webRouter, ] const router = createRouter({ history: createWebHistory(), routes, }); router.beforeEach((to, from , next) => { const AppStore = useAppStore() const ThemeStore = useThemeStore() if (themeConfig && !ThemeStore.init) { ThemeStore.setTheme(themeConfig) } if (userInfo) { if (!AppStore.userInfo) { AppStore.$patch((state) => { state.userInfo = userInfo }) } if (to.query.routeTitle) { to.meta.title = to.query.routeTitle } document.title = to.meta?.title || ThemeStore.title || import.meta.env.VITE_TITLE next() } else if (to.path === '/login') { next() } else { ElMessage.error('用户信息获取失败!') toLogin() } }) let userInfo = null let themeConfig = null export const initMainRouter = async () => { if (localStorage.getItem('ss_token')) { const loading = ElLoading.service({ lock: true, text: '加载中……', background: 'rgba(0, 0, 0, 0.7)', }) await Promise.all([ getCurrentUser(), sysThemeGetThemeByThemeType(), ]).then(async ([user, theme]) => { userInfo = user.data userInfo.isAdmin = true if (userInfo?.isAdmin) { router.addRoute(manageRouter) } themeConfig = theme.data.themeStyle ? JSON.parse(theme.data.themeStyle) : {} loading.close() }) } else if (location.pathname !== '/login') { toLogin() } } export default router;