feat: 支付闭环

This commit is contained in:
Blizzard
2026-03-04 17:05:48 +08:00
parent 042c99aa46
commit 7a32f8a351
31 changed files with 902 additions and 503 deletions
+69 -108
View File
@@ -1,19 +1,22 @@
package radio
import (
"context"
"errors"
"sundynix-go/global"
common "sundynix-go/model/commom/request"
"sundynix-go/model/radio"
"time"
"sundynix-go/model/radio/request"
"sundynix-go/utils/uniqueid"
"sundynix-go/utils/wechat"
"github.com/wechatpay-apiv3/wechatpay-go/core"
"github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi"
"gorm.io/gorm"
)
type SubscriptionService struct{}
const MaxFreeSubscription = 2
// GetUserSubscription 获取用户订阅列表
func (s *SubscriptionService) GetUserSubscription(userId string, info common.PageInfo) ([]radio.RadioSubscription, int64, error) {
var subscriptions []radio.RadioSubscription
@@ -34,7 +37,6 @@ func (s *SubscriptionService) GetUserSubscription(userId string, info common.Pag
Order("created_at DESC").
// 级联加载频道及其封面
Preload("Channel").
Preload("Channel.Cover").
// 关键:子查询过滤——只预加载每个频道 ID 最大的那一条节目
Preload("Channel.Programs", func(db *gorm.DB) *gorm.DB {
// 子查询:找到每个频道下 ID 最大的节目(通常 ID 越大代表越新,也可以用 CreatedAt)
@@ -44,9 +46,7 @@ func (s *SubscriptionService) GetUserSubscription(userId string, info common.Pag
Where("created_at = (SELECT MAX(created_at) FROM sundynix_radio_program AS rp WHERE rp.channel_id = sundynix_radio_program.channel_id AND rp.status = 1)")
// 嵌套预加载:节目里的音频和封面也一并带出来
return db.Where("id IN (?)", subQuery).
Preload("Cover").
Preload("Audio")
return db.Where("id IN (?)", subQuery).Preload("Audio")
}).
Find(&subscriptions).Error
@@ -56,115 +56,76 @@ func (s *SubscriptionService) GetUserSubscription(userId string, info common.Pag
return subscriptions, total, err
}
// GetUserSubscriptionHistory 获取用户历史订阅过的频道ID列表
func (s *SubscriptionService) GetUserSubscriptionHistory(userId string) ([]string, error) {
var channelIds []string
err := global.DB.Model(&radio.RadioSubscription{}).
Where("user_id = ?", userId).
Pluck("channel_id", &channelIds).Error
return channelIds, err
}
// HasEverSubscribed 检查用户是否曾经订阅过该频道(包括已取消的)
func (s *SubscriptionService) HasEverSubscribed(userId, channelId string) (bool, error) {
var count int64
// 使用Unscoped查询包括已软删除的记录
err := global.DB.Unscoped().Model(&radio.RadioSubscription{}).
Where("user_id = ? AND channel_id = ?", userId, channelId).
Count(&count).Error
return count > 0, err
}
// HasSubscription 检查用户当前是否订阅该频道(未取消的)
func (s *SubscriptionService) HasSubscription(userId, channelId string) (bool, error) {
var count int64
err := global.DB.Model(&radio.RadioSubscription{}).
Where("user_id = ? AND channel_id = ?", userId, channelId).
Count(&count).Error
return count > 0, err
}
// CanSubscribe 检查是否可以订阅
// 规则:
// 1. 如果用户是VIP且未过期,可以订阅任意频道
// 2. 如果用户曾经订阅过该频道(取消后再订阅),可以免费订阅
// 3. 否则检查当前有效订阅数量是否达到上限(2个)
func (s *SubscriptionService) CanSubscribe(userId, channelId string) (bool, string, error) {
// 检查是否已经是订阅用户(未取消的订阅)
var existing radio.RadioSubscription
err := global.DB.Where("user_id = ? AND channel_id = ?", userId, channelId).First(&existing).Error
if err == nil {
return false, "您已订阅该频道", nil
}
if !errors.Is(err, gorm.ErrRecordNotFound) {
return false, "", err
}
// 获取radio_user检查VIP状态
var radioUser radio.RadioUser
err = global.DB.Where("user_id = ?", userId).First(&radioUser).Error
if err == nil && radioUser.IsVip == 1 {
// 检查VIP是否过期
if radioUser.VipExpireAt != nil && *radioUser.VipExpireAt > time.Now().Unix() {
return true, "", nil
}
}
// 检查用户是否曾经订阅过该频道(取消后又订阅的情况)
hasEverSubscribed, err := s.HasEverSubscribed(userId, channelId)
// UnlockChannel 解锁频道
func (s *SubscriptionService) UnlockChannel(userId string, req request.UnlockChannel) (resp *jsapi.PrepayWithRequestPaymentResponse, no string, err error) {
//1.查询频道
var channel radio.RadioChannel
err = global.DB.Where("id = ?", req.ChannelId).First(&channel).Error
if err != nil {
return false, "", err
return nil, "", err
}
if hasEverSubscribed {
// 曾今订阅过,可以免费再次订阅
return true, "", nil
//2.创建一个订单 根据eventType 创建不同的订单
var price int
var orderName string
switch req.EventType {
case "1":
price = channel.MonthlyPrice //包月
orderName = channel.Name + " - 月度订阅"
case "2":
price = channel.QuarterlyPrice //包季
orderName = channel.Name + " - 季度订阅"
case "3":
price = channel.AnnualPrice //包年
orderName = channel.Name + " - 年度订阅"
default:
return nil, "", errors.New("无效的订阅类型")
}
// 非VIP用户,检查当前有效订阅数量(排除已取消的)
var count int64
err = global.DB.Model(&radio.RadioSubscription{}).Where("user_id = ?", userId).Count(&count).Error
order := radio.Order{
UserId: userId,
OutTradeNo: uniqueid.GenOrderNo(),
ChannelId: req.ChannelId,
SubscriptionType: req.EventType,
Amount: price,
Name: orderName,
}
err = global.DB.Create(&order).Error
if err != nil {
return false, "", err
return nil, "", err
}
if count >= MaxFreeSubscription {
return false, "免费订阅数量已达上限(2个),请开通VIP或订阅付费频道", nil
}
return true, "", nil
}
// Subscribe 订阅频道
func (s *SubscriptionService) Subscribe(userId, channelId string, subType int) error {
subscription := radio.RadioSubscription{
UserId: userId,
ChannelId: channelId,
}
return global.DB.Create(&subscription).Error
}
// Unsubscribe 退订频道(逻辑删除,更新删除时间表示已取消)
func (s *SubscriptionService) Unsubscribe(userId, channelId string) error {
// 软删除:将DeletedAt设置为当前时间,表示已取消订阅
// 这样用户可以再次免费订阅该频道
return global.DB.Model(&radio.RadioSubscription{}).
Where("user_id = ? AND channel_id = ?", userId, channelId).
Update("deleted_at", time.Now()).Error
}
// GetVipStatus 获取VIP状态
func (s *SubscriptionService) GetVipStatus(userId string) (bool, int64, error) {
var radioUser radio.RadioUser
err := global.DB.Where("user_id = ?", userId).First(&radioUser).Error
//4.调用微信api 拉起支付
var user radio.RadioUser
err = global.DB.Where("user_id = ?", userId).First(&user).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return false, 0, nil
}
return false, 0, err
return nil, "", err
}
payClient, err := wechat.GetWxPayClient()
if err != nil {
return nil, "", err
}
if radioUser.IsVip == 1 && radioUser.VipExpireAt != nil && *radioUser.VipExpireAt > time.Now().Unix() {
return true, *radioUser.VipExpireAt, nil
svc := jsapi.JsapiApiService{Client: payClient}
result, _, err := svc.PrepayWithRequestPayment(context.Background(),
jsapi.PrepayRequest{
Appid: core.String(global.Config.MiniProgram.AppId),
Mchid: core.String(global.Config.WechatPay.MchId),
Description: core.String(order.Name),
OutTradeNo: core.String(order.OutTradeNo),
//TimeExpire: core.Time(time.Now()), //选填
//Attach: core.String("自定义数据说明"), //选填
NotifyUrl: core.String(global.Config.WechatPay.NotifyUrl),
//GoodsTag: core.String("WXG"), //选填
//SupportFapiao: core.Bool(false), //选填
Amount: &jsapi.Amount{
Currency: core.String("CNY"),
Total: core.Int64(int64(price)),
},
Payer: &jsapi.Payer{
Openid: core.String(user.OpenId),
},
})
if err != nil {
return nil, "", err
}
return false, 0, nil
return result, order.OutTradeNo, nil
}