comments.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { Router } from 'express';
  2. import { body } from 'express-validator';
  3. import { CommentService } from '../services/CommentService.js';
  4. import { authenticate } from '../middleware/auth.js';
  5. import { asyncHandler } from '../middleware/error.js';
  6. import { validateRequest } from '../middleware/validate.js';
  7. import { taskQueueService } from '../services/TaskQueueService.js';
  8. const router = Router();
  9. const commentService = new CommentService();
  10. router.use(authenticate);
  11. // 获取评论列表
  12. router.get(
  13. '/',
  14. asyncHandler(async (req, res) => {
  15. const { page = 1, pageSize = 20, accountId, workId, platform, isRead, keyword } = req.query;
  16. const result = await commentService.getComments(req.user!.userId, {
  17. page: Number(page),
  18. pageSize: Number(pageSize),
  19. accountId: accountId ? Number(accountId) : undefined,
  20. workId: workId ? Number(workId) : undefined,
  21. platform: platform as string,
  22. isRead: isRead !== undefined ? isRead === 'true' : undefined,
  23. keyword: keyword as string,
  24. });
  25. res.json({ success: true, data: result });
  26. })
  27. );
  28. // 获取评论统计
  29. router.get(
  30. '/stats',
  31. asyncHandler(async (req, res) => {
  32. const stats = await commentService.getStats(req.user!.userId);
  33. res.json({ success: true, data: stats });
  34. })
  35. );
  36. // 标记为已读
  37. router.post(
  38. '/read',
  39. [
  40. body('commentIds').isArray({ min: 1 }).withMessage('评论ID列表不能为空'),
  41. validateRequest,
  42. ],
  43. asyncHandler(async (req, res) => {
  44. await commentService.markAsRead(req.user!.userId, req.body.commentIds);
  45. res.json({ success: true, message: '已标记为已读' });
  46. })
  47. );
  48. // 回复评论
  49. router.post(
  50. '/reply',
  51. [
  52. body('commentId').isInt().withMessage('评论ID无效'),
  53. body('content').notEmpty().withMessage('回复内容不能为空'),
  54. validateRequest,
  55. ],
  56. asyncHandler(async (req, res) => {
  57. const comment = await commentService.replyComment(
  58. req.user!.userId,
  59. req.body.commentId,
  60. req.body.content
  61. );
  62. res.json({ success: true, data: comment });
  63. })
  64. );
  65. // 批量回复
  66. router.post(
  67. '/batch-reply',
  68. [
  69. body('commentIds').isArray({ min: 1 }).withMessage('评论ID列表不能为空'),
  70. body('content').notEmpty().withMessage('回复内容不能为空'),
  71. validateRequest,
  72. ],
  73. asyncHandler(async (req, res) => {
  74. const result = await commentService.batchReply(
  75. req.user!.userId,
  76. req.body.commentIds,
  77. req.body.content
  78. );
  79. res.json({ success: true, data: result });
  80. })
  81. );
  82. // 同步评论(从平台获取最新评论)- 使用任务队列
  83. router.post(
  84. '/sync',
  85. asyncHandler(async (req, res) => {
  86. const { accountId, accountName } = req.body;
  87. const userId = req.user!.userId;
  88. // 创建任务加入队列
  89. const task = await taskQueueService.createTask(userId, {
  90. type: 'sync_comments',
  91. title: accountName ? `同步评论 - ${accountName}` : '同步所有评论',
  92. accountId: accountId ? Number(accountId) : undefined,
  93. });
  94. res.json({
  95. success: true,
  96. message: '评论同步任务已创建',
  97. data: { taskId: task.id, status: 'pending' },
  98. });
  99. })
  100. );
  101. export default router;