Skip to content

Commit

Permalink
移除routeStore.routes
Browse files Browse the repository at this point in the history
  • Loading branch information
hooray committed Mar 28, 2024
1 parent e983de6 commit ebf59fd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 78 deletions.
82 changes: 55 additions & 27 deletions src/store/modules/menu.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { cloneDeep } from 'lodash-es'
import type { RouteRecordRaw } from 'vue-router'
import useSettingsStore from './settings'
import useUserStore from './user'
import useRouteStore from './route'
import { resolveRoutePath } from '@/utils'
import apiApp from '@/api/modules/app'
import menu from '@/menu'
import type { Menu } from '#/global'
import type { Menu, Route } from '#/global'

const useMenuStore = defineStore(
// 唯一ID
Expand All @@ -15,42 +16,69 @@ const useMenuStore = defineStore(
const userStore = useUserStore()
const routeStore = useRouteStore()

const menus = ref<Menu.recordMainRaw[]>([{
meta: {},
children: [],
}])
const filesystemMenusRaw = ref<Menu.recordMainRaw[]>([])
const actived = ref(0)

// 将多级导航的每一级 path 都转换为完整路径
function convertToFullPath(menu: any[], path: string = '') {
return menu.map((item) => {
item.path = resolveRoutePath(path, item.path)
// 将原始路由转换成导航菜单
function convertRouteToMenu(routes: Route.recordMainRaw[]): Menu.recordMainRaw[] {
const returnMenus: Menu.recordMainRaw[] = []
routes.forEach((item) => {
if (settingsStore.settings.menu.menuMode === 'single') {
returnMenus.length === 0 && returnMenus.push({
meta: {},
children: [],
})
returnMenus[0].children.push(...convertRouteToMenuRecursive(item.children))
}
else {
const menuItem: Menu.recordMainRaw = {
meta: {
title: item?.meta?.title,
icon: item?.meta?.icon,
auth: item?.meta?.auth,
},
children: [],
}
menuItem.children = convertRouteToMenuRecursive(item.children)
returnMenus.push(menuItem)
}
})
return returnMenus
}
function convertRouteToMenuRecursive(routes: RouteRecordRaw[], basePath = ''): Menu.recordRaw[] {
const returnMenus: Menu.recordRaw[] = []
routes.forEach((item) => {
const menuItem: Menu.recordRaw = {
path: resolveRoutePath(basePath, item.path),
meta: {
title: item?.meta?.title,
icon: item?.meta?.icon,
defaultOpened: item?.meta?.defaultOpened,
auth: item?.meta?.auth,
menu: item?.meta?.menu,
link: item?.meta?.link,
},
}
if (item.children) {
item.children = convertToFullPath(item.children, item.path)
menuItem.children = convertRouteToMenuRecursive(item.children, menuItem.path)
}
return item
returnMenus.push(menuItem)
})
return returnMenus
}

// 完整导航数据
const allMenus = computed(() => {
let returnMenus: Menu.recordMainRaw[] = [{
meta: {},
children: [],
}]
let returnMenus: Menu.recordMainRaw[] = []
if (settingsStore.settings.app.routeBaseOn !== 'filesystem') {
if (settingsStore.settings.menu.menuMode === 'single') {
returnMenus[0].children = []
routeStore.routes.forEach((item) => {
returnMenus[0].children?.push(...item.children as Menu.recordRaw[])
})
}
else {
returnMenus = routeStore.routes as Menu.recordMainRaw[]
returnMenus = convertRouteToMenu(routeStore.routesRaw)
// 如果权限功能开启,则需要对导航数据进行筛选过滤
if (settingsStore.settings.app.enablePermission) {
returnMenus = filterAsyncMenus(returnMenus, userStore.permissions)
}
returnMenus.map(item => convertToFullPath(item.children))
}
else {
returnMenus = menus.value
returnMenus = filesystemMenusRaw.value
}
return returnMenus
})
Expand Down Expand Up @@ -157,7 +185,7 @@ const useMenuStore = defineStore(
else {
accessedMenus = cloneDeep(menu)
}
menus.value = accessedMenus.filter(item => item.children.length !== 0)
filesystemMenusRaw.value = accessedMenus.filter(item => item.children.length !== 0)
}
// 生成导航(后端生成)
async function generateMenusAtBack() {
Expand All @@ -173,7 +201,7 @@ const useMenuStore = defineStore(
else {
accessedMenus = cloneDeep(res.data)
}
menus.value = accessedMenus.filter(item => item.children.length !== 0)
filesystemMenusRaw.value = accessedMenus.filter(item => item.children.length !== 0)
}).catch(() => {})
}
// 设置主导航
Expand Down
52 changes: 1 addition & 51 deletions src/store/modules/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,56 +119,6 @@ const useRouteStore = defineStore(
return routes
})

// 判断是否有权限
function hasPermission(permissions: string[], route: Route.recordMainRaw | RouteRecordRaw) {
let isAuth = false
if (route.meta?.auth) {
isAuth = permissions.some((auth) => {
if (typeof route.meta?.auth === 'string') {
return route.meta.auth !== '' ? route.meta.auth === auth : true
}
else if (typeof route.meta?.auth === 'object') {
return route.meta.auth.length > 0 ? route.meta.auth.includes(auth) : true
}
else {
return false
}
})
}
else {
isAuth = true
}
return isAuth
}
// 根据权限过滤路由
function filterAsyncRoutes<T extends Route.recordMainRaw[] | RouteRecordRaw[]>(routes: T, permissions: string[]): T {
const res: any = []
routes.forEach((route) => {
if (hasPermission(permissions, route)) {
const tmpRoute = cloneDeep(route)
if (tmpRoute.children) {
tmpRoute.children = filterAsyncRoutes(tmpRoute.children, permissions)
tmpRoute.children.length && res.push(tmpRoute)
}
else {
res.push(tmpRoute)
}
}
})
return res
}
const routes = computed(() => {
let returnRoutes: Route.recordMainRaw[]
// 如果权限功能开启,则需要对路由数据进行筛选过滤
if (settingsStore.settings.app.enablePermission) {
returnRoutes = filterAsyncRoutes(routesRaw.value as any, userStore.permissions)
}
else {
returnRoutes = cloneDeep(routesRaw.value) as any
}
return returnRoutes
})

// TODO 将设置 meta.sidebar 的属性转换成 meta.menu ,过渡处理,未来将被弃用
let isUsedDeprecatedAttribute = false
function converDeprecatedAttribute<T extends Route.recordMainRaw[]>(routes: T): T {
Expand Down Expand Up @@ -264,7 +214,7 @@ const useRouteStore = defineStore(

return {
isGenerate,
routes,
routesRaw,
currentRemoveRoutes,
flatRoutes,
flatSystemRoutes,
Expand Down

0 comments on commit ebf59fd

Please sign in to comment.