fix(p2): 按审查清单修 103 条「建议修」+ 奖牌榜改名 + 两个守卫(dev_0.2.0)

## 奖牌榜改名(主人要求)
「团体奖牌榜」→「单位奖牌榜」,界面 / 大屏渲染页 / 导出件(工作表名与
下载文件名 `单位奖牌明细.xlsx`)/ 用户文档 全部统一;`DISPLAY_PAGES` 是
大屏控制页与大屏页的共同来源,改一处两边同时生效。旧名保留为别名,
老链接与老配置(`display.pages`)照常有效。真 Chromium 验证:14 个管理端
路由 + 7 个基础数据分区 0 报错,大屏页标题已是「单位奖牌榜」。

## P2 清单(123 条,103 条标「建议修」)
按 A/B/C/D 分组、以「文件所有权互斥」分 10 个代理并行推进,另有独立复核
代理逐条核对:61 条声称已修 / 55 条证实 / 4 条部分 / 0 条不成立 / 0 垃圾。

主要交付(按风险从高到低):
- 写入侧校验:update 路径补必填与 TrimSpace、模板 rules 重复 rank、
  「小项不存在」的四种回答、`defaultProfileId` 外键、decodeJSON 的 8MB
  上限与 413(A01–A06、D02、D03 批量上限 500)
- 危险操作:一键恢复加互斥(第二个请求当场 409)、恢复超时不再说
  「未执行」、`ConfirmDialog` pending 时锁死 ESC/遮罩/关闭
- 认证:改密与撤会话进同一事务、滑动续期写库失败不再假装成功、
  CSRF Cookie 丢失自动补发、密码长度按字符(原按字节)、
  反代 403 只在可信代理透传原 Host 时采信 `X-Forwarded-Host`(全等比较)、
  过期会话定期清理、登出补审计、登录限流可配
- 数据可见性:导出「已发布未结算」行改为 `名次 — / 积分 — / 状态未结算`、
  单位积分明细成绩列与其它导出件逐字一致、空单位也打合计行、
  基础数据包命中「已彻底删除的单位」整行跳过并报界面路径、
  报名导入换单位给明确提示、勾选报名有冲突不再假报成功
- 导入/导出:赛事包上限 512MB(**它是唯一带字体/图片的包**,字体上限
  20MB)而 multipart 内存档位独立钉在 16MB、工作簿表名尾随空格、
  导出件路由与文件名契约补全
- 前端:一次写请求只生成一个 requestId(幂等缓存此前从未命中)、
  基础数据包补 `X-Request-Id`、示例数据模式禁写扩到撤销发布/存草稿、
  访客 401 不再是裸错误、模板 `{{else if}}` 不再静默吞内容
- Console:按设计退出(切赛事/恢复)不再判成崩溃、旧数据目录读不出来
  会记日志、渲染层日志按上限截断、`service.js` 补 17 例测试

## 测试
服务端用例 610 → 674(13 包全绿,新增老库跨九条迁移升级、panic 形状、
429 落 interceptions、三态对照、avg_mode 分母、会话失效、请求体上限等)
前端 449 → 531(vue-tsc + build 通过);Console 28 → 54(19 个文件)。
另修复一条会让整包挂死 600s 的用例(测试主协程堵在 client.Do,defer
轮不到执行 → 改为可取消 + 探针超时,改坏也 7.5s 快速失败)。

## 两个新守卫(防止再出现「组件 setup 抛异常」这类盲区)
- `web/scripts/check-declaration-order.mjs` 已进测试命令:沿 watch/
  watchEffect 的 const 依赖链查 TDZ(正是「运动员页整块空白」的根因)
- `web/scripts/smoke-admin.mjs`(`pnpm smoke`):真 Chromium 点遍 14 个
  管理端路由 + 7 个基础数据分区 + 大屏页,任何 console 错误即失败

## 文档
- `docs/升级说明.md` 新增 dev_0.2.0 一节(三件升级必知:改名、审计前缀
  与历史记录、密码口径;现场变化;赛事包上限;反代与 CSRF)
- 使用手册 / 常见问题 / 部署指南 / 上线检查清单按实现同步
This commit is contained in:
AskaEth committed 2026-09-11 23:46:59 +08:00
1 parent db94d9c119
commit 6841cf49aa
147 files changed
+8141 -423

No files matched your search

+51 -46
View File
@@ -10,7 +10,6 @@ import type {
AthleteStanding,
AuditLog,
AuditLogQuery,
BackupResult,
BackupRestoreInput,
BackupRestoreResult,
CurrentUser,
@@ -80,6 +79,14 @@ interface RequestOptions {
query?: object
body?: unknown
signal?: AbortSignal
/**
* 单次请求的超时覆盖值(毫秒);不传用客户端默认的 `ApiClientOptions.timeoutMs`。
*
* 停机类动作(用备份恢复整库)在服务端要拍安全副本、换文件、校验、再写审计,
* 回执是**全部做完之后**才发出的:默认 15s 会在库已经换掉之后把请求判成超时,
* 于是客户端以为「没执行」而重试。这类接口必须单独放大超时。
*/
timeoutMs?: number
}
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE'
@@ -369,15 +376,8 @@ export interface DisplayApi {
live(): Promise<LiveEvents>
}
export interface ExportQuery {
eventId?: number
teamId?: number
}
export interface ExportApi {
/** 返回可直接放进 <a download> 的地址,省掉一次内存中的 Blob。 */
standingsUrl(query?: ExportQuery): string
standings(query?: ExportQuery): Promise<Blob>
/** 把服务端返回的 Blob 存成文件(积分榜 / 结果导出的唯一入口)。 */
save(blob: Blob, filename: string): void
}
@@ -393,7 +393,6 @@ export interface RulesApi {
}
export interface SystemApi {
backup(): Promise<BackupResult>
/**
* 用一份备份恢复当前库(`POST /api/backup/restore`,仅 admin)。
*
@@ -428,8 +427,6 @@ interface Http {
requestVoid(path: string, options?: RequestOptions): Promise<void>
requestJson(path: string, options?: RequestOptions): Promise<unknown>
requestText(path: string, options?: RequestOptions): Promise<string>
url(path: string, query?: object): string
blob(path: string, options?: RequestOptions): Promise<Blob>
}
function createCrudApi<T, Input>(http: Http, base: string): CrudApi<T, Input> {
@@ -596,13 +593,9 @@ function createResultsApi(http: Http): ResultsApi {
}
}
function createExportApi(http: Http): ExportApi {
// 服务端只注册了 /export/standings.xlsx 与 /export/all.xlsx 等;
// 单位明细 / 分项成绩导出分别是 /export/teams/{id}、/export/events/{id},由各自页面拼地址。
const standingsPath = '/export/standings.xlsx'
/** 导出只剩下「把 Blob 存成文件」这一件事:路径与文件名由各页面按服务端 Content-Disposition 拼。 */
function createExportApi(): ExportApi {
return {
standingsUrl: (query) => http.url(standingsPath, query),
standings: (query) => http.blob(standingsPath, { query }),
save: (blob, filename) => {
const url = URL.createObjectURL(blob)
const anchor = document.createElement('a')
@@ -718,6 +711,15 @@ function defaultRequestId(): string {
return `req-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`
}
/**
* 恢复整库的超时(毫秒)。
*
* 服务端的恢复是「结束请求才写回执」:拍安全副本、换文件、复核、写审计全部做完才 writeJSON。
* 而备份体积随赛事数据增长(几百 MB 很常见),默认 15s 会把「已经在换库」的请求判成超时;
* 60s 相对默认留了 4 倍余量,又远小于现场操作员的等待耐心。
*/
export const RESTORE_TIMEOUT_MS = 60_000
export class ApiClient {
readonly auth: AuthApi
readonly settings: SettingsApi
@@ -761,8 +763,6 @@ export class ApiClient {
requestVoid: (path, requestOptions) => this.requestVoid(path, requestOptions),
requestJson: (path, requestOptions) => this.requestJson(path, requestOptions),
requestText: (path, requestOptions) => this.requestText(path, requestOptions),
url: (path, query) => this.buildUrl(path, query),
blob: (path, requestOptions) => this.fetchBlob(path, requestOptions),
}
this.auth = {
@@ -845,15 +845,22 @@ export class ApiClient {
live: () => http.request<LiveEvents>('/display/live'),
}
this.exports = createExportApi(http)
this.exports = createExportApi()
this.auditLogs = {
list: (query) => http.request<Page<AuditLog>>('/audit-logs', { query }),
}
this.rules = createRulesApi(http)
this.system = {
backup: () => http.request<BackupResult>('/backup', { method: 'POST' }),
// 恢复要走幂等(X-Request-Id):现场断网重试时,重复请求不能把库换两遍。
restore: (input) => http.request<BackupRestoreResult>('/backup/restore', { method: 'POST', body: input }),
// 恢复是停机动作:服务端先拍安全副本、换库文件、复核新库、写审计,**全部做完才发回执**,
// 库越大这一串越慢。用默认 15s 会在库已经换掉之后把请求判成超时,
// 操作员看到「未执行」而重试,等于把刚恢复好的库再覆盖一遍,所以单独放大超时。
// 幂等(X-Request-Id)照旧:现场断网重试时,重复请求不能把库换两遍。
restore: (input) =>
http.request<BackupRestoreResult>('/backup/restore', {
method: 'POST',
body: input,
timeoutMs: RESTORE_TIMEOUT_MS,
}),
}
this.projects = createProjectsApi(http)
@@ -924,23 +931,16 @@ export class ApiClient {
return response.text()
}
private async fetchBlob(path: string, options: RequestOptions = {}): Promise<Blob> {
const response = await this.send(path, options)
if (!response.ok) {
const body = await readBody(response)
if (response.status === 401) {
const error = this.toApiError(body, response.status)
this.notifyUnauthorized(error)
throw error
}
throw this.toApiError(body, response.status)
}
return response.blob()
}
private async send(path: string, options: RequestOptions): Promise<Response> {
const method: HttpMethod = options.method ?? 'GET'
const payload = this.prepareBody(method, options.body)
// 一次调用只用**一个**幂等键,header 与 body 共用它。
// 服务端是两层缓存:外层 cached 认 X-Request-Id,内层 idempotent 认 body.requestId,
// 两边各生成一个就永远拼不出同一个 key,幂等缓存从未命中——现场超时后重试
// 「发布 / 撤销发布 / 存草稿 / 移出报名」会真的重复执行一遍。
// 调用方已经在 body 里给了 key(例如重发同一次写操作)就沿用它,否则这里新建一个。
const given = isRecord(options.body) ? options.body['requestId'] : undefined
const requestId = method === 'GET' ? null : (typeof given === 'string' && given !== '' ? given : this.newRequestId())
const payload = this.prepareBody(method, options.body, requestId)
// 断言:二进制 / 表单载荷只能原样透传,一旦被归一化展开就会静默丢内容(报名表导入踩过)。
if (isRawBody(options.body) && payload !== options.body) {
throw new ApiError('INTERNAL', '请求体归一化破坏了 FormData/Blob 载荷', 0, options.body)
@@ -958,11 +958,13 @@ export class ApiClient {
if (method !== 'GET') {
const token = this.csrfToken()
if (token !== null && token !== '') headers['X-CSRF-Token'] = token
headers['X-Request-Id'] = this.newRequestId()
// 与 body.requestId 是同一个值:服务端两层幂等缓存(cached / idempotent)才能命中。
if (requestId !== null) headers['X-Request-Id'] = requestId
}
const timeoutMs = options.timeoutMs ?? this.timeoutMs
const controller = new AbortController()
const timer = setTimeout(() => controller.abort(), this.timeoutMs)
const timer = setTimeout(() => controller.abort(), timeoutMs)
const external = options.signal
let abortedByCaller = false
if (external !== undefined) {
@@ -991,7 +993,7 @@ export class ApiClient {
// 外部取消与服务端超时共用同一个 signal,必须分开归类,调用方才能决定要不要提示。
if (abortedByCaller) throw new ApiError('ABORTED', '请求已取消', 0, error)
if (controller.signal.aborted) {
throw new ApiError('TIMEOUT', `请求超时(${this.timeoutMs}ms)`, 0, error)
throw new ApiError('TIMEOUT', `请求超时(${timeoutMs}ms)`, 0, error)
}
throw new ApiError('NETWORK', '无法连接服务端', 0, error)
} finally {
@@ -999,13 +1001,16 @@ export class ApiClient {
}
}
/** 写操作注入 requestId:网络抖动重发时服务端能返回首次结果而不是重复执行。 */
private prepareBody(method: HttpMethod, body: unknown): unknown {
/**
* 写操作注入 requestId:网络抖动重发时服务端能返回首次结果而不是重复执行。
* `requestId` 由 `send()` 生成一次并同时用于 header,两者必须是同一个值(见 send 的注释)。
*/
private prepareBody(method: HttpMethod, body: unknown, requestId: string | null): unknown {
if (method === 'GET' || body === undefined) return undefined
// FormData/Blob/URLSearchParams 不能展开,否则文件/二进制内容会被丢掉。
if (isRawBody(body)) return body
if (isRecord(body) && !('requestId' in body)) {
return { ...body, requestId: this.newRequestId() }
if (requestId !== null && isRecord(body) && !('requestId' in body)) {
return { ...body, requestId }
}
return body
}