## 客户端灵动岛(动态岛)
- 新增独立透明置顶窗口:桌面中上方常驻、不占任务栏、不抢焦点(268x44 胶囊 / 404x316 详情 / 424x344 紧急)
- 三种形态与动画:隐藏 ↔ 胶囊 ↔ 详情,尺寸用 easeOutCubic 260ms 逐帧缓动、透明度单独淡入淡出
(尺寸与淡入使用各自动画令牌,避免互相打断导致窗口卡在胶囊尺寸)
- 行为:普通通知变「新消息 · 共 N 条」胶囊,点击展开详情;上课时段自动隐藏并进入待发队列,
下课后自动弹出;紧急通知无论是否上课立即展开(红色呼吸光晕 + 紧急角标,无需点击)
- 上课状态来自 GET /api/schedules/current(10s 心跳 + 下课时长定时器),
新增 src/main/island.ts(控制器/IPC)、src/preload/island.ts、src/island/IslandApp.vue、
src/renderer/island/bridge.ts、island.html 与多页 Vite 构建
## 上课时段发布紧急通知的二次确认
- 服务端:priority=URGENT 且当前处于上课时间段且未带 confirmDuringClass 时返回
409 URGENT_DURING_CLASS(携带 current/next/week/serverTime),绕过界面调接口同样被拦截
- 新增 GET /api/schedules/current(inClass/current/next,at 参数用于诊断与测试)
- Web 管理端:全屏警告遮罩(UrgentClassWarning.vue),显示正在上的课时段与待发布标题,
确认按钮带 3 秒倒计时(圆环进度,倒计时结束前禁用),取消即不发;并发命中 409 同样兜底弹窗
## 打包与验证脚本修复(Electron 宿主环境)
- scripts/lib/node-runtime.mjs:定位真正的 node.exe。此前经 pnpm 运行时 process.execPath 指向
Electron,dist:server 会把 Electron 复制成安装包内的 node.exe,dist:win 会让 electron-builder 报
Unknown argument: .../cli.js(改为显式使用真实 Node)
- scripts/lib/electron-env.mjs:启动 Electron 前清理继承的 ELECTRON_RUN_AS_NODE(否则 Electron 退化为
纯 Node,require('electron') 直接 MODULE_NOT_FOUND),并从 path.txt 定位 electron.exe
(process.versions.electron 存在时 import 'electron' 返回的是 API 对象而非路径)
- scripts/dist-server.mjs:新增构建产物新鲜度校验(曾出现 server 已重建但打进安装包的 shared/dist 是旧的,
安装后服务启动即报 does not provide an export named MAX_TERM_WEEK)
- scripts/ui-smoke:新增 live-probe.mjs 真实制造/清理上课时段,Electron 驱动真实点击断言全屏警告文案、
按钮禁用与 3 秒倒计时时长(实测 2966ms),测试后自动删除探针课表与测试通知
## 验证结果
- pnpm verify:e2e 62/62(新增上课时段拦截、二次确认后发布、普通通知不受限等 8 项)
- pnpm verify:web 8/8(新增上课时段紧急通知全屏警告 + 倒计时)
- pnpm verify:desktop 18/18(新增灵动岛窗口/上课隐藏/下课弹出/紧急立即展开/胶囊点击展开 + 截图)
- 打包后 EXE 冒烟 15/15(packaged: true,退出码 0)
- 服务端安装程序升级安装:.env 与数据库保持不变,/api/schedules/current 正常
- pnpm lint / format:check / typecheck 全部通过
151 lines
5.3 KiB
TypeScript
151 lines
5.3 KiB
TypeScript
import { SOCKET_EVENTS, type NotificationDto } from '@classhelper/shared';
|
||
import {
|
||
assertClassAccess,
|
||
assertClassWritable,
|
||
classScopeWhere,
|
||
resolveClassScope,
|
||
} from '../../lib/access.js';
|
||
import { prisma } from '../../lib/db.js';
|
||
import { ApiError } from '../../lib/http.js';
|
||
import type { TokenPayload } from '../../lib/jwt.js';
|
||
import { logger } from '../../lib/logger.js';
|
||
import { toNotificationDto } from '../../lib/mappers.js';
|
||
import { emitToClass } from '../../realtime/bus.js';
|
||
import { computeClassStatus } from '../schedules/schedules.service.js';
|
||
import type { CreateNotificationInput } from './notifications.schemas.js';
|
||
|
||
const creatorSelect = { select: { id: true, name: true, username: true } } as const;
|
||
|
||
export interface ListNotificationOptions {
|
||
classId?: string;
|
||
priority?: string;
|
||
unreadOnly?: boolean;
|
||
keyword?: string;
|
||
}
|
||
|
||
export async function listNotifications(
|
||
user: TokenPayload,
|
||
options: ListNotificationOptions,
|
||
): Promise<NotificationDto[]> {
|
||
const scope = await resolveClassScope(user, options.classId);
|
||
const isStaff = user.role === 'ADMIN' || user.role === 'TEACHER';
|
||
|
||
const notifications = await prisma.notification.findMany({
|
||
where: {
|
||
...classScopeWhere(scope),
|
||
...(options.priority ? { priority: options.priority } : {}),
|
||
...(options.keyword
|
||
? { OR: [{ title: { contains: options.keyword } }, { content: { contains: options.keyword } }] }
|
||
: {}),
|
||
...(options.unreadOnly ? { reads: { none: { userId: user.sub } } } : {}),
|
||
},
|
||
include: {
|
||
creator: creatorSelect,
|
||
reads: isStaff ? true : { where: { userId: user.sub } },
|
||
},
|
||
orderBy: [{ createdAt: 'desc' }],
|
||
take: 200,
|
||
});
|
||
|
||
return notifications.map((item) => toNotificationDto(item, { userId: user.sub, withStatus: isStaff }));
|
||
}
|
||
|
||
/**
|
||
* 发布通知并实时广播到 class:{classId} 房间。
|
||
*
|
||
* 上课时段保护:若目标班级当前正在上课且优先级为"紧急",必须由前端二次确认
|
||
* (`confirmDuringClass: true`)才允许发布,否则返回 409 URGENT_DURING_CLASS,
|
||
* 由 Web 端弹出全屏警告并倒计时 3 秒。
|
||
*/
|
||
export async function createNotification(
|
||
user: TokenPayload,
|
||
input: CreateNotificationInput,
|
||
): Promise<NotificationDto> {
|
||
await assertClassWritable(user, input.classId);
|
||
|
||
const priority = input.priority ?? 'NORMAL';
|
||
if (priority === 'URGENT' && input.confirmDuringClass !== true) {
|
||
const status = await computeClassStatus(input.classId);
|
||
if (status.inClass) {
|
||
logger.warn(
|
||
`上课时段发布紧急通知被拦截:班级=${input.classId} 课程=${status.current?.courseName ?? '-'}(等待教师二次确认)`,
|
||
);
|
||
throw new ApiError(409, 'URGENT_DURING_CLASS', '现在为上课时间段,发布紧急通知会干扰上课,请再次确认', {
|
||
classId: input.classId,
|
||
current: status.current,
|
||
next: status.next,
|
||
week: status.week,
|
||
serverTime: status.serverTime,
|
||
});
|
||
}
|
||
}
|
||
|
||
const created = await prisma.notification.create({
|
||
data: {
|
||
classId: input.classId,
|
||
title: input.title,
|
||
content: input.content,
|
||
priority,
|
||
createdBy: user.sub,
|
||
},
|
||
include: { creator: creatorSelect, reads: true },
|
||
});
|
||
|
||
const dto = toNotificationDto(created, { userId: user.sub, withStatus: true });
|
||
emitToClass(created.classId, SOCKET_EVENTS.notificationNew, dto);
|
||
return dto;
|
||
}
|
||
|
||
export async function deleteNotification(user: TokenPayload, notificationId: string): Promise<void> {
|
||
const current = await prisma.notification.findUnique({ where: { id: notificationId } });
|
||
if (!current) throw ApiError.notFound('通知不存在');
|
||
await assertClassWritable(user, current.classId);
|
||
await prisma.notification.delete({ where: { id: notificationId } });
|
||
}
|
||
|
||
/** 标记单条通知已读 */
|
||
export async function markAsRead(
|
||
user: TokenPayload,
|
||
notificationId: string,
|
||
): Promise<{ notificationId: string; readAt: string }> {
|
||
const notification = await prisma.notification.findUnique({ where: { id: notificationId } });
|
||
if (!notification) throw ApiError.notFound('通知不存在');
|
||
await assertClassAccess(user, notification.classId);
|
||
|
||
const read = await prisma.notificationRead.upsert({
|
||
where: { notificationId_userId: { notificationId, userId: user.sub } },
|
||
create: { notificationId, userId: user.sub },
|
||
update: {},
|
||
});
|
||
|
||
return { notificationId, readAt: read.readAt.toISOString() };
|
||
}
|
||
|
||
/** 全部标为已读(可按班级限定) */
|
||
export async function markAllAsRead(user: TokenPayload, classId?: string): Promise<{ marked: number }> {
|
||
const scope = await resolveClassScope(user, classId);
|
||
|
||
const pending = await prisma.notification.findMany({
|
||
where: { ...classScopeWhere(scope), reads: { none: { userId: user.sub } } },
|
||
select: { id: true },
|
||
});
|
||
|
||
for (const item of pending) {
|
||
await prisma.notificationRead.upsert({
|
||
where: { notificationId_userId: { notificationId: item.id, userId: user.sub } },
|
||
create: { notificationId: item.id, userId: user.sub },
|
||
update: {},
|
||
});
|
||
}
|
||
|
||
return { marked: pending.length };
|
||
}
|
||
|
||
/** 未读数量(客户端红点) */
|
||
export async function unreadCount(user: TokenPayload, classId?: string): Promise<number> {
|
||
const scope = await resolveClassScope(user, classId);
|
||
return prisma.notification.count({
|
||
where: { ...classScopeWhere(scope), reads: { none: { userId: user.sub } } },
|
||
});
|
||
}
|