refactor: 提取公共工具函数到 shared/utils.js,消除 DRY 重复
- escapeHtml 8 处定义合并为 shared/utils.js 1 处:删除 shortcode.js、posts/show.html、settings/index.html、comments.js、energy.js、common.js 中的重复定义 - formatTime 6 处定义合并为 1 处:统一 ISO 时间格式化逻辑 - api() 5 处定义收敛:前台页面使用 shared/utils.js 统一 api(method,url,data),admin 使用 common.js 的 api 对象 - showToast 3 处定义合并为 1 处:自包含内联样式,前后台通用 - admin/posts.js api 调用迁移为 api.post()
This commit is contained in:
@ -376,17 +376,6 @@
|
||||
var currentFolderId = null;
|
||||
var loading = false;
|
||||
|
||||
function api(method, url, data) {
|
||||
var opts = { method: method, headers: { 'Content-Type': 'application/json' } };
|
||||
var token = getCSRFToken();
|
||||
if (token) opts.headers['X-CSRF-Token'] = token;
|
||||
if (data) opts.body = JSON.stringify(data);
|
||||
return fetch(url, opts).then(function(r) {
|
||||
if (r.status === 401) { window.location.href = '/auth/login?redirect=/posts/' + postId; throw new Error('unauthorized'); }
|
||||
return r.json();
|
||||
});
|
||||
}
|
||||
|
||||
// 更新按钮状态
|
||||
function updateBtn(favorited) {
|
||||
isFavorited = favorited;
|
||||
@ -624,28 +613,7 @@
|
||||
var currentPage = 1;
|
||||
var totalPages = 1;
|
||||
|
||||
// ====== 工具函数 ======
|
||||
function api(method, url, data) {
|
||||
var opts = { method: method, headers: { 'Content-Type': 'application/json' } };
|
||||
var token = getCSRFToken();
|
||||
if (token) opts.headers['X-CSRF-Token'] = token;
|
||||
if (data) opts.body = JSON.stringify(data);
|
||||
return fetch(url, opts).then(function(r) { return r.json(); });
|
||||
}
|
||||
|
||||
function escapeHtml(text) {
|
||||
var d = document.createElement('div');
|
||||
d.textContent = text;
|
||||
return d.innerHTML;
|
||||
}
|
||||
|
||||
function formatTime(iso) {
|
||||
var d = new Date(iso);
|
||||
var pad = function(n) { return n < 10 ? '0'+n : n; };
|
||||
return d.getFullYear() + '-' + pad(d.getMonth()+1) + '-' + pad(d.getDate()) + ' ' + pad(d.getHours()) + ':' + pad(d.getMinutes());
|
||||
}
|
||||
|
||||
// ====== 渲染评论 HTML ======
|
||||
// escapeHtml / formatTime / showToast / api 由 shared/utils.js 统一提供
|
||||
function buildCommentCard(c) {
|
||||
var time = c.created_at ? formatTime(c.created_at) : '';
|
||||
var author = c.author_name ? escapeHtml(c.author_name) : '用户已注销';
|
||||
@ -1057,19 +1025,8 @@
|
||||
});
|
||||
}
|
||||
|
||||
// ====== 评论高亮 + 已删除评论Toast ======
|
||||
function showToast(msg) {
|
||||
var toast = document.createElement('div');
|
||||
toast.className = 'toast';
|
||||
toast.textContent = msg;
|
||||
toast.style.cssText = 'position:fixed;top:20px;left:50%;transform:translateX(-50%);background:rgba(0,0,0,0.85);color:#fff;padding:10px 24px;border-radius:6px;font-size:14px;z-index:9999;animation:fadeIn 0.3s ease';
|
||||
document.body.appendChild(toast);
|
||||
setTimeout(function() {
|
||||
toast.style.opacity = '0';
|
||||
toast.style.transition = 'opacity 0.3s';
|
||||
setTimeout(function() { toast.remove(); }, 300);
|
||||
}, 2500);
|
||||
}
|
||||
// ====== 评论高亮 ======
|
||||
// showToast 由 shared/utils.js 统一提供
|
||||
|
||||
function highlightComment(commentId) {
|
||||
var card = commentsList.querySelector('.comment-card[data-id="' + commentId + '"]') ||
|
||||
|
||||
Reference in New Issue
Block a user