mirror of
https://github.com/hicccc77/WeFlow.git
synced 2026-04-06 15:08:20 +00:00
实现 #580 引用消息支持部分引用显示
This commit is contained in:
@@ -4486,15 +4486,16 @@ class ChatService {
|
||||
*/
|
||||
private parseQuoteMessage(content: string): { content?: string; sender?: string } {
|
||||
try {
|
||||
const normalizedContent = this.decodeHtmlEntities(content || '')
|
||||
// 提取 refermsg 部分
|
||||
const referMsgStart = content.indexOf('<refermsg>')
|
||||
const referMsgEnd = content.indexOf('</refermsg>')
|
||||
const referMsgStart = normalizedContent.indexOf('<refermsg>')
|
||||
const referMsgEnd = normalizedContent.indexOf('</refermsg>')
|
||||
|
||||
if (referMsgStart === -1 || referMsgEnd === -1) {
|
||||
return {}
|
||||
}
|
||||
|
||||
const referMsgXml = content.substring(referMsgStart, referMsgEnd + 11)
|
||||
const referMsgXml = normalizedContent.substring(referMsgStart, referMsgEnd + 11)
|
||||
|
||||
// 提取发送者名称
|
||||
let displayName = this.extractXmlValue(referMsgXml, 'displayname')
|
||||
@@ -4511,8 +4512,8 @@ class ChatService {
|
||||
let displayContent = referContent
|
||||
switch (referType) {
|
||||
case '1':
|
||||
// 文本消息,清理可能的 wxid
|
||||
displayContent = this.sanitizeQuotedContent(referContent)
|
||||
// 文本消息优先取“部分引用”字段,缺失时再回退到完整 content
|
||||
displayContent = this.extractPreferredQuotedText(referMsgXml)
|
||||
break
|
||||
case '3':
|
||||
displayContent = '[图片]'
|
||||
@@ -4552,6 +4553,76 @@ class ChatService {
|
||||
}
|
||||
}
|
||||
|
||||
private extractPreferredQuotedText(referMsgXml: string): string {
|
||||
if (!referMsgXml) return ''
|
||||
|
||||
const sources = [this.decodeHtmlEntities(referMsgXml)]
|
||||
const rawMsgSource = this.extractXmlValue(referMsgXml, 'msgsource')
|
||||
if (rawMsgSource) {
|
||||
const decodedMsgSource = this.decodeHtmlEntities(rawMsgSource)
|
||||
if (decodedMsgSource) {
|
||||
sources.push(decodedMsgSource)
|
||||
}
|
||||
}
|
||||
|
||||
const fullContent = this.sanitizeQuotedContent(this.extractXmlValue(sources[0] || referMsgXml, 'content'))
|
||||
const partialText = this.extractPartialQuotedText(sources[0] || referMsgXml, fullContent)
|
||||
if (partialText) return partialText
|
||||
|
||||
const candidateTags = [
|
||||
'selectedcontent',
|
||||
'selectedtext',
|
||||
'selectcontent',
|
||||
'selecttext',
|
||||
'quotecontent',
|
||||
'quotetext',
|
||||
'partcontent',
|
||||
'parttext',
|
||||
'excerpt',
|
||||
'summary',
|
||||
'preview'
|
||||
]
|
||||
|
||||
for (const source of sources) {
|
||||
for (const tag of candidateTags) {
|
||||
const value = this.sanitizeQuotedContent(this.extractXmlValue(source, tag))
|
||||
if (value) return value
|
||||
}
|
||||
}
|
||||
|
||||
return fullContent
|
||||
}
|
||||
|
||||
private extractPartialQuotedText(xml: string, fullContent: string): string {
|
||||
if (!xml || !fullContent) return ''
|
||||
|
||||
const startChar = this.extractXmlValue(xml, 'start')
|
||||
const endChar = this.extractXmlValue(xml, 'end')
|
||||
const startIndexRaw = this.extractXmlValue(xml, 'startindex')
|
||||
const endIndexRaw = this.extractXmlValue(xml, 'endindex')
|
||||
const startIndex = Number.parseInt(startIndexRaw, 10)
|
||||
const endIndex = Number.parseInt(endIndexRaw, 10)
|
||||
|
||||
if (startChar && endChar) {
|
||||
const startPos = fullContent.indexOf(startChar)
|
||||
if (startPos !== -1) {
|
||||
const endPos = fullContent.indexOf(endChar, startPos + startChar.length - 1)
|
||||
if (endPos !== -1 && endPos >= startPos) {
|
||||
const sliced = fullContent.slice(startPos, endPos + endChar.length).trim()
|
||||
if (sliced) return sliced
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Number.isFinite(startIndex) && Number.isFinite(endIndex) && endIndex >= startIndex) {
|
||||
const chars = Array.from(fullContent)
|
||||
const sliced = chars.slice(startIndex, endIndex + 1).join('').trim()
|
||||
if (sliced) return sliced
|
||||
}
|
||||
|
||||
return ''
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析名片消息
|
||||
* 格式: <msg username="wxid_xxx" nickname="昵称" ... />
|
||||
|
||||
Reference in New Issue
Block a user