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

403 lines
11 KiB
JavaScript

import { createRouter, createWebHashHistory } from "vue-router";
import { convertMenusToRoutes } from "./dynamicRoutes";
import { redirectToAuthorize, ensureUserInfo } from "@/utils/authClient";
const LEGACY_LOCAL_LOGIN_PATHS = ["/login", "/register", "/forget"];
const staticMainChildren = [
{
path: "",
name: "Dashboard",
component: () => import("@/views/dashboard/index.vue"),
meta: { requiresAuth: true, title: "工作台" }
},
{
path: "/system/approval",
name: "BackendApprovalFlow",
component: () => import("@/views/system/approval/index.vue"),
meta: { requiresAuth: true, title: "审批流", modulePath: "/systemManager" }
},
{
path: "/apps/erp/dashboard",
name: "ErpWorkbenchDashboard",
component: () => import("@/views/apps/erp/dashboard/index.vue"),
meta: { requiresAuth: true, title: "工作台", modulePath: "/apps/erp" }
},
{
path: "/apps/erp/workbench/approval",
name: "ErpWorkbenchApprovalList",
component: () => import("@/views/apps/erp/workbench/approvalList.vue"),
meta: { requiresAuth: true, title: "审批列表", modulePath: "/apps/erp" }
},
{
path: "/apps/oa/approvalcenter/mine",
name: "OaApprovalCenterMine",
component: () => import("@/views/apps/oa/approvalcenter/mine.vue"),
meta: { requiresAuth: true, title: "待我审批", modulePath: "/apps/oa" }
},
{
path: "/apps/oa/approvalcenter/list",
name: "OaApprovalCenterList",
component: () => import("@/views/apps/oa/approvalcenter/list.vue"),
meta: { requiresAuth: true, title: "审批列表", modulePath: "/apps/oa" }
},
{
path: "/apps/oa/approvalcenter/settings",
name: "OaApprovalCenterSettings",
component: () => import("@/views/apps/oa/approvalcenter/settings.vue"),
meta: { requiresAuth: true, title: "审批设置", modulePath: "/apps/oa" }
},
{
path: "/apps/oa/scheduling/board",
name: "OaSchedulingBoard",
component: () => import("@/views/apps/oa/scheduling/index.vue"),
meta: { requiresAuth: true, title: "工作排班", modulePath: "/apps/oa" }
},
{
path: "/apps/oa/attendance",
name: "OaAttendance",
component: () => import("@/views/apps/oa/attendance/index.vue"),
meta: { requiresAuth: true, title: "考勤管理", modulePath: "/apps/oa" }
},
{
path: "/apps/oa/ai",
name: "OaAiPlayground",
component: () => import("@/views/apps/oa/ai/index.vue"),
meta: { requiresAuth: true, title: "AI办公", modulePath: "/apps/oa" }
},
{
path: "/basic-settings/ai",
name: "BasicSettingsAi",
component: () => import("@/views/basicSettings/ai/index.vue"),
meta: { requiresAuth: true, title: "AI设置", modulePath: "/apps/oa" }
},
{
path: "/apps/cms/analytics/content",
name: "CmsContentAnalytics",
component: () => import("@/views/apps/cms/analytics/content/index.vue"),
meta: { requiresAuth: true, title: "内容统计", modulePath: "/apps/cms" }
},
{
path: "/apps/cms/article",
name: "CmsArticles",
component: () => import("@/views/apps/cms/article/index/index.vue"),
meta: { requiresAuth: true, title: "文章管理", modulePath: "/apps/cms" }
},
{
path: "/apps/cms/article/type",
name: "CmsArticleCategories",
component: () => import("@/views/apps/cms/article/type/index.vue"),
meta: { requiresAuth: true, title: "文章分类", modulePath: "/apps/cms" }
},
{
path: "/apps/cms/products",
name: "CmsProducts",
component: () => import("@/views/apps/cms/product/index/index.vue"),
meta: { requiresAuth: true, title: "产品管理", modulePath: "/apps/cms" }
},
{
path: "/apps/cms/products/types",
name: "CmsProductCategories",
component: () => import("@/views/apps/cms/product/type/index.vue"),
meta: { requiresAuth: true, title: "产品分类", modulePath: "/apps/cms" }
},
{
path: "/apps/cms/solutions",
name: "CmsSolutions",
component: () => import("@/views/apps/cms/solution/index/index.vue"),
meta: { requiresAuth: true, title: "解决方案管理", modulePath: "/apps/cms" }
},
{
path: "/apps/cms/solutions/types",
name: "CmsSolutionCategories",
component: () => import("@/views/apps/cms/solution/type/index.vue"),
meta: { requiresAuth: true, title: "解决方案分类", modulePath: "/apps/cms" }
},
{
path: "/apps/crm/product",
name: "CrmProduct",
component: () => import("@/views/apps/crm/product/index.vue"),
meta: { requiresAuth: true, title: "产品管理", modulePath: "/apps/crm" }
},
{
path: "/apps/cms/articles",
redirect: "/apps/cms/article"
},
{
path: "/apps/cms/articles/category",
redirect: "/apps/cms/article/type"
},
{
path: "/apps/crm/product/cate",
name: "CrmProductCategory",
component: () => import("@/views/apps/crm/product/cate.vue"),
meta: { requiresAuth: true, title: "产品分类", modulePath: "/apps/crm" }
},
{
path: "/apps/crm/product/cate.vue",
redirect: "/apps/crm/product/cate"
},
{
path: "/apps/crm/setting",
name: "CrmSetting",
component: () => import("@/views/apps/crm/setting/index.vue"),
meta: { requiresAuth: true, title: "基本设置", modulePath: "/apps/crm" }
},
{
path: "/apps/crm/setting/unit",
name: "CrmSettingUnit",
component: () => import("@/views/apps/crm/setting/unit/index.vue"),
meta: { requiresAuth: true, title: "单位设置", modulePath: "/apps/crm" }
},
{
path: "/apps/crm/setting/unit.vue",
redirect: "/apps/crm/setting/unit"
},
{
path: "/user/userProfile",
name: "userProfile",
component: () => import("@/views/user/userProfile.vue"),
meta: { requiresAuth: true, title: "用户中心" }
},
{
path: "/user/notifyConfig",
name: "notifyConfig",
component: () => import("@/views/user/notifyConfig.vue"),
meta: { requiresAuth: true, title: "通知设置" }
},
{
path: "/apps/ai",
name: "aiSquare",
component: () => import("@/views/apps/ai/index.vue"),
meta: { requiresAuth: true, title: "AI广场" }
},
{
path: "/apps/erp/dashborad",
redirect: "/apps/erp/dashboard"
}
];
const staticRoutes = [
{
path: "/auth/callback",
name: "AuthCallback",
component: () => import("@/views/auth/callback.vue"),
meta: { requiresAuth: false }
},
{
path: "/mp-login",
name: "MpLogin",
component: () => import("@/views/auth/mpLogin.vue"),
meta: { requiresAuth: false }
},
{
path: "/app-login",
name: "AppLogin",
component: () => import("@/views/auth/appLogin.vue"),
meta: { requiresAuth: false }
},
{
path: "/home",
name: "Home",
component: () => import("@/views/home/index.vue"),
meta: { requiresAuth: true, title: "系统导航", isStandalone: true }
},
{
path: "/apps/erp/dashborad",
redirect: "/apps/erp/dashboard"
},
{
path: "/product/:code",
name: "ProductPurchase",
component: () => import("@/views/product/index.vue"),
meta: { requiresAuth: true, title: "产品购买", isStandalone: true }
},
{
path: "/packages",
name: "PackageList",
component: () => import("@/views/package/list.vue"),
meta: { requiresAuth: true, title: "套餐购买", isStandalone: true }
},
{
path: "/:pathMatch(.*)*",
name: "NotFound",
component: () => import("@/views/404/404.vue"),
meta: { requiresAuth: false }
}
];
const router = createRouter({
history: createWebHashHistory(),
routes: staticRoutes
});
let dynamicRoutesAdded = false;
let dynamicRoutesData = [];
let routesLoadingPromise = null;
export function resetDynamicRoutes() {
dynamicRoutesAdded = false;
routesLoadingPromise = null;
}
export async function loadAndAddDynamicRoutes() {
if (routesLoadingPromise) {
return routesLoadingPromise;
}
if (dynamicRoutesAdded) {
return Promise.resolve();
}
routesLoadingPromise = (async () => {
try {
const { useMenuStore } = await import("@/stores/menu");
const menuStore = useMenuStore();
const menuData = await menuStore.fetchMenus();
if (menuData && menuData.length > 0) {
addDynamicRoutes(menuData);
dynamicRoutesAdded = true;
routesLoadingPromise = null;
return Promise.resolve();
} else {
addDynamicRoutes([]);
dynamicRoutesAdded = true;
routesLoadingPromise = null;
return Promise.resolve();
}
} catch (error) {
console.error('加载动态路由失败,已保留内置路由:', error);
dynamicRoutesAdded = true;
routesLoadingPromise = null;
return Promise.resolve();
}
})();
return routesLoadingPromise;
}
export async function reloadMenusAndDynamicRoutes() {
const { useMenuStore } = await import("@/stores/menu");
const menuStore = useMenuStore();
await menuStore.refreshMenus();
resetDynamicRoutes();
await loadAndAddDynamicRoutes();
}
function addDynamicRoutes(menus) {
if (!menus?.length) {
return;
}
const dynamicRoutes = convertMenusToRoutes(menus);
if (router.hasRoute('Main')) {
router.removeRoute('Main');
}
router.addRoute({
path: "/",
name: "Main",
component: () => import("@/views/Main.vue"),
meta: { requiresAuth: true },
children: [...staticMainChildren, ...dynamicRoutes]
});
dynamicRoutesAdded = true;
}
function findRouteByName(routes, routeName) {
for (const route of routes) {
if (route.name === routeName) {
return route;
}
if (route.children) {
const found = findRouteByName(route.children, routeName);
if (found) {
return found;
}
}
}
return null;
}
function findFirstValidRoute(routes) {
for (const route of routes) {
if (route.component) {
return route;
}
if (route.children && route.children.length > 0) {
const childRoute = findFirstValidRoute(route.children);
if (childRoute) {
return childRoute;
}
}
}
return null;
}
async function gotoAuthorize() {
try {
await redirectToAuthorize();
return true;
} catch (e) {
console.error(e);
window.alert(e?.message || "统一认证跳转失败,请检查认证中心配置");
return false;
}
}
router.beforeEach(async (to, from, next) => {
const token = localStorage.getItem("token");
if (
to.path === "/auth/callback" ||
to.path === "/mp-login" ||
to.path === "/app-login"
) {
next();
return;
}
if (LEGACY_LOCAL_LOGIN_PATHS.includes(to.path)) {
if (token) {
if (!dynamicRoutesAdded) {
await loadAndAddDynamicRoutes();
}
next({ path: "/home" });
} else {
const ok = await gotoAuthorize();
if (!ok) next(false);
}
return;
}
if (!token) {
const ok = await gotoAuthorize();
if (!ok) next(false);
return;
}
const info = await ensureUserInfo();
if (!info) {
if (localStorage.getItem("token")) {
next();
return;
}
const ok = await gotoAuthorize();
if (!ok) next(false);
return;
}
if (!dynamicRoutesAdded) {
await loadAndAddDynamicRoutes();
next({ path: to.path, replace: true });
return;
}
next();
});
export default router;