feat(评论系统): 添加评论配置和头像显示功能
- 新增评论配置管理功能,包括博主邮箱、标签和头像前缀设置 - 在评论列表显示用户头像 - 实现服务端配置存储和获取逻辑 - 优化前端设置页面,拆分不同配置项 - 修改开发预览页面配置保存逻辑
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
@@ -117,6 +118,7 @@
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>CWD - 开发预览</h1>
|
||||
@@ -143,7 +145,7 @@
|
||||
<input type="text" id="avatarPrefix" value="https://gravatar.com/avatar" />
|
||||
</div>
|
||||
<div class="config-actions">
|
||||
<button class="btn btn-primary" onclick="initWidget()">应用配置</button>
|
||||
<button class="btn btn-primary" onclick="initWidget()">保存</button>
|
||||
<button class="btn btn-secondary" onclick="toggleTheme()">切换主题</button>
|
||||
</div>
|
||||
</div>
|
||||
@@ -166,4 +168,5 @@
|
||||
|
||||
<script type="module" src="/src/dev.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
</html>
|
||||
@@ -9,10 +9,10 @@ const STORAGE_KEY = 'cwd-dev-config';
|
||||
|
||||
// 默认配置
|
||||
const DEFAULT_CONFIG = {
|
||||
el: '#comments',
|
||||
apiBaseUrl: 'http://localhost:8788',
|
||||
postSlug: "https://zishu.me/message",
|
||||
postSlug: window.location.pathname,
|
||||
theme: 'light',
|
||||
avatarPrefix: 'https://gravatar.com/avatar',
|
||||
};
|
||||
|
||||
let widgetInstance = null;
|
||||
@@ -69,10 +69,28 @@ function getConfigFromInputs() {
|
||||
return { apiBaseUrl, postSlug, theme, avatarPrefix };
|
||||
}
|
||||
|
||||
async function loadServerCommentConfig(apiBaseUrl) {
|
||||
try {
|
||||
const res = await fetch(`${apiBaseUrl}/api/config/comments`);
|
||||
if (!res.ok) {
|
||||
return {};
|
||||
}
|
||||
const data = await res.json();
|
||||
return {
|
||||
adminEmail: data.adminEmail || '',
|
||||
adminBadge: data.adminBadge || '',
|
||||
avatarPrefix: data.avatarPrefix || '',
|
||||
};
|
||||
} catch (e) {
|
||||
console.warn('[CWDComments] 加载服务端评论配置失败:', e);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化 widget
|
||||
*/
|
||||
function initWidget() {
|
||||
async function initWidget() {
|
||||
const config = getConfigFromInputs();
|
||||
|
||||
// 保存到本地存储
|
||||
@@ -92,15 +110,17 @@ function initWidget() {
|
||||
|
||||
// 创建新实例
|
||||
try {
|
||||
const serverConfig = await loadServerCommentConfig(config.apiBaseUrl);
|
||||
|
||||
widgetInstance = new CWDComments({
|
||||
el: '#comments',
|
||||
apiBaseUrl: config.apiBaseUrl,
|
||||
postSlug: config.postSlug,
|
||||
theme: config.theme,
|
||||
avatarPrefix: config.avatarPrefix,
|
||||
avatarPrefix: serverConfig.avatarPrefix || config.avatarPrefix,
|
||||
pageSize: 20,
|
||||
adminEmail: 'anghunk@gmail.com', // 博主邮箱,留空不进行匹配
|
||||
adminBadge: '博主', // 自定义标识,默认为"博主"
|
||||
...(serverConfig.adminEmail ? { adminEmail: serverConfig.adminEmail } : {}),
|
||||
...(serverConfig.adminBadge ? { adminBadge: serverConfig.adminBadge } : {}),
|
||||
});
|
||||
widgetInstance.mount();
|
||||
console.log('[CWDComments] Widget 初始化成功', config);
|
||||
|
||||
Reference in New Issue
Block a user