Files
yunzerwebsiteallinone/backend/src/router/dynamicRoutes.js
T
2026-11-16 17:22:50 +08:00

201 lines
6.0 KiB
JavaScript

import { createComponentLoader } from '@/utils/pathResolver';
import { pickDefaultChild } from './menuDefaults';
function computeFullPath(menuPath, parentPath) {
if (!menuPath) return parentPath || '';
if (menuPath.startsWith('/')) {
return menuPath.replace(/\/+/g, '/');
}
const base = (parentPath || '').replace(/\/$/, '');
return `${base}/${menuPath}`.replace(/\/+/g, '/');
}
function toRelativeChildPath(parentAbs, childAbs) {
const base = (parentAbs || '').replace(/\/$/, '');
const target = (childAbs || '').replace(/\/$/, '');
if (!target) return '';
if (target === base) return '';
const prefix = `${base}/`;
if (target.startsWith(prefix)) {
return target.slice(prefix.length);
}
const parts = target.split('/').filter(Boolean);
return parts.length ? parts[parts.length - 1] : '';
}
function hasPageComponent(menu) {
return menu.type === 4 || (menu.component_path && String(menu.component_path).trim() !== '');
}
function resolvePageComponent(menu) {
if (menu.type === 4) {
return () => import('@/views/onepage/index.vue');
}
if (menu.component_path && String(menu.component_path).trim() !== '') {
return createComponentLoader(menu.component_path);
}
return () => import('@/views/404/404.vue');
}
const lockedPageComponent = () => import('@/views/locked/index.vue');
function convertNestedMenuChildren(children, layoutAbsPath) {
if (!children || children.length === 0) return [];
return children.map((child) => nestedMenuToRoute(child, layoutAbsPath));
}
function nestedMenuToRoute(menu, layoutAbsPath) {
const childAbs = computeFullPath(menu.path, layoutAbsPath);
const relPath = toRelativeChildPath(layoutAbsPath, childAbs);
const hasChildren = menu.children && menu.children.length > 0;
const ownPage = hasPageComponent(menu);
const meta = {
title: menu.title,
icon: menu.icon,
id: menu.id,
code: menu.code || menu.module_code,
componentPath: menu.component_path,
};
if (menu.locked) {
const route = {
path: relPath,
name: `menu_${menu.id}`,
meta,
component: lockedPageComponent,
};
if (hasChildren) {
route.children = convertNestedMenuChildren(menu.children, childAbs);
}
return route;
}
if (hasChildren && ownPage) {
return {
path: relPath,
name: `menu_${menu.id}`,
meta,
component: () => import('@/views/layouts/EmptyLayout.vue'),
children: [
{
path: '',
name: `menu_${menu.id}_index`,
meta: { ...meta },
component: resolvePageComponent(menu),
},
...convertNestedMenuChildren(menu.children, childAbs),
],
};
}
if (hasChildren && !ownPage) {
const route = {
path: relPath,
name: `menu_${menu.id}`,
meta,
component: () => import('@/views/layouts/EmptyLayout.vue'),
children: convertNestedMenuChildren(menu.children, childAbs),
};
const firstChild = pickDefaultChild(menu.children);
if (firstChild && firstChild.path) {
const firstAbs = computeFullPath(firstChild.path, childAbs);
const firstRel = toRelativeChildPath(childAbs, firstAbs);
if (firstRel) {
route.redirect = firstRel;
}
}
return route;
}
return {
path: relPath,
name: `menu_${menu.id}`,
meta,
component: resolvePageComponent(menu),
};
}
export function convertMenusToRoutes(menus, parentPath = '') {
if (!menus || menus.length === 0) return [];
return menus.map((menu) => {
const fullPath = menu.path
? menu.path.startsWith('/')
? menu.path.replace(/\/+/g, '/')
: `${(parentPath || '').replace(/\/$/, '')}/${menu.path}`.replace(/\/+/g, '/')
: '';
const hasChildren = menu.children && menu.children.length > 0;
const ownPage = hasPageComponent(menu);
const meta = {
title: menu.title,
icon: menu.icon,
id: menu.id,
code: menu.code || menu.module_code,
componentPath: menu.component_path,
};
if (menu.locked) {
const route = {
path: fullPath || menu.path || '',
name: `menu_${menu.id}`,
meta,
component: lockedPageComponent,
};
if (hasChildren) {
route.children = convertMenusToRoutes(menu.children, fullPath);
}
return route;
}
if (hasChildren && ownPage) {
return {
path: fullPath || menu.path || '',
name: `menu_${menu.id}`,
meta,
component: () => import('@/views/layouts/EmptyLayout.vue'),
children: [
{
path: '',
name: `menu_${menu.id}_index`,
meta: { ...meta },
component: resolvePageComponent(menu),
},
...convertNestedMenuChildren(menu.children, fullPath),
],
};
}
const route = {
path: fullPath || menu.path || '',
name: `menu_${menu.id}`,
meta,
};
if (menu.type === 4) {
route.component = () => import('@/views/onepage/index.vue');
} else if (menu.component_path && menu.component_path.trim() !== '') {
route.component = createComponentLoader(menu.component_path);
} else if (hasChildren) {
route.component = () => import('@/views/layouts/EmptyLayout.vue');
route.children = convertMenusToRoutes(menu.children, fullPath);
const defaultChild =
menu.children.find(
(child) => child.path === '/apps/cms/analytics/content' || child.path === 'analytics/content'
) || pickDefaultChild(menu.children);
if (defaultChild && defaultChild.path) {
const childFullPath = defaultChild.path.startsWith('/')
? defaultChild.path
: `${fullPath}/${defaultChild.path}`;
route.redirect = childFullPath;
}
} else {
route.component = () => import('@/views/404/404.vue');
}
return route;
});
}