From 910a4263f6318f704abd130c40657d38b30670f5 Mon Sep 17 00:00:00 2001 From: xyx Date: Mon, 13 Jul 2026 14:52:03 +0800 Subject: [PATCH] fix: refactor notification display logic and add format function --- .../admin/src/components/NotificationBell.tsx | 15 ++--------- apps/admin/src/pages/Notifications/index.tsx | 5 ++-- apps/admin/src/utils/notification-display.ts | 25 +++++++++++++++++++ apps/server/src/classes/classes.controller.ts | 10 +++++++- 4 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 apps/admin/src/utils/notification-display.ts diff --git a/apps/admin/src/components/NotificationBell.tsx b/apps/admin/src/components/NotificationBell.tsx index 53d3fdb..91e7fb1 100644 --- a/apps/admin/src/components/NotificationBell.tsx +++ b/apps/admin/src/components/NotificationBell.tsx @@ -3,6 +3,7 @@ import { Badge, Popover, Button, List, Typography, Empty } from 'antd'; import { BellOutlined } from '@ant-design/icons'; import { useNavigate } from 'react-router-dom'; import api from '../api'; +import { formatNotificationText, notificationTypeLabels } from '../utils/notification-display'; interface NotificationItem { id: number; @@ -14,18 +15,6 @@ interface NotificationItem { createdAt: string; } -const typeLabels: Record = { - bill_generated: '账单', - bill_paid: '账单', - check_in: '入住', - check_out: '退宿', - deposit_due: '押金', - deposit_refunded: '押金', - class_change: '班级', - schedule_conflict: '排课', - announcement: '公告', -}; - function timeAgo(dateStr: string): string { const diff = Date.now() - new Date(dateStr).getTime(); const mins = Math.floor(diff / 60000); @@ -163,7 +152,7 @@ const NotificationBell: React.FC = () => { strong={!item.isRead} style={{ fontSize: 14 }} > - [{typeLabels[item.type] || item.type}] {item.title} + [{notificationTypeLabels[item.type] || item.type}] {formatNotificationText(item.title)} } description={ diff --git a/apps/admin/src/pages/Notifications/index.tsx b/apps/admin/src/pages/Notifications/index.tsx index e247956..0762ba0 100644 --- a/apps/admin/src/pages/Notifications/index.tsx +++ b/apps/admin/src/pages/Notifications/index.tsx @@ -10,6 +10,7 @@ import { import { useNavigate } from 'react-router-dom'; import api from '../../api'; import { message } from '../../ui/app-message'; +import { formatNotificationText } from '../../utils/notification-display'; const { Sider, Content } = Layout; @@ -169,7 +170,7 @@ const NotificationsPage: React.FC = () => { strong={!item.isRead} style={{ fontSize: 15 }} > - {item.title} + {formatNotificationText(item.title)} {timeAgo(item.createdAt)} @@ -183,7 +184,7 @@ const NotificationsPage: React.FC = () => { ellipsis={{ rows: 1 }} style={{ marginBottom: 0 }} > - {item.content} + {formatNotificationText(item.content)} ) } diff --git a/apps/admin/src/utils/notification-display.ts b/apps/admin/src/utils/notification-display.ts new file mode 100644 index 0000000..909ec64 --- /dev/null +++ b/apps/admin/src/utils/notification-display.ts @@ -0,0 +1,25 @@ +export const notificationTypeLabels: Record = { + bill_generated: '账单', + bill_paid: '账单', + check_in: '入住', + check_out: '退宿', + deposit_due: '押金', + deposit_refunded: '押金', + class_change: '班级', + schedule_conflict: '排课', + announcement: '公告', +}; + +const teacherRoleLabels: Record = { + subject_teacher: '任课老师', + head_teacher: '班主任', + life_teacher: '生活老师', + academic_teacher: '学服老师', +}; + +export function formatNotificationText(text: string): string { + return Object.entries(teacherRoleLabels).reduce( + (result, [roleType, label]) => result.replaceAll(roleType, label), + text, + ); +} diff --git a/apps/server/src/classes/classes.controller.ts b/apps/server/src/classes/classes.controller.ts index d79af3d..1b121a9 100644 --- a/apps/server/src/classes/classes.controller.ts +++ b/apps/server/src/classes/classes.controller.ts @@ -29,6 +29,7 @@ import { extractRequestInfo } from '../common/request-utils'; import { RequirePermission } from '../auth/decorators/permission.decorator'; import { NotificationsService } from '../notifications/notifications.service'; import { NotificationType } from '../entities/notification.entity'; +import { TeacherRoleType } from '../entities'; import * as ExcelJS from 'exceljs'; import { AuthorizationService, CaslAction, SubjectName, AuthenticatedUser } from '../authorization'; @@ -36,6 +37,13 @@ interface AuthenticatedRequest { user: AuthenticatedUser; } +const teacherRoleLabels: Record = { + [TeacherRoleType.SUBJECT_TEACHER]: '任课老师', + [TeacherRoleType.HEAD_TEACHER]: '班主任', + [TeacherRoleType.LIFE_TEACHER]: '生活老师', + [TeacherRoleType.ACADEMIC_TEACHER]: '学服老师', +}; + @UseGuards(JwtAuthGuard) @Controller('classes') export class ClassesController { @@ -302,7 +310,7 @@ export class ClassesController { recipientIds: [dto.userId], type: NotificationType.CLASS_CHANGE, title: '班级分配', - content: `您已被分配到班级担任${dto.roleType}角色`, + content: `您已被分配到班级担任${teacherRoleLabels[dto.roleType] ?? dto.roleType}角色`, }); } catch {} return result;