- 评论页注入绝对 API_BASE:弹窗 WebView(loadDataWithBaseURL) 中相对路径 fetch 解析不可靠,切换排序/懒加载/回复全部改走绝对地址 - externalBaseURL 识别 X-Forwarded-Proto:正文气泡 ident 与评论页地址 不再硬编码 http://,避免反代 301 - 布局改造:顶栏固定(含拖拽把手视觉)+评论区独立滚动容器,滚动/回顶 逻辑迁移至容器;switchSort 显式传参替代隐式 event
803 lines
30 KiB
HTML
803 lines
30 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
|
|
<title>番茄段评 · 第 {{.ParaPlus1}} 段</title>
|
|
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.css">
|
|
<meta name="color-scheme" content="light dark">
|
|
<style>
|
|
:root {
|
|
/* 浅色模式变量 */
|
|
--bg-primary: #f5f5f5;
|
|
--bg-secondary: #ffffff;
|
|
--text-primary: #333333;
|
|
--text-secondary: #666666;
|
|
--text-tertiary: #999999;
|
|
--border-color: #e5e5e5;
|
|
--shadow: rgba(0, 0, 0, 0.05);
|
|
--badge-bg: #f0f5ff;
|
|
--badge-color: #4a80f0;
|
|
--tag-bg: #f0f0f0;
|
|
--tag-color: #666666;
|
|
--avatar-bg: #f0f0f0;
|
|
--loading-color: #666666;
|
|
}
|
|
|
|
/* 深色模式变量 */
|
|
@media (prefers-color-scheme: dark) {
|
|
:root {
|
|
--bg-primary: #000000;
|
|
--bg-secondary: #1c1c1e;
|
|
--text-primary: #ffffff;
|
|
--text-secondary: #ebebf5;
|
|
--text-tertiary: #8e8e93;
|
|
--border-color: #38383a;
|
|
--shadow: rgba(0, 0, 0, 0.3);
|
|
--badge-bg: #1c3a5f;
|
|
--badge-color: #64a3ff;
|
|
--tag-bg: #2c2c2e;
|
|
--tag-color: #ebebf5;
|
|
--avatar-bg: #2c2c2e;
|
|
--loading-color: #8e8e93;
|
|
}
|
|
}
|
|
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
box-sizing: border-box;
|
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
|
|
}
|
|
html, body { height: 100%; }
|
|
body {
|
|
background-color: var(--bg-primary);
|
|
color: var(--text-primary);
|
|
max-width: 600px;
|
|
margin: 0 auto;
|
|
display: flex;
|
|
flex-direction: column;
|
|
overflow: hidden;
|
|
transition: background-color 0.3s ease, color 0.3s ease;
|
|
}
|
|
.header {
|
|
flex-shrink: 0;
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
padding: 10px 15px 12px;
|
|
background: var(--bg-secondary);
|
|
border-radius: 20px 20px 0 0;
|
|
border-bottom: 1px solid var(--border-color);
|
|
box-shadow: 0 2px 8px var(--shadow);
|
|
position: relative;
|
|
z-index: 10;
|
|
transition: background-color 0.3s ease;
|
|
}
|
|
/* BottomSheet 顶部拖拽把手 */
|
|
.header::before {
|
|
content: '';
|
|
position: absolute;
|
|
top: 6px;
|
|
left: 50%;
|
|
transform: translateX(-50%);
|
|
width: 36px;
|
|
height: 4px;
|
|
border-radius: 2px;
|
|
background: var(--border-color);
|
|
}
|
|
#comment-list {
|
|
flex: 1;
|
|
overflow-y: auto;
|
|
-webkit-overflow-scrolling: touch;
|
|
padding: 15px;
|
|
overscroll-behavior: contain;
|
|
}
|
|
.header h1 {
|
|
font-size: 18px;
|
|
color: var(--text-primary);
|
|
margin: 0;
|
|
flex: 1;
|
|
}
|
|
.source-tag {
|
|
display: inline-block;
|
|
padding: 3px 8px;
|
|
background-color: var(--tag-bg);
|
|
border-radius: 4px;
|
|
font-size: 14px;
|
|
margin-left: 8px;
|
|
color: var(--tag-color);
|
|
transition: background-color 0.3s ease, color 0.3s ease;
|
|
}
|
|
.comment-item {
|
|
background: var(--bg-secondary);
|
|
border-radius: 12px;
|
|
padding: 15px;
|
|
box-shadow: 0 2px 8px var(--shadow);
|
|
margin-bottom: 15px;
|
|
transition: background-color 0.3s ease;
|
|
}
|
|
.user-info {
|
|
display: flex;
|
|
align-items: center;
|
|
margin-bottom: 10px;
|
|
}
|
|
.user-avatar, .reply-avatar {
|
|
width: 40px;
|
|
height: 40px;
|
|
border-radius: 50%;
|
|
margin-right: 10px;
|
|
background: var(--avatar-bg);
|
|
object-fit: cover;
|
|
}
|
|
.reply-avatar {
|
|
width: 32px;
|
|
height: 32px;
|
|
}
|
|
.user-name {
|
|
font-weight: 500;
|
|
font-size: 15px;
|
|
color: var(--text-primary);
|
|
}
|
|
.author-badge {
|
|
background: var(--badge-bg);
|
|
color: var(--badge-color);
|
|
font-size: 12px;
|
|
padding: 0 6px;
|
|
border-radius: 3px;
|
|
margin-left: 6px;
|
|
}
|
|
.user-sticker {
|
|
height: 18px;
|
|
margin-left: 6px;
|
|
vertical-align: middle;
|
|
}
|
|
.comment-content {
|
|
font-size: 15px;
|
|
line-height: 1.5;
|
|
margin-bottom: 10px;
|
|
white-space: pre-line;
|
|
}
|
|
.comment-img {
|
|
max-width: 100%;
|
|
border-radius: 6px;
|
|
margin: 8px 0;
|
|
cursor: pointer;
|
|
background: #eee;
|
|
}
|
|
.emoji-img {
|
|
width: 22px;
|
|
height: 22px;
|
|
vertical-align: middle;
|
|
margin: 0 2px;
|
|
}
|
|
.comment-time {
|
|
font-size: 12px;
|
|
color: var(--text-tertiary);
|
|
margin-bottom: 10px;
|
|
}
|
|
.comment-meta {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 15px;
|
|
font-size: 13px;
|
|
color: var(--text-secondary);
|
|
}
|
|
.comment-meta i {
|
|
margin-right: 4px;
|
|
font-size: 14px;
|
|
}
|
|
.comment-meta span {
|
|
cursor: pointer;
|
|
display: inline-flex;
|
|
align-items: center;
|
|
padding: 4px 8px;
|
|
border-radius: 6px;
|
|
transition: all 0.2s ease;
|
|
}
|
|
.comment-meta span:hover {
|
|
background: var(--bg-secondary);
|
|
color: var(--text-primary);
|
|
}
|
|
.comment-meta span:hover i {
|
|
transform: scale(1.1);
|
|
}
|
|
.reply-btn {
|
|
transition: all 0.2s ease;
|
|
}
|
|
.reply-btn:active {
|
|
transform: scale(0.95);
|
|
opacity: 0.4;
|
|
}
|
|
.replies-container {
|
|
margin-top: 15px;
|
|
padding-left: 25px;
|
|
position: relative;
|
|
display: none;
|
|
}
|
|
.replies-container.show {
|
|
display: block;
|
|
}
|
|
.replies-container::before {
|
|
content: '';
|
|
position: absolute;
|
|
left: 8px;
|
|
top: 0;
|
|
bottom: 0;
|
|
width: 2px;
|
|
background: var(--border-color);
|
|
}
|
|
.reply-item {
|
|
display: flex;
|
|
align-items: flex-start;
|
|
margin-bottom: 15px;
|
|
}
|
|
.reply-content {
|
|
flex: 1;
|
|
padding-top: 2px;
|
|
}
|
|
.reply-username {
|
|
font-weight: 500;
|
|
font-size: 14px;
|
|
margin-right: 5px;
|
|
color: var(--text-primary);
|
|
}
|
|
.reply-text {
|
|
font-size: 14px;
|
|
line-height: 1.4;
|
|
margin: 5px 0;
|
|
color: var(--text-primary);
|
|
}
|
|
.reply-time {
|
|
font-size: 11px;
|
|
color: var(--text-tertiary);
|
|
margin-top: 3px;
|
|
}
|
|
.image-viewer {
|
|
position: fixed;
|
|
top: 0;
|
|
left: 0;
|
|
right: 0;
|
|
bottom: 0;
|
|
background: rgba(0,0,0,0.9);
|
|
display: none;
|
|
justify-content: center;
|
|
align-items: center;
|
|
z-index: 1000;
|
|
}
|
|
.image-viewer.show {
|
|
display: flex;
|
|
}
|
|
.image-viewer img {
|
|
max-width: 90%;
|
|
max-height: 90%;
|
|
}
|
|
.close-btn {
|
|
position: absolute;
|
|
top: 20px;
|
|
right: 20px;
|
|
color: #fff;
|
|
font-size: 30px;
|
|
cursor: pointer;
|
|
}
|
|
.empty, .loading {
|
|
text-align: center;
|
|
color: var(--text-tertiary);
|
|
}
|
|
.empty {
|
|
padding: 20px;
|
|
background: var(--bg-secondary);
|
|
border-radius: 12px;
|
|
}
|
|
.loading {
|
|
padding: 10px;
|
|
color: var(--loading-color);
|
|
}
|
|
.sort-tabs {
|
|
display: flex;
|
|
gap: 0;
|
|
background: var(--bg-primary);
|
|
border-radius: 20px;
|
|
padding: 3px;
|
|
}
|
|
.sort-tab {
|
|
padding: 6px 16px;
|
|
background: transparent;
|
|
border: none;
|
|
border-radius: 17px;
|
|
cursor: pointer;
|
|
font-size: 14px;
|
|
color: var(--text-secondary);
|
|
transition: all 0.2s ease;
|
|
user-select: none;
|
|
white-space: nowrap;
|
|
}
|
|
.sort-tab.active {
|
|
background: var(--bg-secondary);
|
|
color: var(--text-primary);
|
|
font-weight: 500;
|
|
box-shadow: 0 1px 3px var(--shadow);
|
|
}
|
|
.sort-tab:hover:not(.active) {
|
|
opacity: 0.7;
|
|
}
|
|
.back-to-top {
|
|
position: fixed;
|
|
bottom: 30px;
|
|
right: 30px;
|
|
width: 50px;
|
|
height: 50px;
|
|
background: var(--bg-secondary);
|
|
border-radius: 50%;
|
|
display: none;
|
|
align-items: center;
|
|
justify-content: center;
|
|
cursor: pointer;
|
|
box-shadow: 0 4px 12px var(--shadow);
|
|
transition: all 0.3s ease;
|
|
z-index: 999;
|
|
border: 1px solid var(--border-color);
|
|
}
|
|
.back-to-top.show {
|
|
display: flex;
|
|
}
|
|
.back-to-top:hover {
|
|
transform: translateY(-5px);
|
|
box-shadow: 0 6px 16px var(--shadow);
|
|
}
|
|
.back-to-top i {
|
|
font-size: 20px;
|
|
color: var(--text-primary);
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="header">
|
|
<h1><span class="source-tag">段落评论 · {{.Total}} 条</span></h1>
|
|
<span style="display: flex; align-items: center;">
|
|
<i class="fas fa-book-open" style="color: var(--text-tertiary); font-size: 16px; margin: 0 10px;"></i>
|
|
</span>
|
|
<div class="sort-tabs">
|
|
<div class="sort-tab{{if eq .Sort 1}} active{{end}}" onclick="switchSort(1, this)">最热</div>
|
|
<div class="sort-tab{{if eq .Sort 3}} active{{end}}" onclick="switchSort(3, this)">最新</div>
|
|
</div>
|
|
</div>
|
|
<div id="comment-list">
|
|
{{if not .Comments}} <div class="empty">暂无评论</div>{{else}}{{range $i, $c := .Comments}} <div class="comment-item" id="comment-{{$i}}">
|
|
<div class="user-info">
|
|
<img src="{{$c.UserAvatar}}" class="user-avatar" alt="{{$c.UserName}}的头像" onerror="this.style.display='none'" loading="lazy">
|
|
<div class="user-name">{{$c.UserName}}{{if $c.IsAuthor}}<span class="author-badge">作者</span>{{end}}{{if $c.StickerImageURL}}<img src="{{$c.StickerImageURL}}" class="user-sticker" alt="勋章">{{end}}</div>
|
|
</div>
|
|
<div class="comment-content">{{$c.CommentTextHTML}}</div>
|
|
{{if $c.ImageURL}} <img src="{{$c.ImageURL}}" class="comment-img" alt="评论图片" onclick="showImage('{{$c.ImageLargeURL}}')" onerror="this.style.display='none'" loading="lazy">
|
|
{{end}} <div class="comment-time">{{$c.CreateTime}}</div>
|
|
<div class="comment-meta">
|
|
<span><i class="far fa-heart"></i>{{$c.DiggCount}}</span>
|
|
<span class="reply-btn" onclick="loadReplies({{$i}}, '{{$c.CommentID}}', {{$c.ReplyCount}})"><i class="far fa-comment-dots"></i>{{$c.ReplyCount}}</span>
|
|
</div>
|
|
<div class="replies-container" id="replies-{{$i}}"></div>
|
|
</div>
|
|
{{end}}{{end}}{{if and (not .HasMore) .Comments}}
|
|
<div class="empty">没有更多评论了</div>{{end}}
|
|
</div>
|
|
|
|
<div class="image-viewer" id="imageViewer">
|
|
<span class="close-btn" onclick="closeImage()">×</span>
|
|
<img id="previewImage" src="" alt="图片预览">
|
|
</div>
|
|
|
|
<div class="back-to-top" id="backToTop" onclick="scrollToTop()">
|
|
<i class="fas fa-arrow-up"></i>
|
|
</div>
|
|
|
|
<script>
|
|
const itemID = '{{.ItemID}}';
|
|
const bookID = '{{.BookID}}';
|
|
const para = {{.Para}};
|
|
let currentSort = {{.Sort}};
|
|
// 服务端注入的绝对地址:弹窗 WebView(loadDataWithBaseURL) 中相对路径解析不可靠
|
|
const API_BASE = '{{.BaseURL}}';
|
|
|
|
const commentList = document.getElementById('comment-list');
|
|
const imageViewer = document.getElementById('imageViewer');
|
|
const backToTopBtn = document.getElementById('backToTop');
|
|
|
|
const LOADING_HTML = '<div class="loading"><i class="fas fa-spinner fa-spin"></i> 加载中...</div>';
|
|
|
|
// 已加载回复的评论
|
|
const loadedReplies = new Set();
|
|
|
|
// URL 构建(绝对地址,兼容弹窗 WebView)
|
|
function buildReviewURL(params) {
|
|
const baseParams = {
|
|
item_id: itemID,
|
|
book_id: bookID,
|
|
para: para
|
|
};
|
|
const query = new URLSearchParams({...baseParams, ...params});
|
|
return API_BASE + '/review?' + query.toString();
|
|
}
|
|
|
|
// 时间格式化
|
|
function formatTime(timestamp, format = 'simple') {
|
|
const date = new Date(timestamp * 1000);
|
|
if (format === 'full') {
|
|
return date.toLocaleString('zh-CN', {
|
|
year: 'numeric',
|
|
month: '2-digit',
|
|
day: '2-digit',
|
|
hour: '2-digit',
|
|
minute: '2-digit'
|
|
}).replace(/\//g, '-');
|
|
}
|
|
return date.toLocaleString('zh-CN');
|
|
}
|
|
|
|
// 图片渲染
|
|
function renderImage(imageData, type = 'comment') {
|
|
if (!imageData?.[0]) return '';
|
|
const imgUrl = imageData[0].expand_web_url || imageData[0].web_uri || '';
|
|
if (!imgUrl) return '';
|
|
|
|
const escapedUrl = escapeHtml(imgUrl);
|
|
if (type === 'reply') {
|
|
return '<img src="' + escapedUrl + '" class="comment-img" alt="回复图片" onclick="showImage(\'' + escapedUrl + '\')" onerror="this.style.display=\'none\'" style="max-width:200px;margin-top:5px;border-radius:4px;cursor:pointer;" loading="lazy">';
|
|
}
|
|
const largeUrl = escapeHtml(imageData[0].expand_web_url || imgUrl);
|
|
return '<img src="' + escapedUrl + '" class="comment-img" alt="评论图片" onclick="showImage(\'' + largeUrl + '\')" onerror="this.style.display=\'none\'" loading="lazy">';
|
|
}
|
|
|
|
// 防抖
|
|
function debounce(fn, delay) {
|
|
let timer;
|
|
return function(...args) {
|
|
clearTimeout(timer);
|
|
timer = setTimeout(() => fn.apply(this, args), delay);
|
|
};
|
|
}
|
|
|
|
// 滚动信息获取(评论区为独立滚动容器)
|
|
function getScrollInfo() {
|
|
const scrollTop = commentList.scrollTop;
|
|
const scrollHeight = commentList.scrollHeight;
|
|
const clientHeight = commentList.clientHeight;
|
|
return {
|
|
scrollTop,
|
|
scrollHeight,
|
|
clientHeight,
|
|
distanceFromBottom: scrollHeight - (scrollTop + clientHeight)
|
|
};
|
|
}
|
|
|
|
// 切换排序
|
|
async function switchSort(newSort, tab) {
|
|
if (currentSort === newSort) return;
|
|
|
|
document.querySelectorAll('.sort-tab').forEach(t => t.classList.remove('active'));
|
|
if (tab) tab.classList.add('active');
|
|
|
|
commentList.innerHTML = LOADING_HTML;
|
|
|
|
try {
|
|
currentSort = newSort;
|
|
currentOffset = 0;
|
|
|
|
const url = buildReviewURL({ offset: 0, count: 20, sort: newSort });
|
|
const response = await fetch(url);
|
|
const result = await response.json();
|
|
|
|
if (result.code === 0 && result.data && result.data.comments) {
|
|
hasMore = result.data.has_more || false;
|
|
currentOffset = result.data.next_offset || 0;
|
|
|
|
const comments = result.data.comments || [];
|
|
|
|
if (comments.length === 0) {
|
|
commentList.innerHTML = '<div class="empty">暂无评论</div>';
|
|
commentIndex = 0;
|
|
} else {
|
|
commentList.innerHTML = comments.map((comment, idx) => renderComment(comment, idx)).join('');
|
|
commentIndex = comments.length;
|
|
|
|
// 如果没有更多数据,显示提示
|
|
if (!hasMore) {
|
|
commentList.insertAdjacentHTML('beforeend', '<div class="empty">没有更多评论了</div>');
|
|
}
|
|
}
|
|
|
|
loadedReplies.clear();
|
|
commentList.scrollTo({ top: 0, behavior: 'smooth' });
|
|
} else {
|
|
commentList.innerHTML = '<div class="empty">加载失败,请重试</div>';
|
|
commentIndex = 0;
|
|
}
|
|
} catch (error) {
|
|
console.error('切换排序失败:', error);
|
|
commentList.innerHTML = '<div class="empty">加载失败,请重试</div>';
|
|
}
|
|
}
|
|
|
|
// 加载回复
|
|
async function loadReplies(idx, commentID, count) {
|
|
const container = document.getElementById('replies-' + idx);
|
|
|
|
if (count === 0) {
|
|
if (container.classList.contains('show')) {
|
|
container.classList.remove('show');
|
|
return;
|
|
}
|
|
container.innerHTML = '<div class="loading" style="color: #999;">暂无回复</div>';
|
|
container.classList.add('show');
|
|
loadedReplies.add(commentID);
|
|
return;
|
|
}
|
|
|
|
if (loadedReplies.has(commentID)) {
|
|
container.classList.toggle('show');
|
|
return;
|
|
}
|
|
|
|
container.innerHTML = LOADING_HTML;
|
|
container.classList.add('show');
|
|
|
|
try {
|
|
const url = buildReviewURL({ comment_id: commentID, offset: 0, count: 20 });
|
|
const response = await fetch(url);
|
|
const result = await response.json();
|
|
|
|
if (result.code !== 0 || !result.data) {
|
|
container.innerHTML = '<div class="loading">暂无回复</div>';
|
|
return;
|
|
}
|
|
|
|
const replies = result.data.reply_list || [];
|
|
const totalCount = result.data.count || replies.length;
|
|
const hasMore = result.data.has_more || false;
|
|
const nextOffset = result.data.next_offset || 0;
|
|
|
|
if (replies.length === 0) {
|
|
container.innerHTML = '<div class="loading">暂无回复</div>';
|
|
return;
|
|
}
|
|
|
|
let html = replies.map(renderReply).join('');
|
|
|
|
if (hasMore) {
|
|
html += '<div class="load-more-replies" style="text-align:center;padding:10px;color:var(--text-secondary);cursor:pointer;" ' +
|
|
'onclick="loadMoreReplies(\'' + escapeHtml(commentID) + '\', ' + nextOffset + ', this)">' +
|
|
'<i class="fas fa-angle-down"></i> 加载更多回复 (还有 ' + (totalCount - replies.length) + ' 条)' +
|
|
'</div>';
|
|
}
|
|
|
|
container.innerHTML = html;
|
|
loadedReplies.add(commentID);
|
|
} catch (error) {
|
|
console.error('加载回复失败:', error);
|
|
container.innerHTML = '<div class="loading">加载失败</div>';
|
|
}
|
|
}
|
|
|
|
// 加载更多回复
|
|
async function loadMoreReplies(commentID, offset, button) {
|
|
button.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 加载中...';
|
|
|
|
try {
|
|
const url = buildReviewURL({ comment_id: commentID, offset: offset, count: 20 });
|
|
const response = await fetch(url);
|
|
const result = await response.json();
|
|
|
|
if (result.code !== 0 || !result.data) {
|
|
button.textContent = '加载失败';
|
|
return;
|
|
}
|
|
|
|
const replies = result.data.reply_list || [];
|
|
const totalCount = result.data.count || 0;
|
|
const hasMore = result.data.has_more || false;
|
|
const nextOffset = result.data.next_offset || 0;
|
|
|
|
button.insertAdjacentHTML('beforebegin', replies.map(renderReply).join(''));
|
|
|
|
if (hasMore) {
|
|
const loadedCount = offset + replies.length;
|
|
button.innerHTML = '<i class="fas fa-angle-down"></i> 加载更多回复 (还有 ' + (totalCount - loadedCount) + ' 条)';
|
|
button.onclick = () => loadMoreReplies(commentID, nextOffset, button);
|
|
} else {
|
|
button.remove();
|
|
}
|
|
} catch (error) {
|
|
console.error('加载更多回复失败:', error);
|
|
button.textContent = '加载失败,点击重试';
|
|
}
|
|
}
|
|
|
|
// HTML 转义
|
|
const htmlEscapeMap = {
|
|
'&': '&',
|
|
'<': '<',
|
|
'>': '>',
|
|
'"': '"',
|
|
"'": '''
|
|
};
|
|
function escapeHtml(text) {
|
|
return text.replace(/[&<>"']/g, m => htmlEscapeMap[m]);
|
|
}
|
|
|
|
// 表情替换
|
|
const emojiMap = {{.EmojiMapJS}};
|
|
const emojiPattern = new RegExp(
|
|
Object.keys(emojiMap)
|
|
.map(e => e.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
|
|
.join('|'),
|
|
'g'
|
|
);
|
|
|
|
function replaceEmoji(text) {
|
|
return text.replace(emojiPattern, match =>
|
|
'<img src="' + emojiMap[match] + '" class="emoji-img" alt="' + match + '" title="' + match + '" loading="lazy">'
|
|
);
|
|
}
|
|
|
|
// 渲染单个回复
|
|
function renderReply(reply) {
|
|
const userName = reply.user_info?.user_name || '匿名用户';
|
|
const userAvatar = reply.user_info?.user_avatar || '';
|
|
const text = reply.text || '';
|
|
const time = formatTime(reply.create_timestamp || 0);
|
|
const imageHTML = renderImage(reply.image_data, 'reply');
|
|
|
|
return '<div class="reply-item">' +
|
|
'<img src="' + escapeHtml(userAvatar) + '" class="reply-avatar" alt="' + escapeHtml(userName) + '的头像" onerror="this.style.display=\'none\';" loading="lazy">' +
|
|
'<div class="reply-content">' +
|
|
'<span class="reply-username">' + escapeHtml(userName) + '</span>' +
|
|
'<div class="reply-text">' + replaceEmoji(escapeHtml(text)) + imageHTML + '</div>' +
|
|
'<div class="reply-time">' + time + '</div>' +
|
|
'</div>' +
|
|
'</div>';
|
|
}
|
|
|
|
// 渲染单个评论
|
|
function renderComment(comment, idx) {
|
|
const userName = comment.user_info?.user_name || '匿名用户';
|
|
const userAvatar = comment.user_info?.user_avatar || '';
|
|
const isAuthor = comment.user_info?.is_author || false;
|
|
const commentText = comment.text || '';
|
|
const commentID = comment.comment_id || '';
|
|
const diggCount = comment.digg_count || 0;
|
|
const replyCount = comment.reply_count || 0;
|
|
const createTime = formatTime(comment.create_timestamp || 0, 'full');
|
|
const imageHTML = renderImage(comment.image_data);
|
|
|
|
return '<div class="comment-item" id="comment-' + idx + '">' +
|
|
'<div class="user-info">' +
|
|
'<img src="' + escapeHtml(userAvatar) + '" class="user-avatar" alt="' + escapeHtml(userName) + '的头像" onerror="this.style.display=\'none\'" loading="lazy">' +
|
|
'<div class="user-name">' + escapeHtml(userName) +
|
|
(isAuthor ? '<span class="author-badge">作者</span>' : '') +
|
|
'</div></div>' +
|
|
'<div class="comment-content">' + replaceEmoji(escapeHtml(commentText)) + '</div>' +
|
|
imageHTML +
|
|
'<div class="comment-time">' + createTime + '</div>' +
|
|
'<div class="comment-meta">' +
|
|
'<span><i class="far fa-heart"></i>' + diggCount + '</span>' +
|
|
'<span class="reply-btn" onclick="loadReplies(' + idx + ', \'' + escapeHtml(commentID) + '\', ' + replyCount + ')">' +
|
|
'<i class="far fa-comment-dots"></i>' + replyCount + '</span>' +
|
|
'</div>' +
|
|
'<div class="replies-container" id="replies-' + idx + '"></div>' +
|
|
'</div>';
|
|
}
|
|
|
|
// 显示图片
|
|
function showImage(url) {
|
|
document.getElementById('previewImage').src = url;
|
|
imageViewer.classList.add('show');
|
|
document.body.style.overflow = 'hidden';
|
|
}
|
|
|
|
// 关闭图片
|
|
function closeImage() {
|
|
imageViewer.classList.remove('show');
|
|
document.body.style.overflow = '';
|
|
}
|
|
|
|
// 点击空白处关闭
|
|
imageViewer.addEventListener('click', e => {
|
|
if (e.target.id === 'imageViewer') closeImage();
|
|
});
|
|
|
|
// ESC 键关闭
|
|
document.addEventListener('keydown', e => {
|
|
if (e.key === 'Escape') closeImage();
|
|
});
|
|
|
|
// 懒加载相关变量
|
|
let currentOffset = {{.NextOffset}};
|
|
const pageSize = {{.PageSize}};
|
|
let hasMore = {{if .HasMore}}true{{else}}false{{end}};
|
|
let isLoading = false;
|
|
let commentIndex = {{.CommentCount}};
|
|
|
|
// 懒加载更多评论
|
|
async function loadMoreComments() {
|
|
if (isLoading || !hasMore) return;
|
|
|
|
isLoading = true;
|
|
|
|
let loadingDiv = document.getElementById('loading-indicator');
|
|
if (!loadingDiv) {
|
|
loadingDiv = document.createElement('div');
|
|
loadingDiv.id = 'loading-indicator';
|
|
loadingDiv.className = 'loading';
|
|
loadingDiv.innerHTML = LOADING_HTML;
|
|
commentList.appendChild(loadingDiv);
|
|
}
|
|
loadingDiv.style.display = 'block';
|
|
|
|
try {
|
|
const url = buildReviewURL({ offset: currentOffset, count: pageSize, sort: currentSort });
|
|
const response = await fetch(url);
|
|
const result = await response.json();
|
|
|
|
if (result.code === 0 && result.data) {
|
|
const comments = result.data.comments || [];
|
|
|
|
if (comments.length > 0) {
|
|
loadingDiv?.remove();
|
|
|
|
hasMore = result.data.has_more === true;
|
|
currentOffset = result.data.next_offset || 0;
|
|
|
|
comments.forEach((comment, idx) => {
|
|
commentList.insertAdjacentHTML('beforeend', renderComment(comment, commentIndex + idx));
|
|
});
|
|
|
|
commentIndex += comments.length;
|
|
|
|
if (!hasMore) {
|
|
commentList.insertAdjacentHTML('beforeend', '<div class="empty">没有更多评论了</div>');
|
|
}
|
|
} else {
|
|
loadingDiv.className = 'empty';
|
|
loadingDiv.textContent = '没有更多评论了';
|
|
hasMore = false;
|
|
}
|
|
} else {
|
|
loadingDiv.className = 'empty';
|
|
loadingDiv.textContent = '没有更多评论了';
|
|
hasMore = false;
|
|
}
|
|
} catch (error) {
|
|
console.error('加载失败:', error);
|
|
if (loadingDiv) {
|
|
loadingDiv.textContent = '加载失败';
|
|
setTimeout(() => loadingDiv.remove(), 2000);
|
|
}
|
|
} finally {
|
|
isLoading = false;
|
|
}
|
|
}
|
|
|
|
// 回到顶部
|
|
function scrollToTop() {
|
|
commentList.scrollTo({
|
|
top: 0,
|
|
behavior: 'smooth'
|
|
});
|
|
}
|
|
|
|
// 滚动监听(挂在评论容器上,顶栏固定不参与滚动)
|
|
const handleScroll = debounce(() => {
|
|
const scroll = getScrollInfo();
|
|
|
|
// 显示/隐藏回到顶部按钮
|
|
backToTopBtn.classList.toggle('show', scroll.scrollTop > 300);
|
|
|
|
// 距离底部 200px 时触发加载
|
|
if (scroll.distanceFromBottom <= 200) {
|
|
loadMoreComments();
|
|
}
|
|
}, 100);
|
|
|
|
commentList.addEventListener('scroll', handleScroll, { passive: true });
|
|
</script>
|
|
</body>
|