From 3cc062aa3b7970a05574d844d394a80130eff621 Mon Sep 17 00:00:00 2001 From: zhanjunhao <642307404@qq.com> Date: Thu, 19 Nov 2026 10:59:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=81=A2=E5=A4=8D=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E7=9B=91=E6=8E=A7=E8=8F=9C=E5=8D=95=E5=85=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/CHANGELOG.md | 12 +- docs/ui/SERVICE_DASHBOARD_SPEC.md | 4 +- src/helpers/navigationVisibility.js | 6 + src/pages/UserCenter/UserCenterSidebar.jsx | 56 +----- src/pages/UserCenter/index.jsx | 13 +- .../unit/service-dashboard-navigation.test.js | 182 ++++++++++++++++++ 6 files changed, 219 insertions(+), 54 deletions(-) create mode 100644 test/unit/service-dashboard-navigation.test.js diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 0164305c..da5f6975 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,6 +1,6 @@ # 变更记录(CHANGELOG) -> 状态:有效(持续追加) · 更新:2026-11-18 · 版本:v3.5 +> 状态:有效(持续追加) · 更新:2026-11-19 · 版本:v3.6 对现状仍有影响的功能与修复记录,按日期倒序。改造、迁移、优化等一次性过程不在此记录。 @@ -8,6 +8,16 @@ **记录规则**:每条写“范围、变更、影响、回滚、关联”五项;回滚优先 `git revert <提交>`,提交含多项改动时按文件还原(`git checkout <提交>^ -- <文件>`),不整仓 reset。本文不作为规则依据。 +## 2026-11-19 + +### 功能 · 恢复服务监控入口并接入超管开关 + +- **范围**:用户中心桌面侧栏、移动底栏、服务监控路由与面包屑。 +- **变更**:恢复服务监控菜单和页面,复用超管运营设置中的 `SidebarModulesAdmin.userCenter.serviceDashboard` 开关;桌面、移动和路由统一判断区域开关、功能开关及可见分组。 +- **影响**:未配置时默认开启;关闭或分组不允许时不挂载监控页面、不请求监控数据,直接访问新旧地址均回到首个可见页面,无可见页面时回到首页;服务监控接口与刷新逻辑保持原有行为。 +- **回滚**:通过超管开关关闭入口;需要代码回滚时还原本条相关导航、路由与可见性判断。 +- **关联**:`docs/ui/SERVICE_DASHBOARD_SPEC.md`、`src/helpers/navigationVisibility.js`、`src/pages/UserCenter/`。 + ## 2026-11-18 ### 调整 · 认证页表单必填标识、就地校验与间距统一 diff --git a/docs/ui/SERVICE_DASHBOARD_SPEC.md b/docs/ui/SERVICE_DASHBOARD_SPEC.md index b9c8bb22..a822465b 100644 --- a/docs/ui/SERVICE_DASHBOARD_SPEC.md +++ b/docs/ui/SERVICE_DASHBOARD_SPEC.md @@ -1,6 +1,6 @@ # 服务监控 UI 规范(/user-center/service-dashboard) -> 状态:有效 · 适用:antd 6.7.0 · 更新:2026-11-11 · 版本:v2.1 +> 状态:有效 · 适用:antd 6.7.0 · 更新:2026-11-19 · 版本:v2.3 服务监控页的规范:分组可用性、历史条与详情弹窗。 @@ -10,7 +10,7 @@ | 关注点 | 规则 | | --- | --- | -| 路由 | `/user-center/service-dashboard`,旧 `/service-dashboard` 重定向 | +| 路由 | `/user-center/service-dashboard`,旧 `/service-dashboard` 重定向;桌面侧栏、移动底栏和页面访问统一受超管“运营设置 → 左侧边栏模块管理 → 用户中心 → 服务监控”开关(`SidebarModulesAdmin.userCenter.serviceDashboard`)及区域 / 功能可见分组控制;关闭或无分组权限时不挂载页面,回到首个可见页面,无可用页面时回到首页;未配置时沿用默认开启 | | 组件 | `Card`、`Button`、详情 `Modal` + `Statistic` + `Table`、`Tooltip`、`Spin` / `Empty` | | 主题 | `theme.useToken()` 注入 `--service-*`;正常态跟随 `colorPrimary`,告警 / 异常用 `colorWarning` / `colorError` | | 业务 | 60 秒自动刷新、逐秒倒计时、分组权限、模型测试与请求不变 | diff --git a/src/helpers/navigationVisibility.js b/src/helpers/navigationVisibility.js index f9c79136..86a082db 100644 --- a/src/helpers/navigationVisibility.js +++ b/src/helpers/navigationVisibility.js @@ -29,6 +29,12 @@ export const isGroupAllowed = (groups, userGroup) => { return allowedGroups.includes(getEffectiveUserGroup(userGroup)); }; +export const isSidebarModuleVisible = (config, key, userGroup) => + config?.enabled !== false && + config?.[key] !== false && + isGroupAllowed(config?.groups, userGroup) && + isGroupAllowed(config?.moduleGroups?.[key], userGroup); + export const isHeaderActionButtonVisible = (config, key, userGroup) => { let buttons = config; diff --git a/src/pages/UserCenter/UserCenterSidebar.jsx b/src/pages/UserCenter/UserCenterSidebar.jsx index 443a9d6e..f25411c7 100644 --- a/src/pages/UserCenter/UserCenterSidebar.jsx +++ b/src/pages/UserCenter/UserCenterSidebar.jsx @@ -41,7 +41,8 @@ import { } from 'lucide-react'; import { StatusContext } from '../../context/Status'; import { UserContext } from '../../context/User'; -import { isGroupAllowed } from '../../helpers/navigationVisibility'; +import { isSidebarModuleVisible } from '../../helpers/navigationVisibility'; +import { normalizeSidebarModulesAdmin } from '../../constants/sidebarModules'; import { useSidebarCollapsed } from '../../hooks/common/useSidebarCollapsed'; import { useWeyAiDownloadEntry } from '../../hooks/common/useWeyAiWebEntry'; @@ -57,47 +58,12 @@ const UserCenterSidebar = () => { const { visible: weyaiDownloadEnabled } = useWeyAiDownloadEntry(); // 获取侧边栏配置 - const sidebarConfig = useMemo(() => { - const defaultConfig = { - enabled: true, - groups: [], - moduleGroups: {}, - dashboard: true, - usageRecord: true, - serviceDashboard: true, - topup: true, - invoice: true, - invite: true, - settings: true, - }; - - try { - const configStr = statusState?.status?.SidebarModulesAdmin; - if (configStr) { - const config = JSON.parse(configStr); - const uc = config.userCenter || {}; - // 向后兼容:只有旧配置同时隐藏了 usageLog 和 taskLog,才隐藏合并后的 usageRecord; - // 只隐藏其中之一时保持显示,避免用户意外失去本应可见的日志入口 - if ( - uc.usageRecord === undefined && - uc.usageLog === false && - uc.taskLog === false - ) { - uc.usageRecord = false; - } - const merged = { ...defaultConfig, ...uc }; - return { - ...merged, - enabled: - merged.enabled !== false && - isGroupAllowed(merged.groups, userGroup), - }; - } - } catch (error) { - // 使用默认配置 - } - return defaultConfig; - }, [statusState?.status?.SidebarModulesAdmin, userGroup]); + const sidebarConfig = useMemo( + () => + normalizeSidebarModulesAdmin(statusState?.status?.SidebarModulesAdmin) + .userCenter, + [statusState?.status?.SidebarModulesAdmin], + ); // 统一单一品牌色高亮(云平台风格):菜单项不再各用一种颜色, // 激活态统一走 brand(azure),非激活中性灰,靠图标本身区分功能。 @@ -175,11 +141,7 @@ const UserCenterSidebar = () => { const inMiniProgram = isMiniProgramWebView(); return allItems.filter( (item) => - sidebarConfig[item.configKey] !== false && - isGroupAllowed( - sidebarConfig.moduleGroups?.[item.configKey], - userGroup, - ) && + isSidebarModuleVisible(sidebarConfig, item.configKey, userGroup) && !(inMiniProgram && isMiniProgramPaymentPath(item.itemKey)), ); }, [allItems, sidebarConfig, userGroup]); diff --git a/src/pages/UserCenter/index.jsx b/src/pages/UserCenter/index.jsx index 030af42d..31d047ed 100644 --- a/src/pages/UserCenter/index.jsx +++ b/src/pages/UserCenter/index.jsx @@ -37,6 +37,8 @@ import { Receipt, } from 'lucide-react'; import { StatusContext } from '../../context/Status'; +import { UserContext } from '../../context/User'; +import { isSidebarModuleVisible } from '../../helpers/navigationVisibility'; import { useMarquee } from '../../hooks/common/useMarquee'; import PageBackground from '../../components/common/PageBackground'; import UserCenterSidebar from './UserCenterSidebar'; @@ -61,6 +63,8 @@ const MobileBottomNav = ({ sidebarConfig }) => { const { t } = useTranslation(); const location = useLocation(); const navigate = useNavigate(); + const [userState] = useContext(UserContext); + const userGroup = userState?.user?.group; const navItems = [ { @@ -116,8 +120,7 @@ const MobileBottomNav = ({ sidebarConfig }) => { const inMiniProgram = isMiniProgramWebView(); const visibleNavItems = navItems.filter( (item) => - sidebarConfig?.enabled !== false && - sidebarConfig?.[item.key] !== false && + isSidebarModuleVisible(sidebarConfig, item.key, userGroup) && !(inMiniProgram && isMiniProgramPaymentPath(item.path)), ); @@ -155,6 +158,8 @@ const MobileBottomNav = ({ sidebarConfig }) => { const UserCenter = ({ keyManagementMode = false }) => { const [statusState] = useContext(StatusContext); + const [userState] = useContext(UserContext); + const userGroup = userState?.user?.group; // 仅用于页面外壳的样式标识(看板与子页面底色不同),不参与路由判断 const ucRoute = useLocation().pathname.split('/')[2] || 'dashboard'; @@ -192,7 +197,7 @@ const UserCenter = ({ keyManagementMode = false }) => { : routePathMap[ userCenterRoutes.find( (key) => - userCenterConfig[key] !== false && + isSidebarModuleVisible(userCenterConfig, key, userGroup) && !( inMiniProgram && isMiniProgramPaymentPath(`/user-center/${routePathMap[key]}`) @@ -203,7 +208,7 @@ const UserCenter = ({ keyManagementMode = false }) => { ? `/user-center/${firstVisiblePath}` : '/'; const guardUserCenterRoute = (key, element) => { - if (userCenterConfig.enabled === false || userCenterConfig[key] === false) { + if (!isSidebarModuleVisible(userCenterConfig, key, userGroup)) { return ; } return element; diff --git a/test/unit/service-dashboard-navigation.test.js b/test/unit/service-dashboard-navigation.test.js new file mode 100644 index 00000000..0b83c807 --- /dev/null +++ b/test/unit/service-dashboard-navigation.test.js @@ -0,0 +1,182 @@ +/* +Copyright (C) 2025 QuantumNous + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +For commercial licensing, please contact support@quantumnous.com +*/ + +import assert from 'node:assert/strict'; +import test from 'node:test'; +import React from 'react'; +import * as icons from 'lucide-react'; +import * as modules from '../../src/constants/sidebarModules.js'; +import * as visibility from '../../src/helpers/navigationVisibility.js'; +import { loadFrontendModule } from './frontend-module.js'; + +const StatusContext = {}; +const UserContext = {}; +let config; +let userGroup = 'default'; +const mocks = { + react: { + ...React, + useContext: (context) => + context === StatusContext + ? [{ status: { SidebarModulesAdmin: config } }] + : [{ user: { group: userGroup } }], + useMemo: (factory) => factory(), + }, + 'react-router-dom': { + Routes: 'routes', + Route: 'route', + Navigate: 'navigate', + Link: 'link', + useLocation: () => ({ pathname: '/user-center/service-dashboard' }), + useNavigate: () => () => {}, + }, + 'react-i18next': { useTranslation: () => ({ t: (value) => value }) }, + 'lucide-react': icons, + '../../context/Status': { StatusContext }, + '../../context/User': { UserContext }, + '../../constants/sidebarModules': modules, + '../../helpers/navigationVisibility': visibility, + '../../helpers/miniProgram': { + isMiniProgramWebView: () => false, + isMiniProgramPaymentPath: () => false, + }, + '../../hooks/common/useMarquee': { useMarquee: () => ({ visible: false }) }, + '../../hooks/common/useSidebarCollapsed': { + useSidebarCollapsed: () => [false, () => {}], + }, + '../../hooks/common/useWeyAiWebEntry': { + useWeyAiDownloadEntry: () => ({ visible: false }), + }, +}; +for (const path of [ + '../../components/common/PageBackground', + './UserCenterSidebar', + './UserDashboard', + './UsageRecord', + './Invite', + './Invoice', + '../TopUp', + '../../components/settings/PersonalSetting', + '../WeyAiDownload', + '../KeyManagement', +]) + mocks[path] = { __esModule: true, default: () => null }; +const ServiceDashboard = () => null; +mocks['../ServiceDashboard'] = { __esModule: true, default: ServiceDashboard }; +const UserCenter = ( + await loadFrontendModule( + new URL('../../src/pages/UserCenter/index.jsx', import.meta.url), + mocks, + ) +).default; +const Sidebar = ( + await loadFrontendModule( + new URL( + '../../src/pages/UserCenter/UserCenterSidebar.jsx', + import.meta.url, + ), + mocks, + ) +).default; + +function descendants(node) { + if (!React.isValidElement(node)) return []; + return [ + node, + ...React.Children.toArray(node.props.children).flatMap(descendants), + ]; +} + +for (const [name, userCenter, group, visible] of [ + ['defaults', {}, 'default', true], + ['enabled', { serviceDashboard: true }, 'default', true], + ['disabled', { serviceDashboard: false }, 'default', false], + ['section disabled', { enabled: false }, 'default', false], + ['allowed section group', { groups: ['vip'] }, 'vip', true], + ['denied section group', { groups: ['vip'] }, 'default', false], + [ + 'allowed module group', + { moduleGroups: { serviceDashboard: ['vip'] } }, + 'vip', + true, + ], + [ + 'denied module group', + { moduleGroups: { serviceDashboard: ['vip'] } }, + 'default', + false, + ], +]) { + test(`service dashboard desktop, mobile and route agree: ${name}`, () => { + config = JSON.stringify({ userCenter }); + userGroup = group; + const tree = UserCenter({}); + const nodes = descendants(tree); + const route = nodes.find((node) => node.props.path === 'service-dashboard'); + const mountsPage = route + ? descendants(route.props.element).some( + (node) => node.type === ServiceDashboard, + ) + : false; + assert.equal(mountsPage, visible); + if (route && !visible) { + assert.equal(route.props.element.type, 'navigate'); + assert.notEqual( + route.props.element.props.to, + '/user-center/service-dashboard', + ); + } + const desktop = descendants(Sidebar()).some( + (node) => node.props.to === '/user-center/service-dashboard', + ); + assert.equal(desktop, visible); + const mobileComponent = nodes.find( + (node) => node.props.sidebarConfig !== undefined, + ); + const mobile = mobileComponent + ? descendants(mobileComponent.type(mobileComponent.props)).some( + (node) => node.type === 'span' && node.props.children === '监控', + ) + : false; + assert.equal(mobile, visible); + }); +} + +test('service dashboard can be the only enabled default route without a redirect loop', () => { + config = JSON.stringify({ + userCenter: { + ...Object.fromEntries( + Object.keys(modules.getDefaultSidebarModulesAdmin().userCenter).map( + (key) => [key, false], + ), + ), + enabled: true, + serviceDashboard: true, + }, + }); + const nodes = descendants(UserCenter({})); + const route = nodes.find((node) => node.props.path === '/'); + assert.equal(route.props.element.props.to, '/user-center/service-dashboard'); + const service = nodes.find((node) => node.props.path === 'service-dashboard'); + assert.ok( + descendants(service.props.element).some( + (node) => node.type === ServiceDashboard, + ), + ); +});