| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- import { Router } from 'express';
- import { workService } from '../services/WorkService.js';
- import { asyncHandler } from '../middleware/error.js';
- import { authenticate } from '../middleware/auth.js';
- import { taskQueueService } from '../services/TaskQueueService.js';
- const router = Router();
- // 所有路由需要认证
- router.use(authenticate);
- /**
- * 获取作品列表
- */
- router.get(
- '/',
- asyncHandler(async (req, res) => {
- const userId = req.user!.userId;
- const { page, pageSize, accountId, platform, status, keyword } = req.query;
- const result = await workService.getWorks(userId, {
- page: page ? parseInt(page as string) : 1,
- pageSize: pageSize ? parseInt(pageSize as string) : 12,
- accountId: accountId ? parseInt(accountId as string) : undefined,
- platform: platform as string | undefined,
- status: status as string | undefined,
- keyword: keyword as string | undefined,
- });
- res.json({ success: true, data: result });
- })
- );
- /**
- * 获取作品统计
- */
- router.get(
- '/stats',
- asyncHandler(async (req, res) => {
- const userId = req.user!.userId;
- const stats = await workService.getStats(userId);
- res.json({ success: true, data: stats });
- })
- );
- /**
- * 同步作品 - 使用任务队列
- */
- router.post(
- '/sync',
- asyncHandler(async (req, res) => {
- const userId = req.user!.userId;
- const { accountId, accountName, platform, platformName } = req.body;
- // 创建同步作品任务:支持按账号、按平台或同步全部
- let title = '同步所有作品';
- if (accountName) title = `同步作品 - ${accountName}`;
- else if (platformName) title = `同步作品 - ${platformName}`;
- const task = taskQueueService.createTask(userId, {
- type: 'sync_works',
- title,
- accountId: accountId ? Number(accountId) : undefined,
- platform: platform ? String(platform) : undefined,
- });
- res.json({
- success: true,
- message: '作品同步任务已创建',
- data: {
- taskId: task.id,
- status: 'pending'
- }
- });
- })
- );
- /**
- * 获取单个作品
- */
- router.get(
- '/:id',
- asyncHandler(async (req, res) => {
- const userId = req.user!.userId;
- const workId = parseInt(req.params.id);
- const work = await workService.getWorkById(userId, workId);
- res.json({ success: true, data: work });
- })
- );
- /**
- * 删除平台上的作品
- */
- router.post(
- '/:id/delete-platform',
- asyncHandler(async (req, res) => {
- const userId = req.user!.userId;
- const workId = parseInt(req.params.id);
-
- // 创建删除任务
- const task = taskQueueService.createTask(userId, {
- type: 'delete_work',
- title: '删除平台作品',
- data: { workId },
- });
-
- res.json({
- success: true,
- message: '删除任务已创建',
- data: { taskId: task.id }
- });
- })
- );
- /**
- * 删除本地作品记录
- */
- router.delete(
- '/:id',
- asyncHandler(async (req, res) => {
- const userId = req.user!.userId;
- const workId = parseInt(req.params.id);
- await workService.deleteWork(userId, workId);
- res.json({ success: true, message: '作品已删除' });
- })
- );
- export default router;
|