Skip to content

Commit

Permalink
fix: 文件路由系统下,用户权限变化后,菜单不更新
Browse files Browse the repository at this point in the history
  • Loading branch information
hooray committed Mar 28, 2024
1 parent 8416b61 commit 79184f4
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/layouts/components/Menu/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ provide(rootMenuInjectionKey, reactive({
'flex-row! w-auto': isMenuPopup && props.mode === 'horizontal',
}"
>
<template v-for="(item, index) in menu" :key="index">
<template v-for="item in menu" :key="item.path ?? JSON.stringify(item)">
<SubMenu v-if="item.children?.length" :menu="item" :unique-key="[item.path ?? JSON.stringify(item)]" />
<Item v-else :item="item" :unique-key="[item.path ?? JSON.stringify(item)]" @click="handleMenuItemClick(item.path ?? JSON.stringify(item))" />
</template>
Expand Down
8 changes: 5 additions & 3 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,22 @@ router.beforeEach(async (to, from, next) => {
}
}
else {
// 获取用户权限
settingsStore.settings.app.enablePermission && await userStore.getPermissions()
// 生成动态路由
switch (settingsStore.settings.app.routeBaseOn) {
case 'frontend':
await routeStore.generateRoutesAtFront(asyncRoutes)
routeStore.generateRoutesAtFront(asyncRoutes)
break
case 'backend':
await routeStore.generateRoutesAtBack()
break
case 'filesystem':
await routeStore.generateRoutesAtFilesystem(asyncRoutesByFilesystem)
routeStore.generateRoutesAtFilesystem(asyncRoutesByFilesystem)
// 文件系统生成的路由,需要手动生成导航数据
switch (settingsStore.settings.menu.baseOn) {
case 'frontend':
await menuStore.generateMenusAtFront()
menuStore.generateMenusAtFront()
break
case 'backend':
await menuStore.generateMenusAtBack()
Expand Down
32 changes: 6 additions & 26 deletions src/store/modules/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ const useMenuStore = defineStore(
let returnMenus: Menu.recordMainRaw[] = []
if (settingsStore.settings.app.routeBaseOn !== 'filesystem') {
returnMenus = convertRouteToMenu(routeStore.routesRaw)
// 如果权限功能开启,则需要对导航数据进行筛选过滤
if (settingsStore.settings.app.enablePermission) {
returnMenus = filterAsyncMenus(returnMenus, userStore.permissions)
}
}
else {
returnMenus = filesystemMenusRaw.value
}
// 如果权限功能开启,则需要对导航数据进行筛选过滤
if (settingsStore.settings.app.enablePermission) {
returnMenus = filterAsyncMenus(returnMenus, userStore.permissions)
}
return returnMenus
})
// 次导航数据
Expand Down Expand Up @@ -176,32 +176,12 @@ const useMenuStore = defineStore(
}
// 生成导航(前端生成)
async function generateMenusAtFront() {
let accessedMenus
// 如果权限功能开启,则需要对导航数据进行筛选过滤
if (settingsStore.settings.app.enablePermission) {
const permissions = await userStore.getPermissions()
accessedMenus = filterAsyncMenus(menu, permissions)
}
else {
accessedMenus = cloneDeep(menu)
}
filesystemMenusRaw.value = accessedMenus.filter(item => item.children.length !== 0)
filesystemMenusRaw.value = menu.filter(item => item.children.length !== 0)
}
// 生成导航(后端生成)
async function generateMenusAtBack() {
await apiApp.menuList().then(async (res) => {
const settingsStore = useSettingsStore()
const userStore = useUserStore()
let accessedMenus: Menu.recordMainRaw[]
// 如果权限功能开启,则需要对导航数据进行筛选过滤
if (settingsStore.settings.app.enablePermission) {
const permissions = await userStore.getPermissions()
accessedMenus = filterAsyncMenus(res.data, permissions)
}
else {
accessedMenus = cloneDeep(res.data)
}
filesystemMenusRaw.value = accessedMenus.filter(item => item.children.length !== 0)
filesystemMenusRaw.value = (res.data as Menu.recordMainRaw[]).filter(item => item.children.length !== 0)
}).catch(() => {})
}
// 设置主导航
Expand Down
23 changes: 6 additions & 17 deletions src/store/modules/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { cloneDeep } from 'lodash-es'
import type { RouteMeta, RouteRecordRaw } from 'vue-router'
import useSettingsStore from './settings'
import useUserStore from './user'
import { resolveRoutePath } from '@/utils'
import { systemRoutes } from '@/router/routes'
import apiApp from '@/api/modules/app'
Expand All @@ -12,7 +11,6 @@ const useRouteStore = defineStore(
'route',
() => {
const settingsStore = useSettingsStore()
const userStore = useUserStore()

const isGenerate = ref(false)
const routesRaw = ref<Route.recordMainRaw[]>([])
Expand Down Expand Up @@ -147,13 +145,10 @@ const useRouteStore = defineStore(
return routes
}

// 根据权限动态生成路由(前端生成)
async function generateRoutesAtFront(asyncRoutes: Route.recordMainRaw[]) {
// 生成路由(前端生成)
function generateRoutesAtFront(asyncRoutes: Route.recordMainRaw[]) {
// 设置 routes 数据
routesRaw.value = converDeprecatedAttribute(cloneDeep(asyncRoutes) as any)
if (settingsStore.settings.app.enablePermission) {
await userStore.getPermissions()
}
isGenerate.value = true
}
// 格式化后端路由数据
Expand All @@ -177,24 +172,18 @@ const useRouteStore = defineStore(
return route
})
}
// 根据权限动态生成路由(后端获取)
// 生成路由(后端获取)
async function generateRoutesAtBack() {
await apiApp.routeList().then(async (res) => {
await apiApp.routeList().then((res) => {
// 设置 routes 数据
routesRaw.value = converDeprecatedAttribute(formatBackRoutes(res.data) as any)
if (settingsStore.settings.app.enablePermission) {
await userStore.getPermissions()
}
isGenerate.value = true
}).catch(() => {})
}
// 根据权限动态生成路由(文件系统生成)
async function generateRoutesAtFilesystem(asyncRoutes: RouteRecordRaw[]) {
// 生成路由(文件系统生成)
function generateRoutesAtFilesystem(asyncRoutes: RouteRecordRaw[]) {
// 设置 routes 数据
filesystemRoutesRaw.value = cloneDeep(asyncRoutes) as any
if (settingsStore.settings.app.enablePermission) {
await userStore.getPermissions()
}
isGenerate.value = true
}
// 记录 accessRoutes 路由,用于登出时删除路由
Expand Down
1 change: 0 additions & 1 deletion src/store/modules/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ const useUserStore = defineStore(
async function getPermissions() {
const res = await apiUser.permission()
permissions.value = res.data.permissions
return permissions.value
}
// 修改密码
async function editPassword(data: {
Expand Down

0 comments on commit 79184f4

Please sign in to comment.