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
+45 -9
View File
@@ -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,