works.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { Router } from 'express';
  2. import { workService } from '../services/WorkService.js';
  3. import { asyncHandler } from '../middleware/error.js';
  4. import { authenticate } from '../middleware/auth.js';
  5. import { taskQueueService } from '../services/TaskQueueService.js';
  6. const router = Router();
  7. // 所有路由需要认证
  8. router.use(authenticate);
  9. /**
  10. * 获取作品列表
  11. */
  12. router.get(
  13. '/',
  14. asyncHandler(async (req, res) => {
  15. const userId = req.user!.userId;
  16. const { page, pageSize, accountId, platform, status, keyword } = req.query;
  17. const result = await workService.getWorks(userId, {
  18. page: page ? parseInt(page as string) : 1,
  19. pageSize: pageSize ? parseInt(pageSize as string) : 12,
  20. accountId: accountId ? parseInt(accountId as string) : undefined,
  21. platform: platform as string | undefined,
  22. status: status as string | undefined,
  23. keyword: keyword as string | undefined,
  24. });
  25. res.json({ success: true, data: result });
  26. })
  27. );
  28. /**
  29. * 获取作品统计
  30. */
  31. router.get(
  32. '/stats',
  33. asyncHandler(async (req, res) => {
  34. const userId = req.user!.userId;
  35. const stats = await workService.getStats(userId);
  36. res.json({ success: true, data: stats });
  37. })
  38. );
  39. /**
  40. * 同步作品 - 使用任务队列
  41. */
  42. router.post(
  43. '/sync',
  44. asyncHandler(async (req, res) => {
  45. const userId = req.user!.userId;
  46. const { accountId, accountName, platform, platformName } = req.body;
  47. // 创建同步作品任务:支持按账号、按平台或同步全部
  48. let title = '同步所有作品';
  49. if (accountName) title = `同步作品 - ${accountName}`;
  50. else if (platformName) title = `同步作品 - ${platformName}`;
  51. const task = taskQueueService.createTask(userId, {
  52. type: 'sync_works',
  53. title,
  54. accountId: accountId ? Number(accountId) : undefined,
  55. platform: platform ? String(platform) : undefined,
  56. });
  57. res.json({
  58. success: true,
  59. message: '作品同步任务已创建',
  60. data: {
  61. taskId: task.id,
  62. status: 'pending'
  63. }
  64. });
  65. })
  66. );
  67. /**
  68. * 获取单个作品
  69. */
  70. router.get(
  71. '/:id',
  72. asyncHandler(async (req, res) => {
  73. const userId = req.user!.userId;
  74. const workId = parseInt(req.params.id);
  75. const work = await workService.getWorkById(userId, workId);
  76. res.json({ success: true, data: work });
  77. })
  78. );
  79. /**
  80. * 删除平台上的作品
  81. */
  82. router.post(
  83. '/:id/delete-platform',
  84. asyncHandler(async (req, res) => {
  85. const userId = req.user!.userId;
  86. const workId = parseInt(req.params.id);
  87. // 创建删除任务
  88. const task = taskQueueService.createTask(userId, {
  89. type: 'delete_work',
  90. title: '删除平台作品',
  91. data: { workId },
  92. });
  93. res.json({
  94. success: true,
  95. message: '删除任务已创建',
  96. data: { taskId: task.id }
  97. });
  98. })
  99. );
  100. /**
  101. * 删除本地作品记录
  102. */
  103. router.delete(
  104. '/:id',
  105. asyncHandler(async (req, res) => {
  106. const userId = req.user!.userId;
  107. const workId = parseInt(req.params.id);
  108. await workService.deleteWork(userId, workId);
  109. res.json({ success: true, message: '作品已删除' });
  110. })
  111. );
  112. export default router;