File size: 9,165 Bytes
6e41657
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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 = '(?<comment_id>\d+)' \|\| '0';/,
    // 文本分享等: d.comment_id = xml ? getXmlValue('comment_id.DATA') : 'xxx';
    /comment_id:\s*JsDecode\('(?<comment_id>\d+)'\)/,
    // 有些模板里以 JsDecode 形式写在配置里
    /d\.comment_id\s*=\s*xml \? getXmlValue\('comment_id\.DATA'\) : '(?<comment_id>\d+)';/,
    // 兜底: window.segment_comment_id = 'xxx';
    /window\.comment_id\s*=\s*'(?<comment_id>\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 += '<div style="max-width: 667px;margin: 0 auto;padding: 10px 10px 80px;">';
      commentHTML += `<p style="font-size: 15px;color: #949494;">留言 ${metadata?.commentNum}</p>`;
      commentHTML += '<div style="margin-top: -10px;">';

      // 留言列表
      for (const comment of elected_comments) {
        commentHTML += '<div style="margin-top: 25px;"><div style="display: flex;">';
        if ([1, 2].includes(comment.identity_type)) {
          commentHTML += `<img src="${comment.logo_url}" style="display: block;width: 30px;height: 30px;border-radius: 50%;margin-right: 8px;" alt="">`;
        } else {
          commentHTML += `<img src="${comment.logo_url}" style="display: block;width: 30px;height: 30px;border-radius: 2px;margin-right: 8px;" alt="">`;
        }
        commentHTML += '<div style="flex: 1;"><p style="display: flex;line-height: 16px;margin-block: 5px;">';
        commentHTML += `<span style="margin-right: 5px;font-size: 15px;color: #949494;">${comment.nick_name}</span>`;
        if (comment.is_from_friend === 1) {
          commentHTML += `<span style="margin-right: 5px;font-size: 12px;color: #00BA5A;">朋友</span>`;
        }
        if (comment.ip_wording) {
          commentHTML += `<span style="margin-right: 5px;font-size: 12px;color: #b5b5b5;">${comment.ip_wording?.province_name}</span>`;
        } else {
          commentHTML += `<span style="margin-right: 5px;font-size: 12px;color: #00BA5A;">作者</span>`;
        }
        commentHTML += `<span style="font-size: 12px;color: #b5b5b5;">${formatTimeStamp(comment.create_time)}</span>`;
        commentHTML += '<span style="flex: 1;"></span><span style="display: inline-flex;align-items: center;">';
        commentHTML += `<span class="sns_opr_btn sns_praise_btn" style="font-size: 12px;color: #8b8a8a;">${comment.like_num || ''}</span>`;
        commentHTML += '</span></p>';
        commentHTML += `<p style="font-size: 15px;color: #333;white-space: pre-line;margin-block: .5em;">${comment.content}</p>`;
        if (comment.multi_info && comment.multi_info.pictures && comment.multi_info.pictures.length > 0) {
          commentHTML += `<p>${comment.multi_info.pictures.map((pic: any) => '<img src="' + pic.url + '" style="max-width: 100%;" alt="">').join('')}</p>`;
        }
        if (comment.author_like_status === 1) {
          commentHTML += '<p style="font-size: 12px;color: #00BA5A;margin-block: .5em;">作者赞过</p>';
        }
        commentHTML += '</div></div>';

        // 留言回复列表
        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 += '<div style="padding-left: 38px;">';
        reply_list
          .sort((a: any, b: any) => a.create_time - b.create_time)
          .forEach((reply: any) => {
            commentHTML += '<div style="display: flex;margin-top: 15px;">';
            if ([1, 2].includes(reply.identity_type)) {
              commentHTML += `<img src="${reply.logo_url}" style="display: block;width: 23px;height: 23px;border-radius: 50%;margin-right: 8px;" alt="">`;
            } else {
              commentHTML += `<img src="${reply.logo_url}" style="display: block;width: 23px;height: 23px;border-radius: 2px;margin-right: 8px;" alt="">`;
            }
            commentHTML += '<div style="flex: 1;"><p style="display: flex;line-height: 16px;margin-block: 5px;">';
            commentHTML += `<span style="margin-right: 5px;font-size: 15px;color: #949494;">${reply.nick_name}</span>`;

            if (reply.is_from_friend === 1) {
              commentHTML += `<span style="margin-right: 5px;font-size: 12px;color: #00BA5A;">朋友</span>`;
            }
            if (reply.ip_wording) {
              commentHTML += `<span style="margin-right: 5px;font-size: 12px;color: #b5b5b5;">${reply.ip_wording?.province_name}</span>`;
            } else {
              commentHTML += `<span style="margin-right: 5px;font-size: 12px;color: #00BA5A;">作者</span>`;
            }
            commentHTML += `<span style="font-size: 12px;color: #b5b5b5;">${formatTimeStamp(reply.create_time)}</span>`;
            commentHTML +=
              '<span style="flex: 1;"></span><span style="display: inline-flex;align-items: center; font-size: 12px;color: #b5b5b5;">';
            commentHTML += `<span class="sns_opr_btn sns_praise_btn" style="font-size: 12px;color: #8b8a8a;">${reply.reply_like_num || ''}</span>`;
            commentHTML += '</span></p>';
            commentHTML += `<p style="font-size: 15px;color: #333;white-space: pre-line;margin-block: .5em;">${reply.to_nick_name ? '回复 ' + reply.to_nick_name + ':' : ''} ${reply.content}</p>`;
            if (reply.multi_info && reply.multi_info.pictures && reply.multi_info.pictures.length > 0) {
              commentHTML += `<p>${reply.multi_info.pictures.map((pic: any) => '<img src="' + pic.url + '" style="max-width: 100%;" alt="">').join('')}</p>`;
            }
            if (reply.author_like_status === 1) {
              commentHTML += '<p style="font-size: 12px;color: #00BA5A;margin-block: .5em;">作者赞过</p>';
            }
            commentHTML += '</div></div>';
          });
        commentHTML += '</div>';

        commentHTML += '</div>';
      }

      commentHTML += '</div></div>';
    }
  }

  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;
}