/** * 轻应用共享常量 + 运行时 URL 构造(管理页预览 / 列表页跳转 / iframe 宿主共用)。 * * 登录参数运行时从 `localStorage.getItem("userInfo")`(JSON) 解析;主题参数用配置值。 */ import type { LightApp, LightAppRouteParam } from "@/strategy-components/api/light-apps"; /** iframe 可挂载的父菜单(取自 core/page-layout/sidebar-menu.ts 的现有菜单 id)。 */ export const MOUNT_OPTIONS: { label: string; value: string }[] = [ { label: "XK中任务", value: "xk-center-task" }, { label: "COH中任务", value: "coh-center-task" }, { label: "PG中任务", value: "pg-center-task" }, { label: "TY中任务", value: "ty-center-task" }, { label: "资源管理", value: "resource-management" }, ]; export function mountLabel(value: string | null | undefined): string { if (!value) return ""; return MOUNT_OPTIONS.find((o) => o.value === value)?.label ?? value; } /** * 登录参数可选来源;值均可在 userInfo 中取到,"other" 表示自填。 * accessToken / yUserId 取登录路由透传值(写入 userInfo 的同名键); * userName / gateDepartName / typeName 取 consumer 接口 userDetailByYuserId 的返回值。 */ export const LOGIN_SOURCE_OPTIONS: { label: string; value: string }[] = [ { label: "accessToken", value: "accessToken" }, { label: "yUserId", value: "yUserId" }, { label: "userName", value: "userName" }, { label: "gateDepartName", value: "gateDepartName" }, { label: "typeName", value: "typeName" }, { label: "其他(自定义值)", value: "other" }, ]; /** * 我方主题色键。当前启用 red(浅色) / dark-blue(深色);blue 为后续将新增的主题。 * 主题参数的映射以这些键为左侧 key(如 `dark-blue=darkBlue`)。 */ export type AppThemeColor = "red" | "blue" | "dark-blue"; /** 我方全部主题色(「应用管理」主题参数逐项配置目标值时遍历;dot 为配色小圆点)。 */ export const APP_THEME_COLORS: { key: AppThemeColor; label: string; dot: string }[] = [ { key: "red", label: "红色", dot: "#F5222D" }, { key: "blue", label: "蓝色", dot: "#1677FF" }, { key: "dark-blue", label: "深蓝色", dot: "#002B5C" }, ]; /** * 主题键别名:兼容历史/手填里用 light/dark 等写法的映射键,归一到当前主题色键。 * 查映射时按别名顺序尝试,避免「键对不上 → 回退到 red」的恒红 bug。 */ const THEME_KEY_ALIASES: Record = { red: ["red", "light"], blue: ["blue"], "dark-blue": ["dark-blue", "darkblue", "dark"], }; /** * 读取我方当前主题色,以 ThemeContext / next-themes 写在 上的 class 为准 * (含嵌入场景的强制主题)。深色=dark-blue,浅蓝=blue,其余(浅色/红白)=red。 */ export function getCurrentTheme(): AppThemeColor { if (typeof document !== "undefined") { const classList = document.documentElement.classList; if (classList.contains("dark")) return "dark-blue"; if (classList.contains("lightblue")) return "blue"; } return "red"; } /** 解析主题映射串(`red=red;dark-blue=darkBlue`)为 { 主题色键: 目标值 }。值不做 trim,便于表单回显。 */ export function parseThemeMap(raw: string | null | undefined): Record { const map: Record = {}; for (const pair of (raw ?? "").split(/[;,]/)) { const idx = pair.search(/[=:]/); if (idx <= 0) continue; const k = pair.slice(0, idx).trim().toLowerCase(); map[k] = pair.slice(idx + 1); } return map; } /** 把主题映射对象序列化为映射串,按 APP_THEME_COLORS 顺序输出,缺省走同名(identity)。 */ export function stringifyThemeMap(map: Record): string { return APP_THEME_COLORS.map((c) => `${c.key}=${map[c.key] ?? c.key}`).join(";"); } /** * 按我方当前主题,从配置中取目标轻应用对应的值(跳转 / iframe 嵌套时注入)。 * * 配置支持两种写法(管理员在「应用管理」主题参数里配置): * 1) 映射串:`red=red;blue=blue;dark-blue=darkBlue` —— 按当前主题(含别名)取对应值; * 取不到时回退到主题色名本身(identity),而非盲目回退首个值(会恒为 red)。 * 2) 纯值:`red` —— 不区分主题,原样返回。 */ export function resolveThemeParamValue( raw: string | null | undefined, theme: AppThemeColor = getCurrentTheme(), ): string { const value = (raw ?? "").trim(); if (!value) return theme; // 不含键值分隔符 → 纯值,不区分主题原样返回 if (!/[=:]/.test(value)) return value; const map = parseThemeMap(value); for (const key of THEME_KEY_ALIASES[theme] ?? [theme]) { if (map[key] != null) return map[key].trim(); } // 映射里没有当前主题(含别名)→ 退回主题色名本身,避免恒为 red return theme; } /** 读取并解析 localStorage 中的 userInfo。 */ export function readUserInfo(): Record { try { const raw = localStorage.getItem("userInfo"); if (!raw) return {}; const parsed = JSON.parse(raw); return parsed && typeof parsed === "object" ? (parsed as Record) : {}; } catch { return {}; } } /** 解析单个路由参数在运行时的取值。 */ export function resolveRouteParamValue( param: LightAppRouteParam, userInfo: Record = readUserInfo(), theme?: AppThemeColor, ): string { if (param.paramType === "theme") { // 主题参数:按我方当前主题,从管理员配置的映射中取目标应用对应的值。 // theme 显式传入时优先(避免切主题瞬间 DOM 的 dark class 还没更新导致读旧值)。 return resolveThemeParamValue(param.themeValue, theme); } // login if (param.source === "other") { return param.customValue ?? ""; } if (param.source) { const value = userInfo[param.source]; return value == null ? "" : String(value); } return ""; } /** * 构造应用的最终访问 URL:在 `app.url` 上追加解析后的路由参数查询串。 * * 兼容 hash 路由地址(如 `https://x#/route`)—— 管理员粘贴完整地址,参数自动追加在末尾, * 落在 hash 路由的 query 上(与现有 XK/PG 页面 `?accessToken=` 拼接方式一致)。 */ export function buildLightAppUrl( app: Pick, userInfo: Record = readUserInfo(), theme?: AppThemeColor, ): string { const pairs = (app.routeParams ?? []) .filter((p) => p.key) .map((p) => `${encodeURIComponent(p.key)}=${encodeURIComponent(resolveRouteParamValue(p, userInfo, theme))}`); if (pairs.length === 0) return app.url; const sep = app.url.includes("?") ? "&" : "?"; return `${app.url}${sep}${pairs.join("&")}`; }