Files
Scoreboard/web/src/admin/components/baseData/ProfilesSection.vue
T
AskaEth b5f40642ff feat: 公告持久化(底栏常驻)+ 团体可单独报名 + 榜单系数开关
公告:
- 服务端把当前公告落库到 settings(display.announce_text / announce_seconds),
  作为 display.announce 随 snapshot 与 /api/settings 下发(非 admin 快照也保留)
- announce 指令除转发外写库;新增 announce_clear 清空并广播;两者写审计
- realtime 协议登记 announce_clear(补上,使清除能即时推到大屏)
- 大屏把「全屏推送」与「底栏常驻」拆开:推送 seconds 秒后只退出全屏态,公告留在底栏,
  直到被替换或清除;新增公告状态机(可测)与底栏取值优先级
  推送中 > 服务端持久值 > 「暂无公告」;管理端加「清除公告」与当前公告状态

团体报名:
- EntryPicker 团体/对抗模式允许不勾队员直接报名该单位(此前必须勾人,导致
  「只想给单位报个团体项目」做不到、单位下没有运动员时完全报不了)
- 核实录入页团体行:显示/填名次/保存/发布/算分闭环无缺陷(用真实服务端跑过),
  抽出共享的 entryRowLabel 供两处复用

榜单口径:
- 基础数据 → 积分模板页新增「榜单口径(全局)」卡片:individualToTeamWeight
  数字输入(默认 1、0–100),说明「个人项目得分按该系数折算计入单位总分」,
  保存走 PUT /api/settings;因该值在服务端是全局键、模板表里没有此字段,
  故做成独立卡片并标注全局,不塞进模板表单

自检:服务端 11 包全绿;前端 vue-tsc + build、node --test 99/99。
2026-09-10 11:06:58 +08:00

516 lines
18 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { computed, ref, watch } from 'vue'
import type { RecordBonusMode, ScoringProfile, ScoringProfileInput, TieMode } from '@/shared'
import MxIcon from '../MxIcon.vue'
import ConfirmDialog from '../ConfirmDialog.vue'
import FormField from './FormField.vue'
import { parseIndividualToTeamWeight, useBaseData } from './useBaseData'
import { useAuthStore } from '../../stores/auth'
import { useToastStore } from '../../stores/toast'
const data = useBaseData()
const auth = useAuthStore()
const toast = useToastStore()
const CODE_PATTERN = /^[A-Za-z0-9_-]{1,32}$/
const TIE_MODE_OPTIONS: Array<{ value: TieMode; label: string }> = [
{ value: 'skip', label: '跳位(并列后空出后续名次)' },
{ value: 'average', label: '均分(并列者取平均分)' },
{ value: 'higher', label: '取高(并列者都按较高名次)' },
]
const BONUS_MODE_OPTIONS: Array<{ value: RecordBonusMode; label: string }> = [
{ value: 'max', label: '取最高一次' },
{ value: 'sum', label: '累加全部' },
]
interface RuleRow {
key: number
rank: string
points: string
}
interface ProfileForm {
code: string
name: string
tieMode: TieMode
recordBonusMode: RecordBonusMode
awardRanks: string
lastPlaceZero: boolean
rules: RuleRow[]
}
type FormFieldKey = 'code' | 'name' | 'awardRanks' | 'rules'
let nextKey = 1
const selectedId = ref<number | null>(null)
const creating = ref(false)
const form = ref<ProfileForm>(emptyForm())
const errors = ref<Partial<Record<FormFieldKey, string>>>({})
const formError = ref<string | null>(null)
const pending = ref(false)
const confirmOpen = ref(false)
const removedRules = ref<string[]>([])
const selectedProfile = computed(() => data.profileById(selectedId.value) ?? null)
const isEditing = computed(() => creating.value || selectedProfile.value !== null)
// ── 榜单口径:全局设置,不随某张模板保存 ──────────────────────────────────
const isAdmin = computed(() => auth.user?.role === 'admin')
const weightInput = ref<number | string>('')
const weightSaving = ref(false)
const weightError = ref<string | null>(null)
/** 库里现有的系数;settings 仅管理员可见,非管理员拿到 null。 */
const storedWeight = computed<number | null>(() => data.settings.value?.standings.individualToTeamWeight ?? null)
const parsedWeight = computed(() => parseIndividualToTeamWeight(weightInput.value))
const weightDirty = computed(
() => parsedWeight.value !== null && storedWeight.value !== null && parsedWeight.value !== storedWeight.value,
)
// 服务端设置变化(规则包导入、别处保存)时回填;本地已改动的输入不被覆盖。
watch(
() => data.settings.value?.standings.individualToTeamWeight,
(value) => {
if (value === undefined) return
if (!weightDirty.value) weightInput.value = value
},
{ immediate: true },
)
async function saveWeight(): Promise<void> {
const value = parsedWeight.value
if (value === null) {
weightError.value = '折算系数需为 0–100 的数字,例如 1 或 0.5'
return
}
// 服务端对 ≤0 直接 400(0 与负数会被拒绝),本地先拦下,别让用户以为能「按 1 处理」。
if (value <= 0) {
weightError.value = '折算系数必须大于 0,服务端会拒绝 0 与负数'
return
}
weightSaving.value = true
weightError.value = null
try {
await data.saveIndividualToTeamWeight(value)
toast.success('榜单口径已保存', '个人项目得分已按新系数折算计入单位总分')
} catch (caught) {
weightError.value = caught instanceof Error ? caught.message : '保存失败'
} finally {
weightSaving.value = false
}
}
watch(
() => data.profiles.value,
(list) => {
if (creating.value) return
const current = list.find((profile) => profile.id === selectedId.value)
if (current === undefined) {
const first = list[0]
if (first === undefined) {
selectedId.value = null
form.value = emptyForm()
} else {
loadProfile(first)
}
return
}
if (form.value.code === '' && form.value.name === '') loadProfile(current)
},
{ immediate: true },
)
function emptyForm(): ProfileForm {
return {
code: '',
name: '',
tieMode: 'skip',
recordBonusMode: 'max',
awardRanks: '6',
lastPlaceZero: false,
rules: [
{ key: (nextKey += 1), rank: '1', points: '10' },
{ key: (nextKey += 1), rank: '2', points: '8' },
{ key: (nextKey += 1), rank: '3', points: '6' },
],
}
}
function loadProfile(profile: ScoringProfile): void {
creating.value = false
selectedId.value = profile.id
form.value = {
code: profile.code,
name: profile.name,
tieMode: profile.tieMode,
recordBonusMode: profile.recordBonusMode,
awardRanks: String(profile.awardRanks ?? profile.rules.length),
lastPlaceZero: profile.lastPlaceZero ?? false,
rules: [...profile.rules]
.sort((a, b) => a.rank - b.rank)
.map((rule) => ({ key: (nextKey += 1), rank: String(rule.rank), points: String(rule.points) })),
}
errors.value = {}
formError.value = null
}
function startCreate(): void {
creating.value = true
selectedId.value = null
form.value = emptyForm()
errors.value = {}
formError.value = null
}
function addRule(): void {
const nextRank = form.value.rules.length + 1
form.value.rules = [
...form.value.rules,
{ key: (nextKey += 1), rank: String(nextRank), points: '0' },
]
}
function removeRule(key: number): void {
form.value.rules = form.value.rules.filter((rule) => rule.key !== key)
}
function validate(): boolean {
const next: Partial<Record<FormFieldKey, string>> = {}
const code = form.value.code.trim()
if (code === '') next.code = '代码必填'
else if (!CODE_PATTERN.test(code)) next.code = '只能用字母、数字、下划线或短横线'
else if (data.profiles.value.some((profile) => profile.id !== selectedId.value && profile.code.toLowerCase() === code.toLowerCase())) {
next.code = '代码已存在,必须唯一'
}
if (form.value.name.trim() === '') next.name = '名称必填'
const awardRanks = Number(form.value.awardRanks)
if (!Number.isInteger(awardRanks) || awardRanks < 0 || awardRanks > 100) next.awardRanks = '录取名次为 0–100 的整数'
if (form.value.rules.length === 0) {
next.rules = '至少配置一条名次分值'
} else {
const ranks = new Set<number>()
for (const rule of form.value.rules) {
const rank = Number(rule.rank)
const points = Number(rule.points)
if (!Number.isInteger(rank) || rank < 1 || rank > 1000) {
next.rules = '名次为 1–1000 的整数'
break
}
if (ranks.has(rank)) {
next.rules = `名次 ${rank} 重复`
break
}
ranks.add(rank)
if (!Number.isFinite(points) || points < 0 || points > 10000) {
next.rules = '分值为 0–10000 的数'
break
}
}
}
errors.value = next
return Object.keys(next).length === 0
}
function buildInput(): ScoringProfileInput {
return {
code: form.value.code.trim(),
name: form.value.name.trim(),
tieMode: form.value.tieMode,
recordBonusMode: form.value.recordBonusMode,
awardRanks: Number(form.value.awardRanks),
lastPlaceZero: form.value.lastPlaceZero,
rules: [...form.value.rules]
.map((rule) => ({ rank: Number(rule.rank), points: Number(rule.points) }))
.sort((a, b) => a.rank - b.rank),
}
}
function requestSave(): void {
if (!validate()) {
toast.error('积分模板校验未通过', '请先修正标红的字段')
return
}
const currentRanks = new Set(form.value.rules.map((rule) => Number(rule.rank)))
removedRules.value =
selectedProfile.value === null
? []
: selectedProfile.value.rules
.filter((rule) => !currentRanks.has(rule.rank))
.map((rule) => `第 ${rule.rank} 名(${rule.points} 分)`)
if (removedRules.value.length > 0) {
confirmOpen.value = true
return
}
void save()
}
async function save(): Promise<void> {
pending.value = true
formError.value = null
try {
const saved = await data.saveProfile(buildInput(), selectedId.value)
toast.success(creating.value ? '积分模板已新增' : '积分模板已保存')
creating.value = false
confirmOpen.value = false
loadProfile(saved)
} catch (caught) {
formError.value = caught instanceof Error ? caught.message : '保存失败'
} finally {
pending.value = false
}
}
const totalTop = computed(() => {
const first = form.value.rules.find((rule) => Number(rule.rank) === 1)
return first === undefined ? null : Number(first.points)
})
</script>
<template>
<div class="bd-split">
<section class="mx-card">
<div class="mx-card-head">
<span class="mx-card-title">积分模板</span>
<button class="mx-btn mx-btn-ghost mx-btn-sm" type="button" :disabled="!data.canWrite.value" @click="startCreate">
<MxIcon name="entry" :size="14" /> 新增
</button>
</div>
<div class="mx-card-body">
<div v-if="data.loading.value && data.profiles.value.length === 0" class="mx-empty bd-empty">
<span class="mx-spinner" />
<div class="mx-empty-title">加载中…</div>
</div>
<div v-else-if="data.profiles.value.length === 0 && !creating" class="mx-empty bd-empty">
<div class="mx-empty-title">还没有积分模板</div>
<div class="mx-empty-desc">模板定义「第几名得几分」,由计分通道引用</div>
</div>
<div v-else class="bd-list">
<button
v-if="creating"
class="bd-list-item active"
type="button"
>
<span class="bd-list-main">
<span>新模板</span>
<small>未保存</small>
</span>
</button>
<button
v-for="profile in data.profiles.value"
:key="profile.id"
class="bd-list-item"
:class="{ active: !creating && profile.id === selectedId }"
type="button"
@click="loadProfile(profile)"
>
<span class="bd-list-main">
<span>{{ profile.name }}</span>
<small>{{ profile.code }} · 录取前 {{ profile.awardRanks ?? profile.rules.length }} 名 · {{ profile.rules.length }} 条明细</small>
</span>
</button>
</div>
</div>
</section>
<section class="mx-card">
<div class="mx-card-head">
<span class="mx-card-title">{{ creating ? '新建积分模板' : (selectedProfile?.name ?? '积分模板明细') }}</span>
<button
class="mx-btn mx-btn-primary mx-btn-sm"
type="button"
:disabled="!data.canWrite.value || !isEditing || pending"
@click="requestSave"
>
{{ pending ? '保存中…' : '保存模板' }}
</button>
</div>
<div class="mx-card-body">
<div v-if="!isEditing" class="mx-empty bd-empty">
<div class="mx-empty-title">选择左侧模板查看明细</div>
<div class="mx-empty-desc">或点「新增」创建一张新模板</div>
</div>
<template v-else>
<div v-if="formError" class="mx-badge mx-badge-error form-error">{{ formError }}</div>
<div class="bd-form-grid">
<FormField label="代码" required :error="errors.code" hint="规则包跨赛事引用用代码,不用 id">
<input v-model="form.code" class="mx-cell-input" placeholder="例如 individual" />
</FormField>
<FormField label="名称" required :error="errors.name">
<input v-model="form.name" class="mx-cell-input" placeholder="例如 个人项目前六名" />
</FormField>
<FormField label="并列处理" hint="多人同名次时如何分配后续名次">
<select v-model="form.tieMode" class="mx-cell-input">
<option v-for="option in TIE_MODE_OPTIONS" :key="option.value" :value="option.value">{{ option.label }}</option>
</select>
</FormField>
<FormField label="破纪录加分" hint="一个项目多次破纪录时的取值方式">
<select v-model="form.recordBonusMode" class="mx-cell-input">
<option v-for="option in BONUS_MODE_OPTIONS" :key="option.value" :value="option.value">{{ option.label }}</option>
</select>
</FormField>
<FormField label="录取名次" :error="errors.awardRanks" hint="超过该名次不计分">
<input v-model="form.awardRanks" class="mx-cell-input" type="number" min="0" max="100" />
</FormField>
<FormField label="末位归零" hint="最后一名是否计 0 分">
<label class="bd-check"><input v-model="form.lastPlaceZero" type="checkbox" /> 最后一名记 0 分</label>
</FormField>
</div>
<div class="rules-head">
<span class="mx-label">名次 → 分值</span>
<div class="rules-meta">
<span v-if="totalTop !== null" class="bd-note">第一名 {{ totalTop }} 分</span>
<button class="mx-btn mx-btn-ghost mx-btn-sm" type="button" :disabled="!data.canWrite.value" @click="addRule">
<MxIcon name="entry" :size="14" /> 增加一行
</button>
</div>
</div>
<span v-if="errors.rules" class="row-error">{{ errors.rules }}</span>
<div class="mx-table-wrap rules-table">
<table class="mx-table mx-table-compact">
<thead>
<tr>
<th class="bd-cell-narrow">名次</th>
<th class="bd-cell-narrow">分值</th>
<th class="right">操作</th>
</tr>
</thead>
<tbody>
<tr v-for="rule in form.rules" :key="rule.key">
<td><input v-model="rule.rank" class="mx-cell-input bd-mono" type="number" min="1" max="1000" /></td>
<td><input v-model="rule.points" class="mx-cell-input bd-mono" type="number" min="0" step="0.5" /></td>
<td>
<div class="bd-actions">
<button class="mx-btn mx-btn-ghost mx-btn-sm danger-text" type="button" :disabled="!data.canWrite.value" @click="removeRule(rule.key)">
移除
</button>
</div>
</td>
</tr>
<tr v-if="form.rules.length === 0">
<td colspan="3">
<div class="mx-empty bd-empty">
<div class="mx-empty-title">没有分值明细</div>
<div class="mx-empty-desc">至少配置一条,否则通道无法算分</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</template>
</div>
</section>
<section class="mx-card global-card">
<div class="mx-card-head">
<span class="mx-card-title">榜单口径<span class="mx-badge mx-badge-neutral global-badge">全局设置</span></span>
<button
class="mx-btn mx-btn-primary mx-btn-sm"
type="button"
:disabled="!isAdmin || !weightDirty || weightSaving"
@click="saveWeight"
>
{{ weightSaving ? '保存中…' : '保存榜单口径' }}
</button>
</div>
<div class="mx-card-body">
<div class="bd-form-grid">
<FormField
label="个人项目得分折算系数"
:error="weightError"
hint="个人项目得分按该系数折算计入单位总分(全局设置,对所有模板生效)。默认 1,步进 0.1;服务端要求大于 0、不超过 100。"
>
<input
v-model="weightInput"
class="mx-cell-input"
type="number"
min="0"
max="100"
step="0.1"
:disabled="!isAdmin || weightSaving"
placeholder="1"
/>
</FormField>
</div>
<p class="bd-note">
这是<strong>全局</strong>设置(standings.individual_to_team_weight),不随某一张积分模板保存,所有模板与所有计分通道共用:
单位总分 = Σ 只记单位的流水 + 系数 × Σ 记到个人的流水。导入规则包时,规则包里的同名配置会覆盖这里。
</p>
<p v-if="!isAdmin" class="bd-note weight-readonly">
只有管理员能读写赛事设置(GET / PUT /api/settings 仅管理员可用),当前账号为只读:读不到具体系数,榜单按库里已有取值折算(未配置时为 1)。
</p>
<p v-else-if="storedWeight === null" class="bd-note">
赛事设置尚未加载(未登录或服务端未连接),暂时读不到当前系数。
</p>
</div>
</section>
<ConfirmDialog
:open="confirmOpen"
tone="warning"
title="保存将移除分值明细?"
:message="`本次保存会删除 ${removedRules.length} 条名次分值,之后的计分将不再包含这些名次。`"
:details="[...removedRules, '已发布的成绩不会自动重算,需要重新发布才生效']"
confirm-text="确认保存"
:pending="pending"
@confirm="save"
@cancel="confirmOpen = false"
/>
</div>
</template>
<style scoped>
.danger-text {
color: var(--mx-red);
}
/* 榜单口径是全局设置,横跨列表与明细两栏,避免被当成某张模板的一部分。 */
.global-card {
grid-column: 1 / -1;
}
.global-badge {
margin-left: 8px;
vertical-align: middle;
}
.weight-readonly {
color: var(--mx-amber);
}
.form-error {
display: block;
margin-bottom: 14px;
padding: 10px 12px;
line-height: 1.5;
}
.rules-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin: 18px 0 8px;
flex-wrap: wrap;
}
.rules-meta {
display: flex;
align-items: center;
gap: 12px;
}
.row-error {
display: block;
margin-bottom: 8px;
font-size: 12px;
color: var(--mx-red);
}
.rules-table {
max-width: 420px;
}
</style>