# All files / modules/share share.controller.ts

0% Statements 0/78
0% Branches 0/1
0% Functions 0/1
0% Lines 0/78

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106

     import Router from '@koa/router';
    import { shareService } from './share.service';
    import { authMiddleware } from '../../middleware/auth';

    const router = new Router();

    /**
     * 获取分享信息（兼容feature_list测试接口）
     * GET /api/share?audioId=xxx
     */
    router.get('/', async (ctx) => {
      try {
        const { audioId } = ctx.query as { audioId: string };
        if (!audioId) {
          ctx.body = {
            code: 400,
            message: 'audioId is required',
          };
          return;
        }
        const card = await shareService.generateShareCard(audioId);

        ctx.body = {
          code: 0,
          message: 'success',
          data: card,
        };
      } catch (error: any) {
        ctx.body = {
          code: 400,
          message: error.message || '获取分享信息失败',
        };
      }
    });

    /**
     * 获取分享卡片信息
     * GET /api/share/card/:audioId
     */
    router.get('/card/:audioId', async (ctx) => {
      try {
        const { audioId } = ctx.params;
        const card = await shareService.generateShareCard(audioId);

        ctx.body = {
          code: 0,
          message: 'success',
          data: card,
        };
      } catch (error: any) {
        ctx.body = {
          code: 400,
          message: error.message || '获取分享信息失败',
        };
      }
    });

    /**
     * 生成二维码数据
     * GET /api/share/qrcode/:audioId
     */
    router.get('/qrcode/:audioId', async (ctx) => {
      try {
        const { audioId } = ctx.params;
        const qrData = shareService.generateQRCodeData(audioId);

        ctx.body = {
          code: 0,
          message: 'success',
          data: {
            text: qrData,
          },
        };
      } catch (error: any) {
        ctx.body = {
          code: 400,
          message: error.message || '生成二维码失败',
        };
      }
    });

    /**
     * 记录分享行为
     * POST /api/share/track
     */
    router.post('/track', authMiddleware, async (ctx) => {
      try {
        const { audioId, platform } = ctx.request.body as { audioId: string; platform: string };
        const userId = (ctx.state as any).user.id;

        await shareService.trackShare(audioId, userId, platform);

        ctx.body = {
          code: 0,
          message: 'success',
        };
      } catch (error: any) {
        ctx.body = {
          code: 400,
          message: error.message || '记录分享失败',
        };
      }
    });

    export default router;