feat: 付费体系重构 P0 - 配额独立化/简历付费下载/PDF生成

This commit is contained in:
yuzhiran
2026-06-12 09:31:11 +08:00
parent 5d407b4f79
commit 065fe7a186
23 changed files with 965 additions and 106 deletions
+138 -43
View File
@@ -1,4 +1,4 @@
import { Controller, Post, Get, Param, Query, Body, UseGuards, HttpException, HttpStatus, Logger, Req } from '@nestjs/common'
import { Controller, Post, Get, Param, Body, UseGuards, HttpException, HttpStatus, Logger, Req } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'
import { Model } from 'mongoose'
import { User, UserDocument } from '../user/user.schema'
@@ -6,12 +6,25 @@ import { PaymentOrder, PaymentOrderDocument } from './payment-order.schema'
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard'
import { CurrentUser } from '../../common/decorators/current-user.decorator'
import { WechatPayService } from './wechat-pay.service'
import { QuotaService } from '../user/quota.service'
import { Public } from '../../common/decorators/public.decorator'
const GROWTH_AMOUNT = 1990 // 19.9 元(分)
const SPRINT_AMOUNT = 4990 // 49.9 元(分)
const GROWTH_AMOUNT = 1990
const SPRINT_AMOUNT = 4990
const VIP_DURATION_DAYS = 30
const PRODUCT_PRICES: Record<string, number> = {
interview: 500,
optimize: 300,
download: 200,
}
const PRODUCT_CREDITS: Record<string, number> = {
interview: 1,
optimize: 1,
download: 1,
}
@Controller('payment')
export class PaymentController {
private readonly logger = new Logger(PaymentController.name)
@@ -20,9 +33,10 @@ export class PaymentController {
@InjectModel(User.name) private userModel: Model<UserDocument>,
@InjectModel(PaymentOrder.name) private orderModel: Model<PaymentOrderDocument>,
private wechatPay: WechatPayService,
private quotaService: QuotaService,
) {}
/** 创建订单(H5Native 扫码支付) */
/** 创建套餐订单(H5Native 扫码支付) */
@UseGuards(JwtAuthGuard)
@Post('create')
async create(@CurrentUser('userId') userId: string, @Body('plan') plan: string = 'growth') {
@@ -36,11 +50,42 @@ export class PaymentController {
const outTradeNo = `${plan === 'sprint' ? 'SPR' : 'VIP'}${Date.now()}${userId.slice(-6)}`
const result = await this.wechatPay.nativePay(title, outTradeNo, amount)
await this.orderModel.create({ outTradeNo, userId, userPhone: user.phone || '', amount, title, status: 'pending', channel: 'native', plan })
await this.orderModel.create({ outTradeNo, userId, userPhone: user.phone || '', amount, title, status: 'pending', channel: 'native', type: 'membership', plan })
return { outTradeNo, codeUrl: result.codeUrl, amount, title }
}
/** 创建按次购买订单 */
@UseGuards(JwtAuthGuard)
@Post('create-product')
async createProduct(
@CurrentUser('userId') userId: string,
@Body('type') type: string,
@Body('metadata') metadata?: Record<string, any>,
) {
if (!['interview', 'optimize', 'download'].includes(type)) {
throw new HttpException('无效产品类型', HttpStatus.BAD_REQUEST)
}
const user = await this.userModel.findById(userId).exec()
if (!user) throw new HttpException('用户不存在', HttpStatus.NOT_FOUND)
const price = PRODUCT_PRICES[type]
if (!price) throw new HttpException('价格未配置', HttpStatus.INTERNAL_SERVER_ERROR)
const titles: Record<string, string> = {
interview: 'AI 模拟面试单次',
optimize: '简历优化单次',
download: '简历下载',
}
const title = titles[type] || type
const outTradeNo = `${type.slice(0, 3).toUpperCase()}${Date.now()}${userId.slice(-6)}`
const result = await this.wechatPay.nativePay(title, outTradeNo, price)
await this.orderModel.create({ outTradeNo, userId, userPhone: user.phone || '', amount: price, title, status: 'pending', channel: 'native', type, plan: 'growth', metadata })
return { outTradeNo, codeUrl: result.codeUrl, amount: price, title }
}
/** JSAPI 支付(微信小程序) */
@UseGuards(JwtAuthGuard)
@Post('jsapi')
@@ -57,7 +102,40 @@ export class PaymentController {
const outTradeNo = `${plan === 'sprint' ? 'SPR' : 'VIP'}${Date.now()}${userId.slice(-6)}`
const result = await this.wechatPay.jsapiPay(title, outTradeNo, amount, openid)
await this.orderModel.create({ outTradeNo, userId, userPhone: user.phone || '', amount, title, status: 'pending', channel: 'jsapi', plan })
await this.orderModel.create({ outTradeNo, userId, userPhone: user.phone || '', amount, title, status: 'pending', channel: 'jsapi', type: 'membership', plan })
return { ...result, outTradeNo }
}
/** JSAPI 按次购买 */
@UseGuards(JwtAuthGuard)
@Post('jsapi-product')
async jsapiProduct(
@CurrentUser('userId') userId: string,
@Body('type') type: string,
@Body('metadata') metadata?: Record<string, any>,
) {
if (!['interview', 'optimize', 'download'].includes(type)) {
throw new HttpException('无效产品类型', HttpStatus.BAD_REQUEST)
}
const user = await this.userModel.findById(userId).exec()
if (!user) throw new HttpException('用户不存在', HttpStatus.NOT_FOUND)
const openid = user.wxOpenid
if (!openid) throw new HttpException('未绑定微信', HttpStatus.BAD_REQUEST)
const price = PRODUCT_PRICES[type]
if (!price) throw new HttpException('价格未配置', HttpStatus.INTERNAL_SERVER_ERROR)
const titles: Record<string, string> = {
interview: 'AI 模拟面试单次',
optimize: '简历优化单次',
download: '简历下载',
}
const title = titles[type] || type
const outTradeNo = `${type.slice(0, 3).toUpperCase()}${Date.now()}${userId.slice(-6)}`
const result = await this.wechatPay.jsapiPay(title, outTradeNo, price, openid)
await this.orderModel.create({ outTradeNo, userId, userPhone: user.phone || '', amount: price, title, status: 'pending', channel: 'jsapi', type, plan: 'growth', metadata })
return { ...result, outTradeNo }
}
@@ -65,10 +143,7 @@ export class PaymentController {
/** 支付回调通知 */
@Public()
@Post('notify')
async notify(
@Body() body: any,
@Req() req: any,
) {
async notify(@Body() body: any, @Req() req: any) {
try {
const wechatSignature = req.headers['wechatpay-signature'] || ''
const wechatTimestamp = req.headers['wechatpay-timestamp'] || ''
@@ -79,7 +154,6 @@ export class PaymentController {
const outTradeNo = decrypted.out_trade_no
const wxTransactionId = decrypted.transaction_id
// 从数据库订单查找 userId,而非从 outTradeNo 解析
const order = await this.orderModel.findOne({ outTradeNo }).exec()
if (!order) {
this.logger.warn(`支付回调:订单不存在 ${outTradeNo}`)
@@ -94,21 +168,10 @@ export class PaymentController {
await order.save()
}
// 根据订单 plan 激活对应套餐
const user = await this.userModel.findById(order.userId).exec()
if (user && user.plan === 'free') {
const expireAt = new Date()
expireAt.setDate(expireAt.getDate() + VIP_DURATION_DAYS)
if (order.plan === 'sprint') {
user.plan = 'sprint'
user.sprintExpireAt = expireAt
user.sprintRemaining = 10 // 每月 10 次冲刺权益(语音分析+缺口分析)
} else {
user.plan = 'growth'
user.vipExpireAt = expireAt
}
user.remaining = 999
await user.save()
if (order.type === 'membership') {
await this.activateMembership(order)
} else {
await this.activateProduct(order)
}
return { code: 'SUCCESS', message: '成功' }
} catch (e) {
@@ -117,6 +180,47 @@ export class PaymentController {
}
}
private async activateMembership(order: PaymentOrderDocument) {
const user = await this.userModel.findById(order.userId).exec()
if (!user || user.plan !== 'free') return
const expireAt = new Date()
expireAt.setDate(expireAt.getDate() + VIP_DURATION_DAYS)
const isSprint = order.plan === 'sprint'
if (isSprint) {
user.plan = 'sprint'
user.sprintExpireAt = expireAt
user.sprintRemaining = 10
} else {
user.plan = 'growth'
user.vipExpireAt = expireAt
}
const credits = isSprint
? { interview: 999, resumeOptimize: 50, resumeDownload: 30 }
: { interview: 999, resumeOptimize: 20, resumeDownload: 10 }
user.remaining = 999
user.interviewCredits = credits.interview
user.resumeOptimizeCredits = credits.resumeOptimize
user.resumeDownloadCredits = credits.resumeDownload
user.freeOptimizeUsed = 3
await user.save()
}
private async activateProduct(order: PaymentOrderDocument) {
const credits = PRODUCT_CREDITS[order.type]
if (!credits) return
const typeMap: Record<string, 'interview' | 'optimize' | 'download'> = {
interview: 'interview',
optimize: 'optimize',
download: 'download',
}
const mapped = typeMap[order.type]
if (mapped) {
await this.quotaService.grantCredits(order.userId, mapped, credits)
}
}
/** 查询订单(微信侧) */
@UseGuards(JwtAuthGuard)
@Post('query')
@@ -132,32 +236,23 @@ export class PaymentController {
async checkOrder(@Param('outTradeNo') outTradeNo: string, @CurrentUser('userId') userId: string) {
const order = await this.orderModel.findOne({ outTradeNo, userId }).exec()
if (!order) throw new HttpException('订单不存在', HttpStatus.NOT_FOUND)
return { status: order.status, plan: order.plan }
return { status: order.status, plan: order.plan, type: order.type }
}
/** 凭订单号激活套餐(前端支付成功后调用,兜底) */
/** 凭订单号激活(前端支付成功后调用,兜底) */
@UseGuards(JwtAuthGuard)
@Post('activate')
async activate(@CurrentUser('userId') userId: string, @Body('outTradeNo') outTradeNo: string) {
const order = await this.orderModel.findOne({ outTradeNo, userId }).exec()
if (!order) throw new HttpException('订单不存在', HttpStatus.NOT_FOUND)
if (order.status !== 'success') throw new HttpException('支付未完成', HttpStatus.BAD_REQUEST)
const user = await this.userModel.findById(userId).exec()
if (!user) throw new HttpException('用户不存在', HttpStatus.NOT_FOUND)
if (user.plan !== 'free') return { success: true, plan: user.plan, message: '已是会员' }
const expireAt = new Date()
expireAt.setDate(expireAt.getDate() + VIP_DURATION_DAYS)
if (order.plan === 'sprint') {
user.plan = 'sprint'
user.sprintExpireAt = expireAt
user.sprintRemaining = 10
} else {
user.plan = 'growth'
user.vipExpireAt = expireAt
if (order.type === 'membership') {
await this.activateMembership(order)
return { success: true, plan: order.plan }
}
user.remaining = 999
await user.save()
return { success: true, plan: user.plan }
await this.activateProduct(order)
return { success: true, type: order.type }
}
}