topfans/frontend/pages/mailbox/components/postcardHelpers.js

62 lines
1.9 KiB
JavaScript

// 共享 helper:被 PostcardBase / PostcardFeedback / PostcardReport / PostcardTarget /
// PostcardActivity / Postcard dispatcher 共用。
// 抽出来避免每个 vue 组件重复 parseData / typeMap / actionLabel / actionColor / formatTime。
/**
* 解析 notification.data (后端 proto structpb.AsMap() 已经是 object;但老数据/测试数据可能是 JSON string)。
* 任何异常/非对象 → {}。
*/
export function parseData(raw) {
if (!raw) return {}
if (typeof raw === 'string') {
try { return JSON.parse(raw) } catch (e) { return {} }
}
if (typeof raw === 'object') return raw
return {}
}
// 类型 → 中文展示名 (与 useMailboxCenter.js#GROUPS.label 对齐)
export const typeMap = {
feedback_replied: '反馈回复',
report_resolved: '举报结果',
target_reported: '被举报',
activity: '活动通知',
}
export function typeLabel(type) {
return typeMap[type] || type || '通知'
}
// 处理动作 → 中文展示
export const actionLabelMap = {
takedown: '已下架',
ban: '已封禁',
warn: '已警告',
dismiss: '已驳回',
restore: '已解除',
}
export function actionLabel(a) {
return actionLabelMap[a] || a || ''
}
// 处理动作 → 角标 CSS class
// red=takedown/ban(严重), orange=warn(警告), green=restore(恢复), gray=dismiss(驳回)/未知
export function actionColorClass(a) {
switch (a) {
case 'takedown':
case 'ban': return 'badge-red'
case 'warn': return 'badge-orange'
case 'restore': return 'badge-green'
case 'dismiss': return 'badge-gray'
default: return 'badge-gray'
}
}
// 时间戳(秒/毫秒都兼容) → YYYY-MM-DD HH:MM
export function formatTime(ms) {
if (!ms) return ''
const d = new Date(typeof ms === 'number' && ms < 1e12 ? ms * 1000 : ms)
const pad = n => (n < 10 ? '0' + n : '' + n)
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}`
}