diff --git a/.env b/.env index 6a529a8..5823e02 100644 --- a/.env +++ b/.env @@ -35,5 +35,3 @@ VITE_SERVICE_EXPIRED_TOKEN_CODES=403 VITE_SERVICE_SERVER_ERROR_CODE=500 -# when the route mode is static, the defined super role -VITE_STATIC_SUPER_ROLE=R_SUPER diff --git a/src/router/elegant/routes.ts b/src/router/elegant/routes.ts index 6e96324..2f267da 100644 --- a/src/router/elegant/routes.ts +++ b/src/router/elegant/routes.ts @@ -5,6 +5,11 @@ import type { GeneratedRoute } from '@elegant-router/types'; +/** + * custom routes + * + * @link https://github.com/soybeanjs/elegant-router?tab=readme-ov-file#custom-route + */ export const generatedRoutes: GeneratedRoute[] = [ { name: '403', @@ -40,14 +45,38 @@ export const generatedRoutes: GeneratedRoute[] = [ } }, { - name: 'about', - path: '/about', - component: 'layout.base$view.about', + name: 'home', + path: '/home', + component: 'layout.base$view.home', meta: { - title: 'about', - i18nKey: 'route.about', - icon: 'fluent:book-information-24-regular', - order: 10 + title: 'home', + i18nKey: 'route.home', + icon: 'mdi:monitor-dashboard', + order: 0, + } + }, + { + name: 'login', + path: '/login/:module(pwd-login|code-login|register|reset-pwd|bind-wechat)?', + component: 'layout.blank$view._builtin_login', + props: true, + meta: { + title: 'login', + i18nKey: 'route.login', + constant: true, + hideInMenu: true + } + }, + { + name: 'user-center', + path: '/user-center', + component: 'layout.base$view.user-center', + meta: { + title: 'user-center', + hideInMenu: true, + constant: true, + keepAlive: false, + i18nKey: 'route.user-center' } }, { @@ -58,7 +87,8 @@ export const generatedRoutes: GeneratedRoute[] = [ title: 'function', i18nKey: 'route.function', icon: 'icon-park-outline:all-application', - order: 6 + order: 6, + roles: ['admin'], }, children: [ { @@ -141,7 +171,6 @@ export const generatedRoutes: GeneratedRoute[] = [ i18nKey: 'route.function_super-page', icon: 'ic:round-supervisor-account', order: 5, - roles: ['R_SUPER'] } }, { @@ -169,38 +198,15 @@ export const generatedRoutes: GeneratedRoute[] = [ ] }, { - name: 'home', - path: '/home', - component: 'layout.base$view.home', + name: 'about', + path: '/about', + component: 'layout.base$view.about', meta: { - title: 'home', - i18nKey: 'route.home', - icon: 'mdi:monitor-dashboard', - order: 1 + title: 'about', + i18nKey: 'route.about', + icon: 'fluent:book-information-24-regular', + order: 100, + roles: ['admin'] } }, - { - name: 'login', - path: '/login/:module(pwd-login|code-login|register|reset-pwd|bind-wechat)?', - component: 'layout.blank$view._builtin_login', - props: true, - meta: { - title: 'login', - i18nKey: 'route.login', - constant: true, - hideInMenu: true - } - }, - { - name: 'user-center', - path: '/user-center', - component: 'layout.base$view.user-center', - meta: { - title: 'user-center', - hideInMenu: true, - constant: true, - keepAlive: false, - i18nKey: 'route.user-center' - } - } ]; diff --git a/src/router/guard/route.ts b/src/router/guard/route.ts index 5c6cf78..61499c5 100644 --- a/src/router/guard/route.ts +++ b/src/router/guard/route.ts @@ -34,8 +34,8 @@ export function createRouteGuard(router: Router) { const needLogin = !to.meta.constant; const routeRoles = to.meta.roles || []; - // const hasRole = authStore.userInfo.roles?.some(role => routeRoles.includes(role)); - const hasAuth = authStore.isStaticSuper || !routeRoles.length; + const hasRole = authStore.userInfo.roles?.some(role => routeRoles.includes(role)); + const hasAuth = !routeRoles.length || hasRole const routeSwitches: CommonType.StrategicPattern[] = [ // if it is login route when logged in, then switch to the root page diff --git a/src/router/routes/index.ts b/src/router/routes/index.ts index 32f89b7..f3b8c50 100644 --- a/src/router/routes/index.ts +++ b/src/router/routes/index.ts @@ -1,57 +1,6 @@ -import type { ElegantConstRoute } from '@elegant-router/types'; -import { generatedRoutes } from '../elegant/routes'; -import { transformElegantRoutesToVueRoutes } from '../elegant/transform'; - -/** - * custom routes - * - * @link https://github.com/soybeanjs/elegant-router?tab=readme-ov-file#custom-route - */ -const customRoutes: any[] = [ - { - name: 'exception', - path: '/exception', - component: 'layout.base', - meta: { - title: 'exception', - i18nKey: 'route.exception', - icon: 'ant-design:exception-outlined', - order: 7 - }, - children: [ - { - name: 'exception_403', - path: '/exception/403', - component: 'view._builtin_403', - meta: { - title: 'exception_403', - i18nKey: 'route.exception_403', - icon: 'ic:baseline-block' - } - }, - { - name: 'exception_404', - path: '/exception/404', - component: 'view._builtin_404', - meta: { - title: 'exception_404', - i18nKey: 'route.exception_404', - icon: 'ic:baseline-web-asset-off' - } - }, - { - name: 'exception_500', - path: '/exception/500', - component: 'view._builtin_500', - meta: { - title: 'exception_500', - i18nKey: 'route.exception_500', - icon: 'ic:baseline-wifi-off' - } - } - ] - } -]; +import type {ElegantConstRoute} from '@elegant-router/types'; +import {generatedRoutes} from '../elegant/routes'; +import {transformElegantRoutesToVueRoutes} from '../elegant/transform'; /** create routes when the auth route mode is static */ export function createStaticRoutes() { @@ -59,7 +8,7 @@ export function createStaticRoutes() { const authRoutes: any[] = []; - [...customRoutes, ...generatedRoutes].forEach(item => { + generatedRoutes.forEach(item => { if (item.meta?.constant) { constantRoutes.push(item); } else { diff --git a/src/store/modules/auth/index.ts b/src/store/modules/auth/index.ts index 78257dd..3a51f97 100644 --- a/src/store/modules/auth/index.ts +++ b/src/store/modules/auth/index.ts @@ -31,13 +31,6 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => { } ); - /** is super role in static route */ - const isStaticSuper = computed(() => { - const { VITE_AUTH_ROUTE_MODE, VITE_STATIC_SUPER_ROLE } = import.meta.env; - - return VITE_AUTH_ROUTE_MODE === 'static' && userInfo.roles?.includes(VITE_STATIC_SUPER_ROLE); - }); - /** Is login */ const isLogin = computed(() => Boolean(token.value)); @@ -160,7 +153,6 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => { return { token, userInfo, - isStaticSuper, isLogin, loginLoading, resetStore, diff --git a/src/store/modules/route/index.ts b/src/store/modules/route/index.ts index c17ece3..d71e234 100644 --- a/src/store/modules/route/index.ts +++ b/src/store/modules/route/index.ts @@ -165,21 +165,37 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => { /** Init auth route */ async function initAuthRoute() { - await initStaticAuthRoute(); - await initDynamicAuthRoute(); + // await initStaticAuthRoute(); + // await initDynamicAuthRoute(); + + + const { data: routes, error } = await doGetUserRoutes(); + if (!error) { + 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]); + + handleAuthRoutes(); + + setRouteHome('home'); + + handleUpdateRootRouteRedirect('home'); + + setIsInitAuthRoute(true); + } else { + await authStore.resetStore(); + } tabStore.initHomeTab(); } /** Init static auth route */ async function initStaticAuthRoute() { const { authRoutes: staticAuthRoutes } = createStaticRoutes(); - if (authStore.isStaticSuper) { - addAuthRoutes(staticAuthRoutes); - } else { - const filteredAuthRoutes = filterAuthRoutesByRoles(staticAuthRoutes, authStore.userInfo.roles ?? []); + const filteredAuthRoutes = filterAuthRoutesByRoles(staticAuthRoutes, authStore.userInfo.roles ?? []); - addAuthRoutes(filteredAuthRoutes); - } + addAuthRoutes(filteredAuthRoutes); handleAuthRoutes(); @@ -196,7 +212,7 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => { setRouteHome('home'); - handleUpdateRootRouteRedirect('manage_role'); + handleUpdateRootRouteRedirect('home'); setIsInitAuthRoute(true); } else { diff --git a/src/typings/env.d.ts b/src/typings/env.d.ts index ace9243..fc48c52 100644 --- a/src/typings/env.d.ts +++ b/src/typings/env.d.ts @@ -55,8 +55,7 @@ declare namespace Env { * use "," to separate multiple codes */ readonly VITE_SERVICE_EXPIRED_TOKEN_CODES: string; - /** when the route mode is static, the defined super role */ - readonly VITE_STATIC_SUPER_ROLE: string; + /** * other backend service base url *