Commit 2d4b771a by 林磊

Merge branch 'feat_micro_build' into 'dev'

feat: 微前端项目打包构建配置修改

See merge request !10
2 parents aba1cf3b 642400f0
// 此文件由 auto-routes 插件自动生成,请勿手动编辑 // 此文件由 auto-routes 插件自动生成,请勿手动编辑
// 如需自定义路由,请在 vite.config.ts 中配置 customRules // 如需自定义路由,请在 vite.config.ts 中配置 customRules
// 生成时间: 2026/1/22 14:14:23 // 生成时间: 2026/1/23 10:58:16
import type { RouteRecordRaw } from 'vue-router'; import type { RouteRecordRaw } from 'vue-router';
...@@ -10,7 +10,7 @@ export const autoRoutes: RouteRecordRaw[] = [ ...@@ -10,7 +10,7 @@ export const autoRoutes: RouteRecordRaw[] = [
id: "assistant", id: "assistant",
title: "Assistant", title: "Assistant",
icon: "", icon: "",
level: 0, level: 1,
hidden: false, hidden: false,
meta: { meta: {
"authority": "assistant", "authority": "assistant",
...@@ -41,7 +41,7 @@ export const autoRoutes: RouteRecordRaw[] = [ ...@@ -41,7 +41,7 @@ export const autoRoutes: RouteRecordRaw[] = [
id: "manage-agentic", id: "manage-agentic",
title: "ManageAgentic", title: "ManageAgentic",
icon: "", icon: "",
level: 0, level: 1,
hidden: false, hidden: false,
meta: { meta: {
"authority": "manage-agentic", "authority": "manage-agentic",
...@@ -55,7 +55,7 @@ export const autoRoutes: RouteRecordRaw[] = [ ...@@ -55,7 +55,7 @@ export const autoRoutes: RouteRecordRaw[] = [
id: "manage-konw", id: "manage-konw",
title: "ManageKonw", title: "ManageKonw",
icon: "", icon: "",
level: 0, level: 1,
hidden: false, hidden: false,
meta: { meta: {
"authority": "manage-konw", "authority": "manage-konw",
......
import type { Router, RouteRecordRaw } from 'vue-router'; import type { Router, RouteRecordRaw, RouterOptions } from 'vue-router';
import { createRouter as _createRouter, createWebHistory } from 'vue-router'; import { createRouter as _createRouter, createWebHistory } from 'vue-router';
// 导入自动生成的路由 // 手动配置的路由
// 注意:使用动态导入以避免构建时错误 // 注意:auto-routes.ts 文件仅作为示例,不在项目中使用
let autoRoutes: RouteRecordRaw[] = []; const routes: RouteRecordRaw[] = [
// 使用同步方式导入(在构建时生成的文件)
// 由于项目使用 ES 模块,这里使用条件导入
try {
// @ts-ignore - 动态导入可能不存在,使用同步导入
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
const autoRoutesModule = require('./auto-routes');
autoRoutes = autoRoutesModule.autoRoutes || [];
} catch (error) {
// 如果自动生成的路由文件不存在,使用手动配置的路由
console.warn('[router] 自动路由文件不存在,使用手动配置的路由');
// 手动配置的路由(作为后备方案)
autoRoutes = [
{ {
path: '/', path: '/',
name: 'Home', name: 'Home',
...@@ -96,31 +82,10 @@ try { ...@@ -96,31 +82,10 @@ try {
name: 'NotFound', name: 'NotFound',
component: () => import('@/views/NotFound.vue'), component: () => import('@/views/NotFound.vue'),
}, },
];
}
// 手动配置的路由(用于覆盖或补充自动生成的路由)
// 手动配置的路由优先级更高,会覆盖自动生成的路由
const manualRoutes: RouteRecordRaw[] = [
// 可以在这里添加需要手动配置的特殊路由
// 例如:需要特殊权限、动态参数等的路由
// {
// path: '/custom',
// name: 'Custom',
// component: () => import('@/views/Custom.vue'),
// meta: {
// title: '自定义路由',
// },
// },
]; ];
// 合并路由:手动配置的路由优先 // 转换为 RouterOptions 需要的类型
const routes: RouteRecordRaw[] = [ const routerRoutes = routes as unknown as RouterOptions['routes'];
...manualRoutes,
...autoRoutes.filter(
(autoRoute) => !manualRoutes.some((manualRoute) => manualRoute.path === autoRoute.path)
),
];
/** /**
* 创建路由实例 * 创建路由实例
...@@ -129,7 +94,7 @@ const routes: RouteRecordRaw[] = [ ...@@ -129,7 +94,7 @@ const routes: RouteRecordRaw[] = [
export function createRouter(base: string = '/'): Router { export function createRouter(base: string = '/'): Router {
const router = _createRouter({ const router = _createRouter({
history: createWebHistory(base), history: createWebHistory(base),
routes, routes: routerRoutes,
}); });
// 路由守卫 // 路由守卫
......
...@@ -31,7 +31,6 @@ ...@@ -31,7 +31,6 @@
"include": [ "include": [
"env.d.ts", "env.d.ts",
"src/**/*.ts", "src/**/*.ts",
"src/**/*.d.ts",
"src/**/*.tsx", "src/**/*.tsx",
"src/**/*.vue", "src/**/*.vue",
"src/**/*.js" "src/**/*.js"
......
import type { RouteRecordRaw } from 'vue-router'; import type { MenuConfig } from "@/api/route";
import { createRouter, createWebHistory } from 'vue-router';
import { ElMessage } from 'element-plus'; import type { RouteRecordRaw } from "vue-router";
import { transformMenuConfig, type MenuConfig } from '@/api/route';
import { fetchMicroAppRouteConfig } from '../config/center'; import { createRouter, createWebHistory } from "vue-router";
import { getMicroAppByPath } from '@/qiankun/apps';
import { useAppStore, useMenuStore, useUserStore } from '@/stores'; import { transformMenuConfig } from "@/api/route";
import { getToken } from '@/utils/auth'; import { isRouteInWhitelist } from "@/config/permission";
import { waitForMicroAppContainer } from '@/utils/microapp'; import { getMicroAppByPath } from "@/qiankun/apps";
import { isRouteInWhitelist } from '@/config/permission'; import { useAppStore, useMenuStore, useUserStore } from "@/stores";
import { getToken } from "@/utils/auth";
import { waitForMicroAppContainer } from "@/utils/microapp";
import { ElMessage } from "element-plus";
import { fetchMicroAppRouteConfig } from "../config/center";
/** /**
* 路由配置 * 路由配置
...@@ -16,43 +21,43 @@ import { isRouteInWhitelist } from '@/config/permission'; ...@@ -16,43 +21,43 @@ import { isRouteInWhitelist } from '@/config/permission';
*/ */
const routes: RouteRecordRaw[] = [ const routes: RouteRecordRaw[] = [
{ {
path: '/login', path: "/login",
name: 'Login', name: "Login",
component: () => import('@/views/Login.vue'), component: () => import("@/views/Login.vue"),
meta: { meta: {
title: '登录', title: "登录",
requiresAuth: false, // 不需要认证 requiresAuth: false, // 不需要认证
}, },
}, },
// ==================== 主应用布局 ==================== // ==================== 主应用布局 ====================
{ {
path: '/', path: "/",
name: 'Layout', name: "Layout",
component: () => import('@/layouts/BasicLayout.vue'), component: () => import("@/layouts/BasicLayout.vue"),
redirect: '/home', redirect: "/home",
meta: { meta: {
requiresAuth: true, // 需要认证 requiresAuth: true, // 需要认证
}, },
children: [ children: [
// ==================== 首页模块 ==================== // ==================== 首页模块 ====================
{ {
path: '/home', path: "/home",
name: 'Home', name: "Home",
component: () => import('@/views/Home.vue'), component: () => import("@/views/Home.vue"),
redirect: '/assisant/assistant', redirect: "/assisant/assistant",
meta: { meta: {
title: '首页', title: "首页",
level: 1, level: 1,
requiresAuth: true, requiresAuth: true,
}, },
children: [ children: [
{ {
path: 'dashboard', path: "dashboard",
name: 'Dashboard', name: "Dashboard",
component: () => import('@/views/Home.vue'), component: () => import("@/views/Home.vue"),
meta: { meta: {
title: '工作台', title: "工作台",
level: 2, level: 2,
requiresAuth: true, requiresAuth: true,
}, },
...@@ -61,21 +66,21 @@ const routes: RouteRecordRaw[] = [ ...@@ -61,21 +66,21 @@ const routes: RouteRecordRaw[] = [
}, },
// ==================== 403 页面 ==================== // ==================== 403 页面 ====================
{ {
path: '/403', path: "/403",
name: 'Forbidden', name: "Forbidden",
component: () => import('@/views/Forbidden.vue'), component: () => import("@/views/Forbidden.vue"),
meta: { meta: {
title: '无权限访问', title: "无权限访问",
requiresAuth: false, requiresAuth: false,
}, },
}, },
// ==================== 404 页面 ==================== // ==================== 404 页面 ====================
{ {
path: '/:pathMatch(.*)*', path: "/:pathMatch(.*)*",
name: 'NotFound', name: "NotFound",
component: () => import('@/views/NotFound.vue'), component: () => import("@/views/NotFound.vue"),
meta: { meta: {
title: '', title: "",
requiresAuth: false, requiresAuth: false,
}, },
}, },
...@@ -85,7 +90,7 @@ const routes: RouteRecordRaw[] = [ ...@@ -85,7 +90,7 @@ const routes: RouteRecordRaw[] = [
const router = createRouter({ const router = createRouter({
history: createWebHistory(), history: createWebHistory(),
routes, routes: routes as readonly RouteRecordRaw[],
}); });
/** /**
...@@ -116,7 +121,7 @@ export function addRoutes(routes: RouteRecordRaw[], parentName?: string) { ...@@ -116,7 +121,7 @@ export function addRoutes(routes: RouteRecordRaw[], parentName?: string) {
} }
// 登录页路径 // 登录页路径
const LOGIN_PATH = '/login'; const LOGIN_PATH = "/login";
// 标记是否正在加载动态路由 // 标记是否正在加载动态路由
let isLoadingRoutes = false; let isLoadingRoutes = false;
...@@ -142,7 +147,7 @@ export async function loadDynamicRoutes() { ...@@ -142,7 +147,7 @@ export async function loadDynamicRoutes() {
const routerData: MenuConfig = { const routerData: MenuConfig = {
topMenus: [], topMenus: [],
routes: [] routes: []
} };
response.data.microApps.forEach((item: { routerList: any; routes: any; }) => { response.data.microApps.forEach((item: { routerList: any; routes: any; }) => {
routerData.topMenus.push(item.routerList as any); routerData.topMenus.push(item.routerList as any);
routerData.routes.push(item.routes as any); routerData.routes.push(item.routes as any);
...@@ -155,13 +160,13 @@ export async function loadDynamicRoutes() { ...@@ -155,13 +160,13 @@ export async function loadDynamicRoutes() {
// 动态添加路由到 Layout 的 children 中 // 动态添加路由到 Layout 的 children 中
if (routes.length > 0) { if (routes.length > 0) {
addRoutes(routes, 'Layout') addRoutes(routes, "Layout");
menuStore.addDynamicRoutes(routes); menuStore.addDynamicRoutes(routes);
} }
} }
} catch (error) { } catch (error) {
console.error( console.error(
'[路由守卫] 加载动态路由和菜单配置失败将使用默认菜单配置:', "[路由守卫] 加载动态路由和菜单配置失败将使用默认菜单配置:",
error, error,
); );
} finally { } finally {
...@@ -190,7 +195,7 @@ async function waitForRoutesAndRematch(targetPath: string): Promise<boolean> { ...@@ -190,7 +195,7 @@ async function waitForRoutesAndRematch(targetPath: string): Promise<boolean> {
// 如果已经加载过,直接检查路由是否匹配,不再重复加载 // 如果已经加载过,直接检查路由是否匹配,不再重复加载
if (menuStore.isMenusLoaded) { if (menuStore.isMenusLoaded) {
const matchedRoutes = router.resolve(targetPath); const matchedRoutes = router.resolve(targetPath);
return matchedRoutes.name !== 'NotFound'; return matchedRoutes.name !== "NotFound";
} }
const appStore = useAppStore(); const appStore = useAppStore();
...@@ -206,11 +211,11 @@ async function waitForRoutesAndRematch(targetPath: string): Promise<boolean> { ...@@ -206,11 +211,11 @@ async function waitForRoutesAndRematch(targetPath: string): Promise<boolean> {
// 检查路由是否已存在 // 检查路由是否已存在
const matchedRoutes = router.resolve(targetPath); const matchedRoutes = router.resolve(targetPath);
const isMatched = matchedRoutes.name !== 'NotFound'; const isMatched = matchedRoutes.name !== "NotFound";
return isMatched; return isMatched;
} catch (error) { } catch (error) {
console.error('[路由守卫] 等待动态路由加载失败:', error); console.error("[路由守卫] 等待动态路由加载失败:", error);
return false; return false;
} finally { } finally {
// 隐藏 loading // 隐藏 loading
...@@ -225,7 +230,7 @@ router.beforeEach(async (to, _from, next) => { ...@@ -225,7 +230,7 @@ router.beforeEach(async (to, _from, next) => {
// 设置页面标题 // 设置页面标题
document.title = to.meta.title document.title = to.meta.title
? `${to.meta.title} - 汽车销售超级孪生智能体` ? `${to.meta.title} - 汽车销售超级孪生智能体`
: '汽车销售超级孪生智能体'; : "汽车销售超级孪生智能体";
// 获取 token // 获取 token
const token = getToken(); const token = getToken();
...@@ -262,11 +267,11 @@ router.beforeEach(async (to, _from, next) => { ...@@ -262,11 +267,11 @@ router.beforeEach(async (to, _from, next) => {
} }
// 没有访问权限,弹框提示后跳转登录页面 // 没有访问权限,弹框提示后跳转登录页面
ElMessage.warning('登录已过期,请重新登录'); ElMessage.warning("登录已过期,请重新登录");
next({ next({
path: LOGIN_PATH, path: LOGIN_PATH,
query: query:
to.fullPath === '/home' || to.fullPath === '/' to.fullPath === "/home" || to.fullPath === "/"
? {} ? {}
: { redirect: encodeURIComponent(to.fullPath) }, : { redirect: encodeURIComponent(to.fullPath) },
replace: true, replace: true,
...@@ -275,7 +280,7 @@ router.beforeEach(async (to, _from, next) => { ...@@ -275,7 +280,7 @@ router.beforeEach(async (to, _from, next) => {
} }
// 如果匹配到404路由,检查是否是子应用路由 // 如果匹配到404路由,检查是否是子应用路由
if (to.name === 'NotFound') { if (to.name === "NotFound") {
const menuStore = useMenuStore(); const menuStore = useMenuStore();
// 优化:如果动态路由还未加载,才进行检查和加载 // 优化:如果动态路由还未加载,才进行检查和加载
...@@ -285,7 +290,7 @@ router.beforeEach(async (to, _from, next) => { ...@@ -285,7 +290,7 @@ router.beforeEach(async (to, _from, next) => {
if (isMicroAppPath) { if (isMicroAppPath) {
console.log( console.log(
'[路由守卫] 检测到可能的子应用路由,等待动态路由加载:', "[路由守卫] 检测到可能的子应用路由,等待动态路由加载:",
to.path, to.path,
); );
...@@ -307,7 +312,7 @@ router.beforeEach(async (to, _from, next) => { ...@@ -307,7 +312,7 @@ router.beforeEach(async (to, _from, next) => {
// ==================== 已登录状态处理 ==================== // ==================== 已登录状态处理 ====================
// 如果已登录,访问登录页则跳转到首页 // 如果已登录,访问登录页则跳转到首页
if (to.path === LOGIN_PATH) { if (to.path === LOGIN_PATH) {
next({ path: '/home' }); next({ path: "/home" });
return; return;
} }
...@@ -325,8 +330,8 @@ router.beforeEach(async (to, _from, next) => { ...@@ -325,8 +330,8 @@ router.beforeEach(async (to, _from, next) => {
next(); next();
} else { } else {
// 无权限,跳转到 403 页面 // 无权限,跳转到 403 页面
ElMessage.error('无权限访问该页面'); ElMessage.error("无权限访问该页面");
next('/403'); next("/403");
} }
} else { } else {
// 如果是微应用路由,确保数据已同步并且容器已就绪 // 如果是微应用路由,确保数据已同步并且容器已就绪
...@@ -335,7 +340,7 @@ router.beforeEach(async (to, _from, next) => { ...@@ -335,7 +340,7 @@ router.beforeEach(async (to, _from, next) => {
// 等待容器就绪 // 等待容器就绪
const isContainerReady = await waitForMicroAppContainer(); const isContainerReady = await waitForMicroAppContainer();
if (!isContainerReady) { if (!isContainerReady) {
console.error('[路由守卫] 微应用容器未就绪,跳转可能失败'); console.error("[路由守卫] 微应用容器未就绪,跳转可能失败");
} }
} }
next(); next();
...@@ -354,14 +359,14 @@ router.beforeEach(async (to, _from, next) => { ...@@ -354,14 +359,14 @@ router.beforeEach(async (to, _from, next) => {
// 等待容器就绪 // 等待容器就绪
const isContainerReady = await waitForMicroAppContainer(); const isContainerReady = await waitForMicroAppContainer();
if (!isContainerReady) { if (!isContainerReady) {
console.error('[路由守卫] 微应用容器未就绪,跳转可能失败'); console.error("[路由守卫] 微应用容器未就绪,跳转可能失败");
} }
} }
next(); next();
} catch { } catch {
// 获取用户信息失败,清除 token 并跳转到登录页 // 获取用户信息失败,清除 token 并跳转到登录页
ElMessage.warning('获取用户信息失败,请重新登录'); ElMessage.warning("获取用户信息失败,请重新登录");
await userStore.logout(); await userStore.logout();
next({ next({
path: LOGIN_PATH, path: LOGIN_PATH,
...@@ -375,7 +380,7 @@ router.beforeEach(async (to, _from, next) => { ...@@ -375,7 +380,7 @@ router.beforeEach(async (to, _from, next) => {
* 全局后置守卫 * 全局后置守卫
*/ */
router.afterEach((to, from) => { router.afterEach((to, from) => {
console.log('[路由] afterEach', to, from); console.log("[路由] afterEach", to, from);
// 路由跳转完成后的处理 // 路由跳转完成后的处理
console.log(`[路由] 从 ${from.path} 跳转到 ${to.path}`); console.log(`[路由] 从 ${from.path} 跳转到 ${to.path}`);
}); });
...@@ -393,7 +398,7 @@ async function syncDataToQiankun(userStore: any): Promise<void> { ...@@ -393,7 +398,7 @@ async function syncDataToQiankun(userStore: any): Promise<void> {
} }
// 延迟导入 qiankun 通信模块 // 延迟导入 qiankun 通信模块
const { communicationManager } = await import('@/qiankun/communication'); const { communicationManager } = await import("@/qiankun/communication");
// 获取当前全局状态,确认数据已同步 // 获取当前全局状态,确认数据已同步
const globalState = communicationManager.getGlobalState(); const globalState = communicationManager.getGlobalState();
...@@ -403,13 +408,13 @@ async function syncDataToQiankun(userStore: any): Promise<void> { ...@@ -403,13 +408,13 @@ async function syncDataToQiankun(userStore: any): Promise<void> {
communicationManager.updateUser({ communicationManager.updateUser({
id: userStore.userInfo.id, id: userStore.userInfo.id,
name: userStore.userInfo.name, name: userStore.userInfo.name,
avatar: userStore.userInfo.avatar || '👨‍💼', avatar: userStore.userInfo.avatar || "👨‍💼",
role: userStore.roles[0] || 'user', role: userStore.roles[0] || "user",
permissions: userStore.permissions, permissions: userStore.permissions,
}); });
} }
} catch (error) { } catch (error) {
console.error('[路由守卫] 同步数据到 qiankun 失败:', error); console.error("[路由守卫] 同步数据到 qiankun 失败:", error);
} }
} }
......
...@@ -35,7 +35,6 @@ ...@@ -35,7 +35,6 @@
"include": [ "include": [
"env.d.ts", "env.d.ts",
"src/**/*.ts", "src/**/*.ts",
"src/**/*.d.ts",
"src/**/*.tsx", "src/**/*.tsx",
"src/**/*.vue", "src/**/*.vue",
"src/**/*.js" "src/**/*.js"
......
...@@ -10,5 +10,5 @@ ...@@ -10,5 +10,5 @@
"allowSyntheticDefaultImports": true, "allowSyntheticDefaultImports": true,
"skipLibCheck": true "skipLibCheck": true
}, },
"include": ["vite.config.ts", "src/**/*.ts", "src/**/*.tsx", "src/**/*.vue"] "include": ["vite.config.ts"]
} }
...@@ -27,7 +27,6 @@ ...@@ -27,7 +27,6 @@
}, },
"include": [ "include": [
"src/**/*.ts", "src/**/*.ts",
"src/**/*.d.ts",
"src/**/*.tsx", "src/**/*.tsx",
"src/**/*.vue", "src/**/*.vue",
"src/**/*.js" "src/**/*.js"
......
...@@ -27,5 +27,5 @@ ...@@ -27,5 +27,5 @@
"skipLibCheck": true "skipLibCheck": true
}, },
"references": [{ "path": "./tsconfig.node.json" }], "references": [{ "path": "./tsconfig.node.json" }],
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue", "env.d.ts"] "include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue", "env.d.ts"]
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!