web iframe结构实现

This commit is contained in:
2025-12-13 14:13:31 +08:00
parent e002f0d989
commit 1776aa2d1e
53 changed files with 3280 additions and 275 deletions

View File

@@ -18,43 +18,47 @@ import {
import type { TbSysViewDTO } from 'shared/types'
import type { RouteRecordRaw } from 'vue-router'
import router from './index'
import { SidebarLayout } from '../layouts'
import { SidebarLayout, BlankLayout } from '@/layouts'
// Platform 布局组件映射
const platformLayoutMap: Record<string, () => Promise<any>> = {
'SidebarLayout': () => Promise.resolve({ default: SidebarLayout }),
'BlankLayout': () => Promise.resolve({ default: BlankLayout }),
'NavigationLayout': () => Promise.resolve({ default: SidebarLayout }),
'BasicLayout': () => Promise.resolve({ default: SidebarLayout })
}
// 视图组件加载器
const VIEW_MODULES = import.meta.glob<{ default: any }>('../views/**/*.vue')
const VIEW_MODULES = import.meta.glob<{ default: any }>('@/views/**/*.vue')
/**
* 视图组件加载函数
* @param componentPath 组件路径
* @param componentPath 组件路径(如 "public/Chat/AIChatView.vue"
*/
function viewLoader(componentPath: string): (() => Promise<any>) | null {
// 将后台路径转换为实际路径
// 将后台路径转换为 @/views 格式
let path = componentPath
// 如果不是以 ../ 开头,则认为是相对 views 目录的路径
if (!path.startsWith('../')) {
if (!path.startsWith('/')) {
path = '/' + path
}
path = '../views' + path
// 移除开头的斜杠(如果有)
if (path.startsWith('/')) {
path = path.substring(1)
}
// 补全 .vue 后缀
// 补全 .vue 后缀(如果没有)
if (!path.endsWith('.vue')) {
path += '.vue'
}
const loader = VIEW_MODULES[path]
// 转换为 /src/views 格式(匹配 import.meta.glob 的 key
const fullPath = `/src/views/${path}`
console.log('[Platform viewLoader] 尝试加载组件:', componentPath, '→', fullPath)
const loader = VIEW_MODULES[fullPath]
if (!loader) {
console.warn(`[路由生成] 未找到组件: ${componentPath},期望路径: ${path}`)
console.warn('[Platform viewLoader] 组件未找到:', fullPath)
console.log('[Platform viewLoader] 可用的组件:', Object.keys(VIEW_MODULES))
return null
}
@@ -74,12 +78,8 @@ const routeConfig: RouteGeneratorConfig = {
// Platform 路由生成选项
const routeOptions: GenerateSimpleRoutesOptions = {
asRootChildren: true, // 作为 Root 路由的子路由
iframePlaceholder: () => Promise.resolve({
default: {
template: '<div class="iframe-placeholder"></div>'
}
}),
asRootChildren: false, // 直接作为根级路由,不是某个布局的子路由
iframePlaceholder: () => import('shared/components').then(m => ({ default: m.IframeView })),
verbose: true // 启用详细日志
}
@@ -89,31 +89,34 @@ const routeOptions: GenerateSimpleRoutesOptions = {
*/
export function addDynamicRoutes(views: TbSysViewDTO[]) {
if (!views || views.length === 0) {
console.warn('[Platform 路由] 视图列表为空')
console.warn('[Platform] addDynamicRoutes: 视图列表为空')
return
}
console.log('[Platform 路由] 开始生成路由,视图数量:', views.length)
console.log('[Platform] addDynamicRoutes: 开始添加动态路由,视图数量:', views.length)
try {
// 使用 shared 中的通用方法生成路由
const routes = generateSimpleRoutes(views, routeConfig, routeOptions)
// 将生成的路由添加到 Platform 的 router
// 直接将路由添加到根级别不是作为Root的children
routes.forEach(route => {
router.addRoute('Root', route)
console.log('[Platform 路由] 已添加路由:', {
path: route.path,
name: route.name,
hasComponent: !!route.component,
childrenCount: route.children?.length || 0
})
console.log('[Platform] addDynamicRoutes: 添加路由', route.path, '使用布局:', route.component?.name || 'unknown')
router.addRoute(route)
})
console.log('✅ Platform 动态路由添加完成')
console.log('所有路由:', router.getRoutes().map(r => ({ path: r.path, name: r.name })))
// 动态添加根路径重定向到第一个菜单项
if (routes.length > 0) {
const firstRoute = routes[0]
router.addRoute({
path: '/',
redirect: firstRoute.path
})
console.log('[Platform] addDynamicRoutes: 添加根路径重定向到', firstRoute.path)
}
} catch (error) {
console.error('Platform 动态路由生成失败:', error)
console.error('[Platform] addDynamicRoutes: 添加路由失败', error)
throw error
}
}
@@ -130,18 +133,35 @@ export function addDynamicRoutes(views: TbSysViewDTO[]) {
*/
export function loadRoutesFromStorage(): boolean {
try {
// 使用 shared 中的通用方法加载视图数据
const views = loadViewsFromStorage('loginDomain', 'userViews')
console.log('[Platform] loadRoutesFromStorage: 开始加载动态路由')
if (views) {
// 使用 shared 中的通用方法加载视图数据
const allViews = loadViewsFromStorage('loginDomain', 'userViews')
console.log('[Platform] loadRoutesFromStorage: 加载的所有视图数量:', allViews?.length || 0)
if (allViews) {
// 过滤出 platform 服务的视图
const platformViews = allViews.filter((view: TbSysViewDTO) =>
view.service === 'platform'
)
console.log('[Platform] loadRoutesFromStorage: 过滤后的 platform 视图:', platformViews)
if (platformViews.length === 0) {
console.warn('[Platform] loadRoutesFromStorage: 没有找到 platform 服务的视图')
return false
}
// 使用 Platform 的 addDynamicRoutes 添加路由
addDynamicRoutes(views)
addDynamicRoutes(platformViews)
return true
}
console.warn('[Platform] loadRoutesFromStorage: 未能加载视图数据')
return false
} catch (error) {
console.error('[Platform 路由] 从 LocalStorage 加载路由失败:', error)
console.error('[Platform] loadRoutesFromStorage: 加载路由失败', error)
return false
}
}

View File

@@ -1,15 +1,9 @@
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { SidebarLayout } from '../layouts'
import { TokenManager } from 'shared/api'
import { loadRoutesFromStorage } from './dynamicRoute'
// platform应用的动态路由会根据layout字段自动添加不需要预定义Root布局
const routes: RouteRecordRaw[] = [
{
path: '/',
name: 'Root',
component: SidebarLayout,
children: []
},
{
path: '/login',
name: 'Login',