import { findIndexOf, isEmpty } from 'xe-utils'; import type { Router, RouteRecordRaw, RouteComponent } from 'vue-router'; import { cookies, NProgress, openLink, isUrl, buildHierarchyTree } from '@utogether/utils'; import { useMultiTagsStoreHook } from '../store/modules/multiTags'; import { usePermissionStoreHook } from '../store/modules/permission'; import { ascending, initRouter, findRouteByPath, formatTwoStageRoutes, formatFlatteningRoutes } from './utils'; // import { getEnv } from '../config'; import { kLOGINER } from '../contant'; import homeRouter from './modules/home'; import errorRouter from './modules/error'; import remainingRouter from './modules/remaining'; import flow from './modules/flow'; const systemRouter = import.meta.glob([ '../views/system/**/*.{vue,tsx}', '../views/uapp/**/*.{vue,tsx}', '../views/upms/**/*.{vue,tsx}', '../views/ufile/**/*.{vue}', '../views/urpt/**/*.{vue,tsx}', '../views/organization/**/*.{vue,tsx}', '../views/udev/**/*.{vue, tsx}' // '../views/uflow/**/*.{vue, tsx}' ]); export const remainingRouters = remainingRouter; // 原始静态路由(未做任何处理) const routes = [homeRouter, errorRouter, flow]; /** 导出处理后的静态路由(三级及以上的路由全部拍成二级) */ export const constantRoutes: Array = formatTwoStageRoutes( formatFlatteningRoutes(buildHierarchyTree(ascending(routes))) ); /** 用于渲染菜单,保持原始层级 */ export const constantMenus: Array = ascending(routes).concat(...remainingRouter); /** 不参与菜单的路由 */ export const remainingPaths = Object.keys(remainingRouter).map(v => { return remainingRouter[v].path; }); /** 创建路由实例 */ // export const router: Router = createRouter({ // history: createWebHistory(window.__HISTORY_PATH__), // routes: constantRoutes.concat(...(remainingRouter as any)), // strict: true, // scrollBehavior(to, from, savedPosition) { // return new Promise(resolve => { // if (savedPosition) { // return savedPosition; // } else { // if (from.meta.saveSrollTop) { // const top: number = document.documentElement.scrollTop || document.body.scrollTop; // resolve({ left: 0, top }); // } // } // }); // } // }); /** 记录已经加载的页面路径 */ const loadedPaths = new Set(); /** 重置已加载页面记录 */ export function resetLoadedPaths() { loadedPaths.clear(); } /** 重置路由 */ export function resetRouter() { router.getRoutes().forEach(route => { const { name, meta } = route; if (name && router.hasRoute(name) && meta?.backstage) { router.removeRoute(name); router.options.routes = formatTwoStageRoutes(formatFlatteningRoutes(buildHierarchyTree(ascending(routes)))); } }); usePermissionStoreHook().clearAllCachePage(); resetLoadedPaths(); } let views = null; export let router = null; export function setViews(cviews, crouter) { views = cviews; router = crouter; Object.assign(views, systemRouter); } export function getViews() { return views; } /** 路由白名单 */ const whiteList = ['/login']; /* router.beforeEach((to: ToRouteType, _from, next) => { if (to.meta?.keepAlive) { handleAliveRoute(to, 'add'); // 页面整体刷新和点击标签页刷新 if (_from.name === undefined || _from.name === 'redirect') { handleAliveRoute(to); } } const userName = cookies.get(kLOGINER); NProgress.start(); if (userName) { routerBefore(to, _from, next); } else { if (to.path !== '/login') { if (whiteList.indexOf(to.path) !== -1) { next(); } else { next({ path: '/login' }); } } else { next(); } } }); router.afterEach(() => { NProgress?.done(); }); */ export const routerBefore = (to, _from, next) => { // TODO 无权限跳转403页面 // if (to.meta?.roles && !isOneOfArray(to.meta?.roles, userInfo?.roles)) { // next({ path: "/error/403" }); // } const userName = cookies.get(kLOGINER); const externalLink = isUrl(to?.name as string); /** 如果已经登录并存在登录信息后不能跳转到路由白名单,而是继续保持在当前页面 */ function toCorrectRoute() { whiteList.includes(to.fullPath) ? next(_from.fullPath) : next(); } if (_from?.name && _from?.name !== 'login') { // m.name === to.name 判断param路由跳转 const cmp = to.matched.find(m => m.path === to.path || (m.name === to.name && to.params))?.components; // 如果路由包含http 则是超链接 反之是普通路由 if (externalLink) { openLink(to?.name as string); NProgress?.done(); } else if (cmp?.default || to.name === 'redirect') { toCorrectRoute(); } else { next({ path: '/error/404' }); } } else { // 刷新 if (usePermissionStoreHook().wholeMenus.length === 0) { initRouter(userName).then((router: Router) => { if (!useMultiTagsStoreHook().getMultiTagsCache) { const { path } = to; const index = findIndexOf(remainingRouter, v => v.path === path); const routes: any = index === -1 ? router.options.routes[0].children : router.options.routes; const route = findRouteByPath(path, routes); if (route && route.meta?.title) { const { path, name, meta } = route; useMultiTagsStoreHook().handleTags('push', { path, name, meta }); } } // 确保动态路由完全加入路由列表并且不影响静态路由 // (注意:动态路由刷新时router.beforeEach可能会触发两次, // 第一次触发动态路由还未完全添加,第二次动态路由才完全添加到路由列表, // 如果需要在router.beforeEach做一些判断可以在to.name存在的条件下去判断,这样就只会触发一次) if (isEmpty(to.name)) router.push(to.fullPath); }); } if (_from.fullPath === '/' && to.fullPath === '/login' && !to.query?.tokenExpire) { to.path = '/welcome'; to.fullPath = '/welcome'; to.name = 'UdpWelcome'; } else if (_from.fullPath === '/' && to.path === '/workflow/approve/index') { to.path = '/welcome'; to.fullPath = '/welcome'; to.name = 'UdpWelcome'; to.query = {}; } // next(); toCorrectRoute(); } }; // export default router;