Skip to content

Commit

Permalink
feat: 路由增加 meta.menu 配置,同时 meta.sidebar 进入弃用过渡期
Browse files Browse the repository at this point in the history
  • Loading branch information
hooray committed Mar 16, 2024
1 parent 1685f8b commit 04fa39c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/layouts/components/Menu/sub.vue
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const transitionClass = computed(() => {
const hasChildren = computed(() => {
let flag = true
if (props.menu.children) {
if (props.menu.children.every((item: any) => item.meta?.sidebar === false)) {
if (props.menu.children.every((item: any) => item.meta?.menu === false)) {
flag = false
}
}
Expand Down Expand Up @@ -189,7 +189,7 @@ function handleMouseleave() {
}"
>
<template v-for="item in menu.children" :key="item.path ?? JSON.stringify(item)">
<SubMenu v-if="item.meta?.sidebar !== false" :unique-key="[...uniqueKey, item.path ?? JSON.stringify(item)]" :menu="item" :level="level + 1" />
<SubMenu v-if="item.meta?.menu !== false" :unique-key="[...uniqueKey, item.path ?? JSON.stringify(item)]" :menu="item" :level="level + 1" />
</template>
</OverlayScrollbarsComponent>
</Transition>
Expand Down
4 changes: 2 additions & 2 deletions src/layouts/components/Search/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,14 @@ function initSourceList() {
function hasChildren(item: RouteRecordRaw) {
let flag = true
if (item.children?.every(i => i.meta?.sidebar === false)) {
if (item.children?.every(i => i.meta?.menu === false)) {
flag = false
}
return flag
}
function getSourceList(arr: RouteRecordRaw[], basePath?: string, icon?: string, breadcrumb?: { title?: string | (() => string) }[]) {
arr.forEach((item) => {
if (item.meta?.sidebar !== false) {
if (item.meta?.menu !== false) {
const breadcrumbTemp = cloneDeep(breadcrumb) || []
if (item.children && hasChildren(item)) {
breadcrumbTemp.push({
Expand Down
2 changes: 1 addition & 1 deletion src/store/modules/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const useMenuStore = defineStore(
function getDeepestPath(menu: Menu.recordRaw, rootPath = '') {
let retnPath = ''
if (menu.children) {
const item = menu.children.find(item => item.meta?.sidebar !== false)
const item = menu.children.find(item => item.meta?.menu !== false)
if (item) {
retnPath = getDeepestPath(item, resolveRoutePath(rootPath, menu.path))
}
Expand Down
31 changes: 29 additions & 2 deletions src/store/modules/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,37 @@ const useRouteStore = defineStore(
return returnRoutes
})

// TODO 将设置 meta.sidebar 的属性转换成 meta.menu ,过渡处理,未来将被弃用
let isUsedDeprecatedAttribute = false
function converDeprecatedAttribute<T extends Route.recordMainRaw[]>(routes: T): T {
routes.forEach((route) => {
route.children = converDeprecatedAttributeRecursive(route.children)
})
if (isUsedDeprecatedAttribute) {
console.warn('[Fantastic-admin] 路由配置中的 "sidebar" 属性即将被弃用, 请尽快替换为 "menu" 属性')
}
return routes
}
function converDeprecatedAttributeRecursive(routes: RouteRecordRaw[]) {
if (routes) {
routes.forEach((route) => {
if (typeof route.meta?.sidebar === 'boolean') {
isUsedDeprecatedAttribute = true
route.meta.menu = route.meta.sidebar
delete route.meta.sidebar
}
if (route.children) {
converDeprecatedAttributeRecursive(route.children)
}
})
}
return routes
}

// 根据权限动态生成路由(前端生成)
async function generateRoutesAtFront(asyncRoutes: Route.recordMainRaw[]) {
// 设置 routes 数据
routesRaw.value = cloneDeep(asyncRoutes) as any
routesRaw.value = converDeprecatedAttribute(cloneDeep(asyncRoutes) as any)
if (settingsStore.settings.app.enablePermission) {
await userStore.getPermissions()
}
Expand Down Expand Up @@ -203,7 +230,7 @@ const useRouteStore = defineStore(
async function generateRoutesAtBack() {
await apiApp.routeList().then(async (res) => {
// 设置 routes 数据
routesRaw.value = formatBackRoutes(res.data) as any
routesRaw.value = converDeprecatedAttribute(formatBackRoutes(res.data) as any)
if (settingsStore.settings.app.enablePermission) {
await userStore.getPermissions()
}
Expand Down
3 changes: 2 additions & 1 deletion src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ declare module 'vue-router' {
defaultOpened?: boolean
auth?: string | string[]
sidebar?: boolean
menu?: boolean
breadcrumb?: boolean
activeMenu?: string
cache?: boolean | string | string[]
Expand Down Expand Up @@ -263,7 +264,7 @@ declare namespace Menu {
icon?: string
defaultOpened?: boolean
auth?: string | string[]
sidebar?: boolean
menu?: boolean
link?: string
}
children?: recordRaw[]
Expand Down

0 comments on commit 04fa39c

Please sign in to comment.