File size: 1,869 Bytes
db242f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { FastifyRequest } from 'fastify';

import {
  Body,
  Controller,
  Get,
  HttpCode,
  Param,
  Post,
  Query,
  RawBodyRequest,
  Req,
} from '@nestjs/common';
import { Role } from '@prisma/client';

import { Payload, Roles } from '@/common/guards/auth.guard';
import { Public } from '@/common/guards/auth.guard';
import { JWTPayload } from '@/libs/jwt/jwt.service';
import { PaymentService } from '@/libs/payment/payment.service';

import { newOrderDto } from 'shared';

import { OrderService } from './order.service';

@Controller('order')
export class OrderController {
  constructor(
    private readonly orderService: OrderService,
    private paymentService: PaymentService,
  ) {}

  /* 新建订单 */
  @Post('new')
  async newOrder(@Payload('id') userId: number, @Body() data: newOrderDto) {
    const order = await this.orderService.createOrder(userId, data.productId);
    // TODO 防止短时间内重复产生订单
    const result = await this.paymentService.xhStartPay({
      orderId: order.id,
      price: order.amount,
      attach: '',
      title: 'AI产品',
    });
    return {
      success: true,
      data: {
        url: result.url,
        qrcode: result.url_qrcode,
      },
    };
  }

  /* 获取自己的订单 */
  @Get('my')
  async listOrder(@Payload('id') userId: number) {
    return this.orderService.listOrder(userId);
  }

  /* 获取所有订单 */
  @Roles(Role.Admin)
  @Get('all')
  async listAllOrder(@Query('userId') userId?: number) {
    return this.orderService.listOrder(userId);
  }

  /* 支付回调:虎皮椒 */
  @Public()
  @Post('callback/xunhu')
  @HttpCode(200)
  async finishOrder(@Req() req: RawBodyRequest<FastifyRequest>) {
    const raw = req.body;
    const orderId = await this.paymentService.xhCallback(raw);
    await this.orderService.finishOrder(orderId);
    return 'success';
  }
}