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:
@ -11,30 +11,14 @@
|
||||
var pagination = document.getElementById('commentPagination');
|
||||
var loading = document.getElementById('commentLoading');
|
||||
|
||||
function api(url, opts) {
|
||||
opts = opts || {};
|
||||
return fetch(url, opts).then(function(r) { return r.json(); });
|
||||
}
|
||||
|
||||
function escapeHtml(text) {
|
||||
if (!text) return '';
|
||||
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());
|
||||
}
|
||||
// escapeHtml / formatTime / api 由 shared/utils.js 统一提供
|
||||
|
||||
function loadComments(page) {
|
||||
loading.style.display = 'block';
|
||||
var url = '/api/admin/comments?page=' + page + '&page_size=20';
|
||||
if (currentKeyword) url += '&keyword=' + encodeURIComponent(currentKeyword);
|
||||
|
||||
api(url).then(function(d) {
|
||||
api('GET', url).then(function(d) {
|
||||
loading.style.display = 'none';
|
||||
if (!d.success) return;
|
||||
totalPages = d.data.total_pages || 1;
|
||||
|
||||
Reference in New Issue
Block a user