From 18dacb0c75c98d4d631b5ca0821115bca7f88ba1 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Mon, 2 Dec 2024 20:03:31 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=B7=AF=E7=94=B1=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E5=90=88=E5=B9=B6=E6=8E=92=E5=BA=8F=EF=BC=88=E8=BF=98=E5=AD=98?= =?UTF-8?q?=E5=9C=A8=E5=88=87=E6=8D=A2=E8=B4=A6=E5=8F=B7=E8=8F=9C=E5=8D=95?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E9=97=AE=E9=A2=98=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/common/router.ts | 13 +++-- src/router/elegant/routes.ts | 6 ++- src/router/elegant/transform.ts | 25 +++------- src/store/modules/route/index.ts | 83 ++++++++----------------------- src/store/modules/route/shared.ts | 2 +- 5 files changed, 37 insertions(+), 92 deletions(-) diff --git a/src/hooks/common/router.ts b/src/hooks/common/router.ts index bc5277d..ef4529e 100644 --- a/src/hooks/common/router.ts +++ b/src/hooks/common/router.ts @@ -1,7 +1,7 @@ -import { useRouter } from 'vue-router'; -import type { RouteLocationRaw } from 'vue-router'; -import type { RouteKey } from '@elegant-router/types'; -import { router as globalRouter } from '@/router'; +import type {RouteLocationRaw} from 'vue-router'; +import {useRouter} from 'vue-router'; +import type {RouteKey} from '@elegant-router/types'; +import {router as globalRouter} from '@/router'; /** * Router push @@ -24,7 +24,7 @@ export function useRouterPush(inSetup = true) { } async function routerPushByKey(key: RouteKey, options?: RouterPushOptions) { - const { query, params } = options || {}; + const {query, params} = options || {}; const routeLocation: RouteLocationRaw = { name: key @@ -53,7 +53,6 @@ export function useRouterPush(inSetup = true) { */ async function toLogin(loginModule?: UnionKey.LoginModule, redirectUrl?: string) { const module = loginModule || 'pwd-login'; - const options: RouterPushOptions = { params: { module @@ -77,7 +76,7 @@ export function useRouterPush(inSetup = true) { async function toggleLoginModule(module: UnionKey.LoginModule) { const query = route.value.query as Record; - return routerPushByKey('login', { query, params: { module } }); + return routerPushByKey('login', {query, params: {module}}); } /** Redirect from login */ diff --git a/src/router/elegant/routes.ts b/src/router/elegant/routes.ts index 2f267da..a3170ce 100644 --- a/src/router/elegant/routes.ts +++ b/src/router/elegant/routes.ts @@ -79,6 +79,8 @@ export const generatedRoutes: GeneratedRoute[] = [ i18nKey: 'route.user-center' } }, + + // 下面是管理员角色才有 { name: 'function', path: '/function', @@ -87,7 +89,7 @@ export const generatedRoutes: GeneratedRoute[] = [ title: 'function', i18nKey: 'route.function', icon: 'icon-park-outline:all-application', - order: 6, + order: 1002, roles: ['admin'], }, children: [ @@ -205,7 +207,7 @@ export const generatedRoutes: GeneratedRoute[] = [ title: 'about', i18nKey: 'route.about', icon: 'fluent:book-information-24-regular', - order: 100, + order: 1003, roles: ['admin'] } }, diff --git a/src/router/elegant/transform.ts b/src/router/elegant/transform.ts index 6f4d128..25195f0 100644 --- a/src/router/elegant/transform.ts +++ b/src/router/elegant/transform.ts @@ -3,9 +3,9 @@ // Generated by elegant-router // Read more: https://github.com/soybeanjs/elegant-router -import type { RouteRecordRaw } from 'vue-router'; -import type { ElegantConstRoute } from '@elegant-router/vue'; -import type { RouteKey, RouteMap, RoutePath } from '@elegant-router/types'; +import type {RouteRecordRaw} from 'vue-router'; +import type {ElegantConstRoute} from '@elegant-router/vue'; +import type {RouteKey, RouteMap} from '@elegant-router/types'; import BaseLayout from '@/layouts/base-layout/index.vue'; import BlankLayout from '@/layouts/blank-layout/index.vue'; @@ -84,14 +84,14 @@ function transformElegantRouteToVueRoute( route.props = true; } - const { name, path, component, children, ...rest } = route; + const {name, path, component, children, ...rest} = route; - const vueRoute = { name, path, ...rest } as RouteRecordRaw; + const vueRoute = {name, path, ...rest} as RouteRecordRaw; try { if (component) { if (isSingleLevelRoute(route)) { - const { layout, view } = getSingleLevelRouteComponent(component); + const {layout, view} = getSingleLevelRouteComponent(component); const singleLevelRoute: RouteRecordRaw = { path, @@ -208,18 +208,5 @@ const routeMap: RouteMap = { * @param name route name */ export function getRoutePath(name: T) { - console.log(name); return routeMap[name]; } - -/** - * get route name by route path - * @param path route path - */ -export function getRouteName(path: RoutePath) { - const routeEntries = Object.entries(routeMap) as [RouteKey, RoutePath][]; - - const routeName: RouteKey | null = routeEntries.find(([, routePath]) => routePath === path)?.[0] || null; - - return routeName; -} diff --git a/src/store/modules/route/index.ts b/src/store/modules/route/index.ts index d71e234..ac2715c 100644 --- a/src/store/modules/route/index.ts +++ b/src/store/modules/route/index.ts @@ -1,16 +1,16 @@ -import { computed, ref, shallowRef } from 'vue'; -import type { RouteRecordRaw } from 'vue-router'; -import { defineStore } from 'pinia'; -import { useBoolean } from '@sa/hooks'; -import type { CustomRoute, ElegantConstRoute, LastLevelRouteKey, RouteKey } from '@elegant-router/types'; -import { SetupStoreId } from '@/enum'; -import { router } from '@/router'; -import { createStaticRoutes, getAuthVueRoutes } from '@/router/routes'; -import { ROOT_ROUTE } from '@/router/routes/builtin'; -import { getRoutePath } from '@/router/elegant/transform'; -import { useAppStore } from '../app'; -import { useAuthStore } from '../auth'; -import { useTabStore } from '../tab'; +import {computed, ref, shallowRef} from 'vue'; +import type {RouteRecordRaw} from 'vue-router'; +import {defineStore} from 'pinia'; +import {useBoolean} from '@sa/hooks'; +import type {CustomRoute, ElegantConstRoute, LastLevelRouteKey, RouteKey} from '@elegant-router/types'; +import {SetupStoreId} from '@/enum'; +import {router} from '@/router'; +import {createStaticRoutes, getAuthVueRoutes} from '@/router/routes'; +import {ROOT_ROUTE} from '@/router/routes/builtin'; +import {getRoutePath} from '@/router/elegant/transform'; +import {useAppStore} from '../app'; +import {useAuthStore} from '../auth'; +import {useTabStore} from '../tab'; import { filterAuthRoutesByRoles, getBreadcrumbsByRoute, @@ -26,8 +26,8 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => { const appStore = useAppStore(); const authStore = useAuthStore(); const tabStore = useTabStore(); - const { bool: isInitConstantRoute, setBool: setIsInitConstantRoute } = useBoolean(); - const { bool: isInitAuthRoute, setBool: setIsInitAuthRoute } = useBoolean(); + const {bool: isInitConstantRoute, setBool: setIsInitConstantRoute} = useBoolean(); + const {bool: isInitAuthRoute, setBool: setIsInitAuthRoute} = useBoolean(); /** Home route key */ @@ -146,17 +146,9 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => { async function initConstantRoute() { if (isInitConstantRoute.value) return; - // if (authRouteMode.value === 'static') { - const { constantRoutes } = createStaticRoutes(); + const {constantRoutes} = createStaticRoutes(); addAuthRoutes(constantRoutes); - // } else { - // const { data, error } = await fetchGetConstantRoutes(); - - // if (!error) { - // addAuthRoutes(data); - // } - // } handleAuthRoutes(); @@ -165,17 +157,12 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => { /** Init auth route */ async function initAuthRoute() { - // await initStaticAuthRoute(); - // await initDynamicAuthRoute(); - - const { data: routes, error } = await doGetUserRoutes(); + const {data: routes, error} = await doGetUserRoutes(); if (!error) { - const { authRoutes: staticAuthRoutes } = createStaticRoutes(); + const {authRoutes: staticAuthRoutes} = createStaticRoutes(); const filteredAuthRoutes = filterAuthRoutesByRoles(staticAuthRoutes, authStore.userInfo.roles ?? []); - console.log(authStore.userInfo.roles,filteredAuthRoutes) - console.log(authStore.userInfo.roles,[ ...filteredAuthRoutes,...routes]) - addAuthRoutes([ ...filteredAuthRoutes,...routes]); + addAuthRoutes(filteredAuthRoutes.concat(routes)); handleAuthRoutes(); @@ -190,36 +177,6 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => { tabStore.initHomeTab(); } - /** Init static auth route */ - async function initStaticAuthRoute() { - const { authRoutes: staticAuthRoutes } = createStaticRoutes(); - const filteredAuthRoutes = filterAuthRoutesByRoles(staticAuthRoutes, authStore.userInfo.roles ?? []); - - addAuthRoutes(filteredAuthRoutes); - - handleAuthRoutes(); - - setIsInitAuthRoute(true); - } - - /** Init dynamic auth route */ - async function initDynamicAuthRoute() { - const { data: routes, error } = await doGetUserRoutes(); - if (!error) { - addAuthRoutes(routes); - - handleAuthRoutes(); - - setRouteHome('home'); - - handleUpdateRootRouteRedirect('home'); - - setIsInitAuthRoute(true); - } else { - await authStore.resetStore(); - } - } - /** handle auth routes */ function handleAuthRoutes() { const sortRoutes = sortRoutesByOrder(authRoutes.value); @@ -265,7 +222,7 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => { const redirect = getRoutePath(redirectKey); if (redirect) { - const rootRoute: CustomRoute = { ...ROOT_ROUTE, redirect }; + const rootRoute: CustomRoute = {...ROOT_ROUTE, redirect}; router.removeRoute(rootRoute.name); diff --git a/src/store/modules/route/shared.ts b/src/store/modules/route/shared.ts index 958d6d3..e82caa6 100644 --- a/src/store/modules/route/shared.ts +++ b/src/store/modules/route/shared.ts @@ -57,7 +57,7 @@ function sortRouteByOrder(route: ElegantConstRoute) { * @param routes routes */ export function sortRoutesByOrder(routes: ElegantConstRoute[]) { - routes.sort((next, prev) => (Number(next.meta?.order) || 0) - (Number(prev.meta?.order) || 0)); + routes.sort((next, prev) => (Number(next.meta?.order) || 1) - (Number(prev.meta?.order) || 1)); routes.forEach(sortRouteByOrder); return routes;