50 lines
1.3 KiB
JavaScript
50 lines
1.3 KiB
JavaScript
/**
|
||
* 长连接 appWs:notice 与 NotificationTab.onAppWsNotice 同一套字段解析
|
||
* @param {object} msg
|
||
* @returns {{ time: string, text: string, id: string, picture: string } | null}
|
||
*/
|
||
export function parseAppWsNoticeDisplay(msg) {
|
||
if (!msg || typeof msg !== 'object') return null
|
||
const outer = msg.data
|
||
if (!outer || typeof outer !== 'object') return null
|
||
const row =
|
||
outer.data && typeof outer.data === 'object' && !Array.isArray(outer.data)
|
||
? outer.data
|
||
: outer
|
||
const createTime =
|
||
row.createTime || row.time || msg.createTime || outer.createTime || ''
|
||
const text =
|
||
row.content ||
|
||
row.message ||
|
||
row.title ||
|
||
outer.content ||
|
||
msg.content ||
|
||
''
|
||
if (!String(text).trim() && !createTime) return null
|
||
const id =
|
||
row.id != null && row.id !== ''
|
||
? String(row.id)
|
||
: row.noticeId != null && row.noticeId !== ''
|
||
? String(row.noticeId)
|
||
: ''
|
||
const picture = row.picture || outer.picture || msg.picture || ''
|
||
return {
|
||
time: String(createTime),
|
||
text: String(text),
|
||
id,
|
||
picture: String(picture || '').trim(),
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 展示用时间(原样或截断常见 ISO)
|
||
*/
|
||
export function formatNoticeTimeForBanner(raw) {
|
||
if (!raw) return ''
|
||
const s = String(raw).trim()
|
||
if (/^\d{4}-\d{2}-\d{2}[\sT]\d{2}:\d{2}/.test(s)) {
|
||
return s.replace('T', ' ').replace(/\.\d{3}Z?$/, '')
|
||
}
|
||
return s
|
||
}
|