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

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

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

     import Router from '@koa/router';
    import { Context } from 'koa';
    import * as MemberService from './member.service';
    import { BadRequestError, NotFoundError } from '../../middleware/errorHandler';
    import { authMiddleware } from '../../middleware/auth';

    const router = new Router();

    // 获取会员权益信息
    router.get('/benefits', async (ctx: Context) => {
      const benefits = MemberService.getMemberBenefits();

      ctx.body = {
        code: 0,
        message: 'success',
        data: benefits,
      };
    });

    // 获取用户会员状态
    router.get('/status', authMiddleware, async (ctx: Context) => {
      const userId = ctx.state.user.userId;
      const status = await MemberService.getMemberStatus(userId);

      ctx.body = {
        code: 0,
        message: 'success',
        data: status,
      };
    });

    // 创建订单
    router.post('/order', authMiddleware, async (ctx: Context) => {
      const userId = ctx.state.user.userId;
      const { productType } = ctx.request.body as { productType: 'monthly' | 'yearly' };

      if (!['monthly', 'yearly'].includes(productType)) {
        throw new BadRequestError('无效的产品类型');
      }

      const result = await MemberService.createOrder(userId, productType);

      ctx.body = {
        code: 0,
        message: '订单创建成功',
        data: result,
      };
    });

    // 模拟支付（仅开发环境）
    router.post('/pay/mock', authMiddleware, async (ctx: Context) => {
      if (process.env.NODE_ENV === 'production') {
        throw new BadRequestError('生产环境不可用');
      }

      const userId = ctx.state.user.userId;
      const { orderNo } = ctx.request.body as { orderNo: string };

      if (!orderNo) {
        throw new BadRequestError('订单号不能为空');
      }

      const result = await MemberService.mockPaymentSuccess(orderNo, userId);

      ctx.body = {
        code: 0,
        message: '支付成功',
        data: result,
      };
    });

    // 获取订单列表
    router.get('/orders', authMiddleware, async (ctx: Context) => {
      const userId = ctx.state.user.userId;
      const { page = 1, pageSize = 10 } = ctx.query as { page?: string; pageSize?: string };

      const result = await MemberService.getOrders(
        userId,
        Number(page) || 1,
        Number(pageSize) || 10
      );

      ctx.body = {
        code: 0,
        message: 'success',
        data: result,
      };
    });

    export default router;