优化backend前端布局
This commit is contained in:
1 parent
68617335f0
commit
e34543465a
11 files changed
+1899
-903
No files matched your search
@@ -0,0 +1,318 @@
|
||||
import { computed } from "vue";
|
||||
import { useRoute } from "vue-router";
|
||||
import { useMenuStore } from "@/stores/menu";
|
||||
import { pickDefaultChild, isWorkbenchMenu } from "@/router/menuDefaults";
|
||||
|
||||
/**
|
||||
* 后台框架导航模型。
|
||||
*
|
||||
* 层级约定:
|
||||
* 模块(从门户 /home 进入,如「OA管理」/apps/oa)
|
||||
* └─ 顶级菜单(顶部导航,如「仪表盘」「审批中心」)
|
||||
* └─ 左侧菜单(选中顶级菜单后的内容)
|
||||
*
|
||||
* 顶部导航展示的是当前模块下的顶级菜单,不是门户里的模块卡片。
|
||||
* 选中态直接由当前路由反推,因此标签页切换、刷新、深链接都能自动还原。
|
||||
*/
|
||||
|
||||
const fixedCmsMenu = {
|
||||
id: -200,
|
||||
path: "/apps/cms",
|
||||
title: "内容管理",
|
||||
icon: "Document",
|
||||
sort: -200,
|
||||
order: -200,
|
||||
children: [
|
||||
{
|
||||
id: -2001,
|
||||
path: "/apps/cms/analytics/content",
|
||||
title: "内容统计",
|
||||
icon: "DataAnalysis",
|
||||
sort: 0,
|
||||
order: 0,
|
||||
children: [],
|
||||
},
|
||||
{
|
||||
id: -201,
|
||||
path: "/apps/cms/article",
|
||||
title: "文章管理",
|
||||
icon: "Document",
|
||||
sort: 1,
|
||||
order: 1,
|
||||
children: [],
|
||||
},
|
||||
{
|
||||
id: -202,
|
||||
path: "/apps/cms/article/type",
|
||||
title: "文章分类",
|
||||
icon: "Folder",
|
||||
sort: 2,
|
||||
order: 2,
|
||||
children: [],
|
||||
},
|
||||
{
|
||||
id: -203,
|
||||
path: "/apps/cms/products",
|
||||
title: "产品管理",
|
||||
icon: "Goods",
|
||||
sort: 3,
|
||||
order: 3,
|
||||
children: [],
|
||||
},
|
||||
{
|
||||
id: -204,
|
||||
path: "/apps/cms/products/types",
|
||||
title: "产品分类",
|
||||
icon: "Folder",
|
||||
sort: 4,
|
||||
order: 4,
|
||||
children: [],
|
||||
},
|
||||
{
|
||||
id: -205,
|
||||
path: "/apps/cms/solutions",
|
||||
title: "解决方案管理",
|
||||
icon: "Promotion",
|
||||
sort: 5,
|
||||
order: 5,
|
||||
children: [],
|
||||
},
|
||||
{
|
||||
id: -206,
|
||||
path: "/apps/cms/solutions/types",
|
||||
title: "解决方案分类",
|
||||
icon: "Folder",
|
||||
sort: 6,
|
||||
order: 6,
|
||||
children: [],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
function processMenus(menus) {
|
||||
return (menus || [])
|
||||
.filter((menu) => {
|
||||
// is_visible 只控制侧栏/顶部是否展示,不影响动态路由注册
|
||||
if (menu.is_visible !== undefined && Number(menu.is_visible) === 0) {
|
||||
return false;
|
||||
}
|
||||
if (menu.path && String(menu.path).trim() !== "") return true;
|
||||
return !!(menu.children && menu.children.length > 0);
|
||||
})
|
||||
.map((menu) => {
|
||||
const sort = Number(menu.sort) || 0;
|
||||
return {
|
||||
id: menu.id,
|
||||
pid: menu.pid || 0,
|
||||
path: menu.path,
|
||||
icon: menu.icon || "Grid",
|
||||
title: menu.title,
|
||||
component_path: menu.component_path || "",
|
||||
type: menu.type,
|
||||
locked: !!menu.locked,
|
||||
sort,
|
||||
order: sort,
|
||||
children: menu.children ? processMenus(menu.children) : [],
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function mergeFixedCmsMenu(menus) {
|
||||
return menus.some((menu) => menu.path === fixedCmsMenu.path)
|
||||
? menus
|
||||
: [fixedCmsMenu, ...menus];
|
||||
}
|
||||
|
||||
function sortMenusRecursively(menus) {
|
||||
(menus || []).forEach((menu) => {
|
||||
if (menu.children && menu.children.length > 0) {
|
||||
menu.children.sort((a, b) => {
|
||||
if (a.order !== b.order) return a.order - b.order;
|
||||
return (Number(a.id) || 0) - (Number(b.id) || 0);
|
||||
});
|
||||
sortMenusRecursively(menu.children);
|
||||
}
|
||||
});
|
||||
return menus;
|
||||
}
|
||||
|
||||
function findMenuItem(menus, targetIndex) {
|
||||
for (const menu of menus || []) {
|
||||
if (menu.path === targetIndex) return menu;
|
||||
if (menu.children && menu.children.length > 0) {
|
||||
const found = findMenuItem(menu.children, targetIndex);
|
||||
if (found) return found;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function isPathInSubtree(base, currentPath) {
|
||||
if (!base) return false;
|
||||
const normalized = base.replace(/\/+$/, "");
|
||||
return currentPath === normalized || currentPath.startsWith(`${normalized}/`);
|
||||
}
|
||||
|
||||
function findParentModule(menus, currentPath) {
|
||||
for (const menu of menus) {
|
||||
if (!menu.path || menu.path === "/home") {
|
||||
const found = findParentModule(menu.children || [], currentPath);
|
||||
if (found) return found;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isPathInSubtree(menu.path, currentPath)) return menu;
|
||||
|
||||
for (const child of menu.children || []) {
|
||||
if (isPathInSubtree(child.path, currentPath)) return menu;
|
||||
for (const grandchild of child.children || []) {
|
||||
if (isPathInSubtree(grandchild.path, currentPath)) return menu;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** 在当前模块的顶级菜单里,找出路径最具体覆盖当前路由的那一项 */
|
||||
function findActiveTopMenu(topMenus, currentPath) {
|
||||
let best = null;
|
||||
for (const top of topMenus) {
|
||||
const stack = [top];
|
||||
while (stack.length) {
|
||||
const node = stack.pop();
|
||||
if (node.path && isPathInSubtree(node.path, currentPath)) {
|
||||
const length = node.path.length;
|
||||
if (!best || length > best.length) {
|
||||
best = { menu: top, length };
|
||||
}
|
||||
}
|
||||
if (node.children && node.children.length) {
|
||||
stack.push(...node.children);
|
||||
}
|
||||
}
|
||||
}
|
||||
return best?.menu || null;
|
||||
}
|
||||
|
||||
/** 点击顶级菜单时落到具体页面,避免多出一个「目录」标签页 */
|
||||
function resolveLandingMenu(menu) {
|
||||
if (!menu) return null;
|
||||
if (menu.locked) return menu.path ? menu : null;
|
||||
|
||||
let current = menu;
|
||||
const visited = new Set();
|
||||
while (current.children && current.children.length > 0 && !visited.has(current.id)) {
|
||||
visited.add(current.id);
|
||||
// 目录本身有页面时,访问目录路径即可(路由会渲染 index 子路由)
|
||||
if (current.component_path) break;
|
||||
const next = pickDefaultChild(current.children);
|
||||
if (!next) break;
|
||||
current = next;
|
||||
}
|
||||
return current.path ? current : menu.path ? menu : null;
|
||||
}
|
||||
|
||||
function flattenMenus(menus, parentTitle = "") {
|
||||
const result = [];
|
||||
for (const menu of menus || []) {
|
||||
// 只把「可直接打开的页面」放进搜索结果,纯目录交给路由 redirect,
|
||||
// 否则会同时产生「目录」和「落地页」两个标签页
|
||||
const isPage = !menu.children?.length || !!menu.component_path;
|
||||
if (menu.path && menu.path !== "/home" && isPage) {
|
||||
result.push({
|
||||
id: menu.id,
|
||||
path: menu.path,
|
||||
title: menu.title,
|
||||
icon: menu.icon,
|
||||
locked: menu.locked,
|
||||
parentTitle: parentTitle || menu.title,
|
||||
});
|
||||
}
|
||||
if (menu.children && menu.children.length) {
|
||||
result.push(...flattenMenus(menu.children, menu.title || parentTitle));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/** 在当前模块下找到可直接打开的「仪表盘 / 工作台」页面,作为默认标签页 */
|
||||
function findWorkbenchPage(menus) {
|
||||
const stack = [...menus];
|
||||
const visited = new Set();
|
||||
while (stack.length) {
|
||||
const node = stack.shift();
|
||||
if (!node || visited.has(node.id)) continue;
|
||||
visited.add(node.id);
|
||||
if (
|
||||
node.path &&
|
||||
isWorkbenchMenu(node) &&
|
||||
(!node.children?.length || !!node.component_path)
|
||||
) {
|
||||
return node;
|
||||
}
|
||||
if (node.children?.length) stack.push(...node.children);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function useAdminNavigation() {
|
||||
const route = useRoute();
|
||||
const menuStore = useMenuStore();
|
||||
|
||||
const list = computed(() =>
|
||||
sortMenusRecursively(processMenus(mergeFixedCmsMenu(menuStore.menus || []))),
|
||||
);
|
||||
|
||||
const currentModule = computed(() => {
|
||||
const path = route.path;
|
||||
if (!path || path === "/" || path === "/home") return null;
|
||||
return findParentModule(list.value, path);
|
||||
});
|
||||
|
||||
const topMenus = computed(() => currentModule.value?.children || []);
|
||||
|
||||
const activeTopMenu = computed(() => {
|
||||
const menus = topMenus.value;
|
||||
if (!menus.length) return null;
|
||||
return (
|
||||
findActiveTopMenu(menus, route.path) ||
|
||||
pickDefaultChild(menus) ||
|
||||
menus[0] ||
|
||||
null
|
||||
);
|
||||
});
|
||||
|
||||
const sideMenus = computed(() => {
|
||||
const active = activeTopMenu.value;
|
||||
if (!active) return [];
|
||||
return active.children && active.children.length ? active.children : [active];
|
||||
});
|
||||
|
||||
const sideTitle = computed(
|
||||
() => activeTopMenu.value?.title || currentModule.value?.title || "功能菜单",
|
||||
);
|
||||
|
||||
const searchableMenus = computed(() => flattenMenus(list.value));
|
||||
|
||||
// 「首页」已移除:进入模块后默认展示该模块的仪表盘 / 工作台
|
||||
const defaultTab = computed(() => {
|
||||
const module = currentModule.value;
|
||||
if (!module) return null;
|
||||
const target =
|
||||
findWorkbenchPage(module.children || []) || resolveLandingMenu(module);
|
||||
return target?.path ? target : null;
|
||||
});
|
||||
|
||||
return {
|
||||
list,
|
||||
currentModule,
|
||||
topMenus,
|
||||
activeTopMenu,
|
||||
sideMenus,
|
||||
sideTitle,
|
||||
defaultTab,
|
||||
searchableMenus,
|
||||
findMenuItem,
|
||||
resolveLandingMenu,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user