import { Router } from 'express'; import { body } from 'express-validator'; import { CommentService } from '../services/CommentService.js'; import { authenticate } from '../middleware/auth.js'; import { asyncHandler } from '../middleware/error.js'; import { validateRequest } from '../middleware/validate.js'; import { taskQueueService } from '../services/TaskQueueService.js'; const router = Router(); const commentService = new CommentService(); router.use(authenticate); // 获取评论列表 router.get( '/', asyncHandler(async (req, res) => { const { page = 1, pageSize = 20, accountId, workId, platform, isRead, keyword } = req.query; const result = await commentService.getComments(req.user!.userId, { page: Number(page), pageSize: Number(pageSize), accountId: accountId ? Number(accountId) : undefined, workId: workId ? Number(workId) : undefined, platform: platform as string, isRead: isRead !== undefined ? isRead === 'true' : undefined, keyword: keyword as string, }); res.json({ success: true, data: result }); }) ); // 获取评论统计 router.get( '/stats', asyncHandler(async (req, res) => { const stats = await commentService.getStats(req.user!.userId); res.json({ success: true, data: stats }); }) ); // 标记为已读 router.post( '/read', [ body('commentIds').isArray({ min: 1 }).withMessage('评论ID列表不能为空'), validateRequest, ], asyncHandler(async (req, res) => { await commentService.markAsRead(req.user!.userId, req.body.commentIds); res.json({ success: true, message: '已标记为已读' }); }) ); // 回复评论 router.post( '/reply', [ body('commentId').isInt().withMessage('评论ID无效'), body('content').notEmpty().withMessage('回复内容不能为空'), validateRequest, ], asyncHandler(async (req, res) => { const comment = await commentService.replyComment( req.user!.userId, req.body.commentId, req.body.content ); res.json({ success: true, data: comment }); }) ); // 批量回复 router.post( '/batch-reply', [ body('commentIds').isArray({ min: 1 }).withMessage('评论ID列表不能为空'), body('content').notEmpty().withMessage('回复内容不能为空'), validateRequest, ], asyncHandler(async (req, res) => { const result = await commentService.batchReply( req.user!.userId, req.body.commentIds, req.body.content ); res.json({ success: true, data: result }); }) ); // 同步评论(从平台获取最新评论)- 使用任务队列 router.post( '/sync', asyncHandler(async (req, res) => { const { accountId, accountName } = req.body; const userId = req.user!.userId; // 创建任务加入队列 const task = await taskQueueService.createTask(userId, { type: 'sync_comments', title: accountName ? `同步评论 - ${accountName}` : '同步所有评论', accountId: accountId ? Number(accountId) : undefined, }); res.json({ success: true, message: '评论同步任务已创建', data: { taskId: task.id, status: 'pending' }, }); }) ); export default router;