import { formatTimeStamp } from '#shared/utils/helpers'; import { getCommentCache } from '~/store/v2/comment'; import { getCommentReplyCache } from '~/store/v2/comment_reply'; import { getMetadataCache } from '~/store/v2/metadata'; /** * 从文章 HTML 中提取 comment_id。 * 文本分享与普通图文的页面结构不同,因此需要兼容多种写法。 */ export function extractCommentId(html: string): string | null { const patterns = [ // 普通图文: var comment_id = 'xxx' || '0'; /var comment_id = '(?\d+)' \|\| '0';/, // 文本分享等: d.comment_id = xml ? getXmlValue('comment_id.DATA') : 'xxx'; /comment_id:\s*JsDecode\('(?\d+)'\)/, // 有些模板里以 JsDecode 形式写在配置里 /d\.comment_id\s*=\s*xml \? getXmlValue\('comment_id\.DATA'\) : '(?\d+)';/, // 兜底: window.segment_comment_id = 'xxx'; /window\.comment_id\s*=\s*'(?\d+)'/, ]; for (const pattern of patterns) { const match = html.match(pattern); if (match) { // 优先使用命名分组,否则退回第一个分组 if ('groups' in match && match.groups && match.groups.comment_id) { return match.groups.comment_id; } if (match[1]) { return match[1]; } } } return null; } /** * 渲染文章的评论内容 * @param url 文章链接 */ export async function renderComments(url: string) { let commentHTML = ''; let elected_comments = []; const commentCache = await getCommentCache(url); if (commentCache) { const commentResponse = commentCache.data; if (Array.isArray(commentResponse)) { elected_comments = commentResponse.flatMap(response => response.elected_comment); } else if (commentResponse) { elected_comments = commentResponse.elected_comment; } if (elected_comments.length > 0) { // 评论总数 const metadata = await getMetadataCache(url); commentHTML += '
'; commentHTML += `

留言 ${metadata?.commentNum}

`; commentHTML += '
'; // 留言列表 for (const comment of elected_comments) { commentHTML += '
'; if ([1, 2].includes(comment.identity_type)) { commentHTML += ``; } else { commentHTML += ``; } commentHTML += '

'; commentHTML += `${comment.nick_name}`; if (comment.is_from_friend === 1) { commentHTML += `朋友`; } if (comment.ip_wording) { commentHTML += `${comment.ip_wording?.province_name}`; } else { commentHTML += `作者`; } commentHTML += `${formatTimeStamp(comment.create_time)}`; commentHTML += ''; commentHTML += `${comment.like_num || ''}`; commentHTML += '

'; commentHTML += `

${comment.content}

`; if (comment.multi_info && comment.multi_info.pictures && comment.multi_info.pictures.length > 0) { commentHTML += `

${comment.multi_info.pictures.map((pic: any) => '').join('')}

`; } if (comment.author_like_status === 1) { commentHTML += '

作者赞过

'; } commentHTML += '
'; // 留言回复列表 let reply_list = []; const commentReplyResponse = await getCommentReplyCache(url, comment.content_id); if ( commentReplyResponse && commentReplyResponse.data && commentReplyResponse.data.reply_list.reply_list.length > 0 ) { reply_list = commentReplyResponse.data.reply_list.reply_list; } else if (comment.reply_new && comment.reply_new.reply_list.length > 0) { reply_list = comment.reply_new.reply_list; } commentHTML += '
'; reply_list .sort((a: any, b: any) => a.create_time - b.create_time) .forEach((reply: any) => { commentHTML += '
'; if ([1, 2].includes(reply.identity_type)) { commentHTML += ``; } else { commentHTML += ``; } commentHTML += '

'; commentHTML += `${reply.nick_name}`; if (reply.is_from_friend === 1) { commentHTML += `朋友`; } if (reply.ip_wording) { commentHTML += `${reply.ip_wording?.province_name}`; } else { commentHTML += `作者`; } commentHTML += `${formatTimeStamp(reply.create_time)}`; commentHTML += ''; commentHTML += `${reply.reply_like_num || ''}`; commentHTML += '

'; commentHTML += `

${reply.to_nick_name ? '回复 ' + reply.to_nick_name + ':' : ''} ${reply.content}

`; if (reply.multi_info && reply.multi_info.pictures && reply.multi_info.pictures.length > 0) { commentHTML += `

${reply.multi_info.pictures.map((pic: any) => '').join('')}

`; } if (reply.author_like_status === 1) { commentHTML += '

作者赞过

'; } commentHTML += '
'; }); commentHTML += '
'; commentHTML += '
'; } commentHTML += '
'; } } return commentHTML; } // 从本地缓存获取文章的评论数据 export async function getArticleComments(url: string) { let elected_comments = []; const commentCache = await getCommentCache(url); if (commentCache) { const commentResponse = commentCache.data; if (Array.isArray(commentResponse)) { elected_comments = commentResponse.flatMap(response => response.elected_comment); } else if (commentResponse) { elected_comments = commentResponse.elected_comment; } if (elected_comments.length > 0) { for (const comment of elected_comments) { // 留言回复列表 let reply_list = []; const commentReplyResponse = await getCommentReplyCache(url, comment.content_id); if ( commentReplyResponse && commentReplyResponse.data && commentReplyResponse.data.reply_list.reply_list.length > 0 ) { reply_list = commentReplyResponse.data.reply_list.reply_list; } else if (comment.reply_new && comment.reply_new.reply_list.length > 0) { reply_list = comment.reply_new.reply_list; } reply_list.sort((a: any, b: any) => a.create_time - b.create_time); comment.$reply_list = reply_list; } } } return elected_comments; }