feat: 新增CWD评论系统前后端代码及文档
refactor: 移除旧版评论系统代码并重构为Vue3前端 docs: 更新后端配置文档说明 fix: 修复评论提交频率限制和邮件通知逻辑 style: 格式化代码并优化样式 test: 添加Vitest测试配置 build: 更新依赖项和构建配置 chore: 清理无用文件和缓存
This commit is contained in:
23
cwd-comments-api/src/utils/auth.ts
Normal file
23
cwd-comments-api/src/utils/auth.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { Context, Next } from 'hono';
|
||||
import { Bindings } from '../bindings';
|
||||
|
||||
export const adminAuth = async (c: Context<{ Bindings: Bindings }>, next: Next) => {
|
||||
const token = c.req.header('Authorization')?.replace('Bearer ', '');
|
||||
if (!token) return c.json({ message: "Unauthorized" }, 401);
|
||||
|
||||
const sessionData = await c.env.CWD_AUTH_KV.get(`token:${token}`);
|
||||
if (!sessionData) {
|
||||
return c.json({ message: "Token expired or invalid" }, 401);
|
||||
}
|
||||
|
||||
const session = JSON.parse(sessionData);
|
||||
const currentIp = c.req.header('cf-connecting-ip');
|
||||
|
||||
// 安全检查:如果 IP 发生变化(比如 Token 被盗),要求重新登录
|
||||
// if (session.ip !== currentIp) {
|
||||
// await c.env.CWD_AUTH_KV.delete(`token:${token}`);
|
||||
// return c.json({ message: "Security alert: IP changed" }, 401);
|
||||
// }
|
||||
|
||||
await next();
|
||||
};
|
||||
23
cwd-comments-api/src/utils/cors.ts
Normal file
23
cwd-comments-api/src/utils/cors.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { cors } from 'hono/cors'
|
||||
|
||||
export const customCors = (allowOriginStr: string | undefined) => {
|
||||
// 1. 将环境变量字符串解析为数组
|
||||
// 如果环境变量不存在,则默认为空数组
|
||||
const allowedOrigins = allowOriginStr
|
||||
? allowOriginStr.split(',').map(origin => origin.trim())
|
||||
: []
|
||||
|
||||
return cors({
|
||||
origin: (origin) => {
|
||||
// 如果请求的 origin 在白名单中,或者是本地文件(null)
|
||||
if (!origin || allowedOrigins.includes(origin)) {
|
||||
return origin
|
||||
}
|
||||
},
|
||||
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
|
||||
allowHeaders: ['Content-Type', 'Authorization'],
|
||||
exposeHeaders: ['Content-Length'],
|
||||
maxAge: 600,
|
||||
credentials: true,
|
||||
})
|
||||
}
|
||||
98
cwd-comments-api/src/utils/email.ts
Normal file
98
cwd-comments-api/src/utils/email.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import { Bindings } from '../bindings';
|
||||
|
||||
/**
|
||||
* 回复通知邮件
|
||||
*/
|
||||
export async function sendCommentReplyNotification(
|
||||
env: Bindings,
|
||||
params: {
|
||||
toEmail: string;
|
||||
toName: string;
|
||||
postTitle: string;
|
||||
parentComment: string;
|
||||
replyAuthor: string;
|
||||
replyContent: string;
|
||||
postUrl: string;
|
||||
}
|
||||
) {
|
||||
const { toEmail, toName, postTitle, parentComment, replyAuthor, replyContent, postUrl } = params;
|
||||
|
||||
const html = `
|
||||
<div style="font-family: sans-serif; line-height: 1.6; color: #333;">
|
||||
<p>Hi <b>${toName}</b>,</p>
|
||||
<p>${replyAuthor} 回复了你在 <b>${postTitle}</b> 中的评论:</p>
|
||||
<blockquote style="margin: 10px 0; padding: 10px; border-left: 4px solid #e2e8f0; background: #f8fafc;">
|
||||
${parentComment}
|
||||
</blockquote>
|
||||
<p>最新回复:</p>
|
||||
<blockquote style="margin: 10px 0; padding: 10px; border-left: 4px solid #3b82f6; background: #eff6ff;">
|
||||
${replyContent}
|
||||
</blockquote>
|
||||
<p style="margin-top: 20px;">
|
||||
<a href="${postUrl}" style="background: #3b82f6; color: white; padding: 10px 20px; text-decoration: none; border-radius: 6px; display: inline-block;">
|
||||
查看完整回复
|
||||
</a>
|
||||
</p>
|
||||
<hr style="border: none; border-top: 1px solid #eee; margin-top: 30px;">
|
||||
<p style="font-size: 12px; color: #999;">此邮件由系统自动发送,请勿直接回复。</p>
|
||||
</div>
|
||||
`;
|
||||
|
||||
if (!env.SEND_EMAIL || !env.CF_FROM_EMAIL) {
|
||||
throw new Error('未配置邮件发送绑定或发件人地址');
|
||||
}
|
||||
|
||||
await env.SEND_EMAIL.send({
|
||||
to: [{ email: toEmail }],
|
||||
from: { email: env.CF_FROM_EMAIL },
|
||||
subject: `你在 example.com 上的评论有了新回复`,
|
||||
html
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 站长通知邮件
|
||||
*/
|
||||
export async function sendCommentNotification(
|
||||
env: Bindings,
|
||||
params: {
|
||||
postTitle: string;
|
||||
postUrl: string;
|
||||
commentAuthor: string;
|
||||
commentContent: string;
|
||||
}
|
||||
) {
|
||||
const { postTitle, postUrl, commentAuthor, commentContent } = params;
|
||||
const toEmail = await getAdminNotifyEmail(env);
|
||||
|
||||
const html = `
|
||||
<div style="font-family: sans-serif;">
|
||||
<p><b>${commentAuthor}</b> 在文章《${postTitle}》下发表了评论:</p>
|
||||
<div style="padding: 15px; border: 1px solid #ddd; border-radius: 8px;">
|
||||
${commentContent}
|
||||
</div>
|
||||
<p><a href="${postUrl}">点击跳转到文章</a></p>
|
||||
</div>
|
||||
`;
|
||||
|
||||
if (!env.SEND_EMAIL || !env.CF_FROM_EMAIL) {
|
||||
throw new Error('未配置邮件发送绑定或发件人地址');
|
||||
}
|
||||
|
||||
await env.SEND_EMAIL.send({
|
||||
to: [{ email: toEmail }],
|
||||
from: { email: env.CF_FROM_EMAIL },
|
||||
subject: `新评论通知:${postTitle}`,
|
||||
html
|
||||
});
|
||||
}
|
||||
|
||||
// 读取管理员通知邮箱:优先 KV 设置,其次环境变量
|
||||
async function getAdminNotifyEmail(env: Bindings): Promise<string> {
|
||||
if (env.CWD_CONFIG_KV) {
|
||||
const val = await env.CWD_CONFIG_KV.get('settings:admin_notify_email');
|
||||
if (val) return val;
|
||||
}
|
||||
if (env.EMAIL_ADDRESS) return env.EMAIL_ADDRESS;
|
||||
throw new Error('未配置管理员通知邮箱');
|
||||
}
|
||||
19
cwd-comments-api/src/utils/getAvatar.ts
Normal file
19
cwd-comments-api/src/utils/getAvatar.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* 默认 gravatar.com 前缀
|
||||
*/
|
||||
const DEFAULT_AVATAR_PREFIX = 'https://gravatar.com/avatar';
|
||||
|
||||
/**
|
||||
* 辅助函数:生成 gravatar.com 头像地址 (MD5 算法)
|
||||
* @param email - 邮箱地址
|
||||
* @param prefix - 头像服务前缀,默认为 https://gravatar.com/avatar
|
||||
*/
|
||||
export const getCravatar = async (email: string, prefix?: string): Promise<string> => {
|
||||
const cleanEmail = email.trim().toLowerCase();
|
||||
const msgUint8 = new TextEncoder().encode(cleanEmail);
|
||||
const hashBuffer = await crypto.subtle.digest('MD5', msgUint8);
|
||||
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
||||
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
|
||||
const avatarPrefix = prefix || DEFAULT_AVATAR_PREFIX;
|
||||
return `${avatarPrefix}/${hashHex}?s=200&d=retro`;
|
||||
};
|
||||
Reference in New Issue
Block a user