69 lines
1.7 KiB
JavaScript
69 lines
1.7 KiB
JavaScript
function formatDateTime(iso) {
|
||
if (!iso || typeof iso !== 'string') return '-';
|
||
// expected format: 2026-03-30T12:34:56
|
||
const s = iso.replace('T', ' ').replace('Z', '');
|
||
if (s.length > 19) return s.slice(0, 19);
|
||
return s;
|
||
}
|
||
|
||
function formatTime(iso) {
|
||
if (!iso || typeof iso !== 'string') return '-';
|
||
const s = iso.replace('T', ' ').replace('Z', '');
|
||
// 返回 yyyy-MM-dd HH:mm 格式
|
||
if (s.length >= 16) return s.slice(0, 16);
|
||
return s;
|
||
}
|
||
|
||
function eventStatusText(status) {
|
||
// 与后端 EventStatus 枚举一致
|
||
const strMap = {
|
||
NotStarted: '未开始',
|
||
Enrolling: '报名中',
|
||
RegistrationEnded: '报名结束',
|
||
Ongoing: '比赛中',
|
||
Ended: '已结束'
|
||
};
|
||
if (strMap[status] != null) return strMap[status];
|
||
|
||
const n = status === undefined || status === null || status === '' ? NaN : Number(status);
|
||
const numMap = {
|
||
0: '未开始',
|
||
1: '报名中',
|
||
2: '报名结束',
|
||
3: '比赛中',
|
||
4: '已结束'
|
||
};
|
||
if (!Number.isNaN(n) && numMap[n] !== undefined) return numMap[n];
|
||
return status !== undefined && status !== null && status !== '' ? `状态${status}` : '';
|
||
}
|
||
|
||
/** 列表角标样式用:0~4,未知时回退 0 */
|
||
function eventStatusForPill(status) {
|
||
const strToNum = {
|
||
NotStarted: 0,
|
||
Enrolling: 1,
|
||
RegistrationEnded: 2,
|
||
Ongoing: 3,
|
||
Ended: 4
|
||
};
|
||
if (strToNum[status] !== undefined) return strToNum[status];
|
||
const n = Number(status);
|
||
if (!Number.isNaN(n) && n >= 0 && n <= 4) return n;
|
||
return 0;
|
||
}
|
||
|
||
function eventFormatText(v) {
|
||
if (v === 'Offline') return '线下';
|
||
if (v === 'Online') return '线上';
|
||
return '';
|
||
}
|
||
|
||
module.exports = {
|
||
formatDateTime,
|
||
formatTime,
|
||
eventStatusText,
|
||
eventStatusForPill,
|
||
eventFormatText
|
||
};
|
||
|