feat: 支付闭环
This commit is contained in:
@@ -69,8 +69,6 @@ func (s *CategoryService) SaveCategory(req radioReq.SaveCategory) error {
|
||||
category := radio.RadioCategory{
|
||||
Name: req.Name,
|
||||
Description: req.Description,
|
||||
IconId: req.IconId,
|
||||
CoverId: req.CoverId,
|
||||
Sort: req.Sort,
|
||||
Status: req.Status,
|
||||
}
|
||||
@@ -82,8 +80,6 @@ func (s *CategoryService) UpdateCategory(req radioReq.UpdateCategory) error {
|
||||
updates := map[string]interface{}{
|
||||
"name": req.Name,
|
||||
"description": req.Description,
|
||||
"icon_id": req.IconId,
|
||||
"cover_id": req.CoverId,
|
||||
"sort": req.Sort,
|
||||
"status": req.Status,
|
||||
}
|
||||
|
||||
@@ -6,7 +6,9 @@ import (
|
||||
common "sundynix-go/model/commom/request"
|
||||
"sundynix-go/model/radio"
|
||||
radioReq "sundynix-go/model/radio/request"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -55,22 +57,56 @@ func (s *ChannelService) GetChannelList(userId string, info radioReq.GetChannelL
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
//查询用户的订阅频道
|
||||
now := time.Now()
|
||||
var subIds []string
|
||||
err = global.DB.Model(&radio.RadioSubscription{}).
|
||||
Where("user_id = ?", userId).
|
||||
Where("status = ?", 1). // 建议将常量也参数化,提高安全性
|
||||
Where("expired_at > ?", now).
|
||||
Pluck("channel_id", &subIds).
|
||||
Error
|
||||
// 使用map
|
||||
subMap := make(map[string]bool)
|
||||
for _, id := range subIds {
|
||||
subMap[id] = true
|
||||
}
|
||||
for i := range list {
|
||||
list[i].HasSubscribed = 0
|
||||
if subMap[list[i].Id] {
|
||||
list[i].HasSubscribed = 1
|
||||
}
|
||||
}
|
||||
return list, total, nil
|
||||
}
|
||||
|
||||
func (s *ChannelService) GetAllChannelList(categoryId, userId string) ([]radio.RadioChannel, error) {
|
||||
var res []radio.RadioChannel
|
||||
err := global.DB.Where("category_id = ?", categoryId).Find(&res).Preload("Cover").Error
|
||||
return res, err
|
||||
}
|
||||
|
||||
// GetChannelById 获取频道详情
|
||||
func (s *ChannelService) GetChannelById(userId, id string) (radio.RadioChannel, error) {
|
||||
var channel radio.RadioChannel
|
||||
err := global.DB.Where("id = ?", id).Preload("Cover").First(&channel).Error
|
||||
err := global.DB.Where("id = ?", id).First(&channel).Error
|
||||
if err != nil {
|
||||
return channel, err
|
||||
}
|
||||
if channel.IsFree == 1 {
|
||||
return channel, nil
|
||||
}
|
||||
channel.HasSubscribed = 0
|
||||
if userId != "" {
|
||||
var sub radio.RadioSubscription
|
||||
err = global.DB.Model(&radio.RadioSubscription{}).
|
||||
Where("user_id = ?", userId).
|
||||
Where("channel_id = ?", id).
|
||||
Where("status = ?", 1).
|
||||
Where("expired_at > ?", time.Now()).
|
||||
First(&sub).Error
|
||||
if err != nil {
|
||||
// 记录日志但不返回错误,避免影响主流程
|
||||
global.Logger.Warn("query subscription status failed", zap.Error(err))
|
||||
return channel, nil
|
||||
}
|
||||
channel.HasSubscribed = 1
|
||||
channel.ExpiredAt = &sub.ExpiredAt
|
||||
}
|
||||
return channel, nil
|
||||
}
|
||||
|
||||
@@ -80,7 +116,7 @@ func (s *ChannelService) SaveChannel(req radioReq.SaveChannel) error {
|
||||
CategoryId: req.CategoryId,
|
||||
Name: req.Name,
|
||||
Description: req.Description,
|
||||
CoverId: req.CoverId,
|
||||
Cover: req.Cover,
|
||||
Tags: req.Tags,
|
||||
IsVipOnly: req.IsVipOnly,
|
||||
MonthlyPrice: req.MonthlyPrice,
|
||||
@@ -98,7 +134,7 @@ func (s *ChannelService) UpdateChannel(req radioReq.UpdateChannel) error {
|
||||
"category_id": req.CategoryId,
|
||||
"name": req.Name,
|
||||
"description": req.Description,
|
||||
"cover_id": req.CoverId,
|
||||
"cover": req.Cover,
|
||||
"tags": req.Tags,
|
||||
"is_vip_only": req.IsVipOnly,
|
||||
"monthly_price": req.MonthlyPrice,
|
||||
|
||||
@@ -6,6 +6,8 @@ type ServiceGroup struct {
|
||||
ProgramService
|
||||
SubscriptionService
|
||||
InteractionService
|
||||
PayService
|
||||
OrderService
|
||||
}
|
||||
|
||||
var GroupApp = new(ServiceGroup)
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package radio
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sundynix-go/model/radio"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type OrderService struct{}
|
||||
|
||||
var OrderServiceApp = new(OrderService)
|
||||
|
||||
// ExecuteOrderUnlock 核心原子操作:解锁权限
|
||||
func (s *OrderService) ExecuteOrderUnlock(tx *gorm.DB, outTradeNo string) error {
|
||||
var order radio.Order
|
||||
// 1. 锁住订单行,防止回调和主动查询并发导致时长翻倍
|
||||
if err := tx.Set("gorm:query_option", "FOR UPDATE").
|
||||
Where("out_trade_no = ?", outTradeNo).First(&order).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 2. 幂等检查
|
||||
if order.Status == 1 {
|
||||
return nil // 已处理,直接返回
|
||||
}
|
||||
|
||||
// 3. 更新订单状态
|
||||
if err := tx.Model(&order).Updates(map[string]interface{}{
|
||||
"status": 1,
|
||||
"pay_status": "SUCCESS",
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 4. 根据订单中的 sub_type 决定增加几个月
|
||||
var months int
|
||||
switch order.SubscriptionType {
|
||||
case "1":
|
||||
months = 1 // 月
|
||||
case "2":
|
||||
months = 3 // 季
|
||||
case "3":
|
||||
months = 12 // 年
|
||||
}
|
||||
|
||||
// 5. 更新或创建订阅权限
|
||||
var sub radio.RadioSubscription
|
||||
now := time.Now()
|
||||
err := tx.Where("user_id = ? AND channel_id = ?", order.UserId, order.ChannelId).First(&sub).Error
|
||||
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// 首次订阅
|
||||
return tx.Create(&radio.RadioSubscription{
|
||||
UserId: order.UserId,
|
||||
ChannelId: order.ChannelId,
|
||||
ExpiredAt: now.AddDate(0, months, 0),
|
||||
Status: 1,
|
||||
}).Error
|
||||
} else {
|
||||
// 续费逻辑
|
||||
newExpiredAt := sub.ExpiredAt
|
||||
if sub.ExpiredAt.Before(now) {
|
||||
newExpiredAt = now // 已过期,从现在开始往后加
|
||||
}
|
||||
return tx.Model(&sub).Updates(map[string]interface{}{
|
||||
"expired_at": newExpiredAt.AddDate(0, months, 0),
|
||||
"status": 1,
|
||||
}).Error
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
package radio
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"sundynix-go/global"
|
||||
"sundynix-go/model/radio"
|
||||
"sundynix-go/model/system"
|
||||
"sundynix-go/utils/wechat"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/core"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/core/auth/verifiers"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/core/notify"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/services/payments"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/utils"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type PayService struct{}
|
||||
|
||||
var PayServiceApp = new(PayService)
|
||||
|
||||
// PrePay 预支付
|
||||
func (s *PayService) PrePay(orderId, userId string) (resp *jsapi.PrepayWithRequestPaymentResponse, err error) {
|
||||
//1.查询订单和 用户
|
||||
var order radio.Order
|
||||
err = global.DB.Where("id = ?", orderId).First(&order).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var user system.User
|
||||
err = global.DB.Where("id = ?", userId).First(&user).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
payClient, err := wechat.GetWxPayClient()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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(order.Amount)),
|
||||
},
|
||||
Payer: &jsapi.Payer{
|
||||
Openid: core.String(user.MiniOpenId),
|
||||
},
|
||||
//Detail: &jsapi.Detail{
|
||||
// CostPrice: core.Int64(608800),
|
||||
// GoodsDetail: []jsapi.GoodsDetail{jsapi.GoodsDetail{
|
||||
// GoodsName: core.String("iPhoneX 256G"),
|
||||
// MerchantGoodsId: core.String("ABC"),
|
||||
// Quantity: core.Int64(1),
|
||||
// UnitPrice: core.Int64(828800),
|
||||
// WechatpayGoodsId: core.String("1001"),
|
||||
// }},
|
||||
// InvoiceId: core.String("wx123"),
|
||||
//}, //选填
|
||||
//SceneInfo: &jsapi.SceneInfo{
|
||||
// DeviceId: core.String("013467007045764"),
|
||||
// PayerClientIp: core.String("14.23.150.211"),
|
||||
// StoreInfo: &jsapi.StoreInfo{
|
||||
// Address: core.String("广东省深圳市南山区科技中一道10000号"),
|
||||
// AreaCode: core.String("440305"),
|
||||
// Id: core.String("0001"),
|
||||
// Name: core.String("腾讯大厦分店"),
|
||||
// },
|
||||
//},
|
||||
//SettleInfo: &jsapi.SettleInfo{
|
||||
// ProfitSharing: core.Bool(false),
|
||||
//}, //选填
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
|
||||
}
|
||||
|
||||
// PayCallback 支付回调
|
||||
func (s *PayService) PayCallback(c *gin.Context) error {
|
||||
//1.加载共钥
|
||||
mchPublicKeyPath := global.Config.WechatPay.PublicKeyPath
|
||||
mchPublicKey, err := utils.LoadPublicKeyWithPath(mchPublicKeyPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx := context.Background()
|
||||
//2.创建客户端
|
||||
handler, err := notify.NewRSANotifyHandler(global.Config.WechatPay.MchAPIv3Key,
|
||||
verifiers.NewSHA256WithRSAPubkeyVerifier(global.Config.WechatPay.PublicKeyId, *mchPublicKey))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
//3.验签 解密
|
||||
//将支付回调通知中的内容,解析为 payments.Transaction。
|
||||
transaction := new(payments.Transaction)
|
||||
notifyReq, err := handler.ParseNotifyRequest(ctx, c.Request, transaction)
|
||||
// 4.如果验签未通过,或者解密失败
|
||||
if err != nil {
|
||||
//应答微信
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
||||
"code": "FAIL",
|
||||
"message": "失败",
|
||||
})
|
||||
global.Logger.Error("wxPay回调-验签或解密:", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
//5.应答微信
|
||||
c.Status(http.StatusOK)
|
||||
// 6.处理通知内容
|
||||
global.Logger.Info("wxPay回调-成功:", zap.Any("notifyReq", notifyReq.Summary))
|
||||
//7. 异步处理数据
|
||||
go func() {
|
||||
err = global.DB.Transaction(func(tx *gorm.DB) error {
|
||||
//7.1 回调记录
|
||||
payNotify := radio.PayNotify{
|
||||
Amount: *transaction.Amount.Total,
|
||||
Currency: *transaction.Amount.Currency,
|
||||
PayerCurrency: *transaction.Amount.PayerCurrency,
|
||||
PayerTotal: *transaction.Amount.PayerTotal,
|
||||
Appid: *transaction.Appid,
|
||||
MchId: *transaction.Mchid,
|
||||
OutTradeNo: *transaction.OutTradeNo,
|
||||
Attach: *transaction.Attach,
|
||||
BankType: *transaction.BankType,
|
||||
Payer: *transaction.Payer.Openid,
|
||||
SuccessTime: *transaction.SuccessTime,
|
||||
TradeState: *transaction.TradeState,
|
||||
TradeStateDesc: *transaction.TradeStateDesc,
|
||||
TradeType: *transaction.TradeType,
|
||||
TransactionId: *transaction.TransactionId,
|
||||
}
|
||||
err = global.DB.Create(&payNotify).Error
|
||||
if err != nil {
|
||||
global.Logger.Error("wxPay回调-存储数据异常:", zap.Error(err))
|
||||
}
|
||||
if payNotify.TradeState == "SUCCESS" {
|
||||
return OrderServiceApp.ExecuteOrderUnlock(tx, *transaction.OutTradeNo)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
}()
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
// QueryPay 根据商户订单号查询订单状态
|
||||
func (s *PayService) QueryPay(no string, userId string) (bool, error) {
|
||||
var order radio.Order
|
||||
if err := global.DB.Where("out_trade_no = ?", no).First(&order).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
// 1. 如果本地已经成功,直接返回成功
|
||||
if order.Status == 1 {
|
||||
return true, nil
|
||||
}
|
||||
//2.本地还是待支付,主动调用微信 API 查询
|
||||
payClient, err := wechat.GetWxPayClient()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
svc := jsapi.JsapiApiService{Client: payClient}
|
||||
resp, _, err := svc.QueryOrderByOutTradeNo(context.Background(), jsapi.QueryOrderByOutTradeNoRequest{
|
||||
OutTradeNo: core.String(no),
|
||||
Mchid: core.String(global.Config.WechatPay.MchId),
|
||||
})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
global.Logger.Info("查询订单状态:", zap.Any("resp", resp))
|
||||
state := *resp.TradeState
|
||||
outTradeNo := *resp.OutTradeNo
|
||||
if state == "SUCCESS" {
|
||||
// 3. 微信那边付过了,本地还没解锁,立即执行事务
|
||||
err = global.DB.Transaction(func(tx *gorm.DB) error {
|
||||
return OrderServiceApp.ExecuteOrderUnlock(tx, outTradeNo)
|
||||
})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
@@ -12,7 +12,7 @@ type ProgramService struct{}
|
||||
|
||||
// GetProgramList 获取节目列表
|
||||
func (s *ProgramService) GetProgramList(info radioReq.GetProgramList) ([]radio.RadioProgram, int64, error) {
|
||||
db := global.DB.Model(&radio.RadioProgram{}).Preload("Cover").Preload("Audio")
|
||||
db := global.DB.Model(&radio.RadioProgram{}).Preload("Audio")
|
||||
var list []radio.RadioProgram
|
||||
var total int64
|
||||
|
||||
@@ -39,18 +39,22 @@ func (s *ProgramService) GetProgramList(info radioReq.GetProgramList) ([]radio.R
|
||||
// GetProgramById 获取节目详情
|
||||
func (s *ProgramService) GetProgramById(id string) (*radio.RadioProgram, error) {
|
||||
var program radio.RadioProgram
|
||||
err := global.DB.Where("id = ?", id).Preload("Cover").Preload("Audio").First(&program).Error
|
||||
err := global.DB.Where("id = ?", id).Preload("Audio").First(&program).Error
|
||||
return &program, err
|
||||
}
|
||||
|
||||
// SaveProgram 保存节目
|
||||
func (s *ProgramService) SaveProgram(req radioReq.SaveProgram) error {
|
||||
var channel radio.RadioChannel
|
||||
if err := global.DB.Where("id = ?", req.ChannelId).First(&channel).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
program := radio.RadioProgram{
|
||||
ChannelId: req.ChannelId,
|
||||
Title: req.Title,
|
||||
Description: req.Description,
|
||||
Content: req.Content,
|
||||
CoverId: req.CoverId,
|
||||
Cover: channel.Cover,
|
||||
AudioId: req.AudioId,
|
||||
Duration: req.Duration,
|
||||
Tags: req.Tags,
|
||||
@@ -66,7 +70,7 @@ func (s *ProgramService) UpdateProgram(req radioReq.UpdateProgram) error {
|
||||
"title": req.Title,
|
||||
"description": req.Description,
|
||||
"content": req.Content,
|
||||
"cover_id": req.CoverId,
|
||||
"cover": req.Cover,
|
||||
"audio_id": req.AudioId,
|
||||
"duration": req.Duration,
|
||||
"tags": req.Tags,
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user