new-api/controller/redemption.go
zizi 0a0616a370 feat: add subscription redemption support to redemption codes
Redemption codes can now redeem either quota (existing) or a subscription
plan (new). The Redemption model gains Type (1=quota, 2=subscription) and
PlanId fields. When Type=2, Redeem() calls CreateUserSubscriptionFromPlanTx
to activate the plan, reusing all existing subscription logic (purchase
limits, group upgrade, quota reset, expiry). Old codes default to Type=0
which behaves identically to Type=1 (quota).
2026-05-19 01:10:14 +08:00

230 lines
5.7 KiB
Go

package controller
import (
"net/http"
"strconv"
"unicode/utf8"
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/i18n"
"github.com/QuantumNous/new-api/model"
"github.com/QuantumNous/new-api/setting/operation_setting"
"github.com/gin-gonic/gin"
)
func GetAllRedemptions(c *gin.Context) {
pageInfo := common.GetPageQuery(c)
redemptions, total, err := model.GetAllRedemptions(pageInfo.GetStartIdx(), pageInfo.GetPageSize())
if err != nil {
common.ApiError(c, err)
return
}
pageInfo.SetTotal(int(total))
pageInfo.SetItems(redemptions)
common.ApiSuccess(c, pageInfo)
return
}
func SearchRedemptions(c *gin.Context) {
keyword := c.Query("keyword")
pageInfo := common.GetPageQuery(c)
redemptions, total, err := model.SearchRedemptions(keyword, pageInfo.GetStartIdx(), pageInfo.GetPageSize())
if err != nil {
common.ApiError(c, err)
return
}
pageInfo.SetTotal(int(total))
pageInfo.SetItems(redemptions)
common.ApiSuccess(c, pageInfo)
return
}
func GetRedemption(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
common.ApiError(c, err)
return
}
redemption, err := model.GetRedemptionById(id)
if err != nil {
common.ApiError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"data": redemption,
})
return
}
func AddRedemption(c *gin.Context) {
if !operation_setting.IsPaymentComplianceConfirmed() {
common.ApiErrorI18n(c, i18n.MsgPaymentComplianceRequired)
return
}
redemption := model.Redemption{}
err := c.ShouldBindJSON(&redemption)
if err != nil {
common.ApiError(c, err)
return
}
if utf8.RuneCountInString(redemption.Name) == 0 || utf8.RuneCountInString(redemption.Name) > 20 {
common.ApiErrorI18n(c, i18n.MsgRedemptionNameLength)
return
}
if redemption.Count <= 0 {
common.ApiErrorI18n(c, i18n.MsgRedemptionCountPositive)
return
}
if redemption.Count > 100 {
common.ApiErrorI18n(c, i18n.MsgRedemptionCountMax)
return
}
// 校验兑换类型
if redemption.Type != 0 && redemption.Type != common.RedemptionTypeQuota && redemption.Type != common.RedemptionTypeSubscription {
common.ApiErrorI18n(c, i18n.MsgRedemptionTypeInvalid)
return
}
if redemption.Type == common.RedemptionTypeSubscription {
if redemption.PlanId <= 0 {
common.ApiErrorI18n(c, i18n.MsgRedemptionPlanRequired)
return
}
plan, err := model.GetSubscriptionPlanById(redemption.PlanId)
if err != nil || plan == nil {
common.ApiErrorI18n(c, i18n.MsgRedemptionPlanNotFound)
return
}
}
if valid, msg := validateExpiredTime(c, redemption.ExpiredTime); !valid {
c.JSON(http.StatusOK, gin.H{"success": false, "message": msg})
return
}
var keys []string
for i := 0; i < redemption.Count; i++ {
key := common.GetUUID()
cleanRedemption := model.Redemption{
UserId: c.GetInt("id"),
Name: redemption.Name,
Key: key,
Type: redemption.Type,
Quota: redemption.Quota,
PlanId: redemption.PlanId,
CreatedTime: common.GetTimestamp(),
ExpiredTime: redemption.ExpiredTime,
}
err = cleanRedemption.Insert()
if err != nil {
common.SysError("failed to insert redemption: " + err.Error())
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": i18n.T(c, i18n.MsgRedemptionCreateFailed),
"data": keys,
})
return
}
keys = append(keys, key)
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"data": keys,
})
return
}
func DeleteRedemption(c *gin.Context) {
id, _ := strconv.Atoi(c.Param("id"))
err := model.DeleteRedemptionById(id)
if err != nil {
common.ApiError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
})
return
}
func UpdateRedemption(c *gin.Context) {
statusOnly := c.Query("status_only")
redemption := model.Redemption{}
err := c.ShouldBindJSON(&redemption)
if err != nil {
common.ApiError(c, err)
return
}
cleanRedemption, err := model.GetRedemptionById(redemption.Id)
if err != nil {
common.ApiError(c, err)
return
}
if statusOnly == "" {
if valid, msg := validateExpiredTime(c, redemption.ExpiredTime); !valid {
c.JSON(http.StatusOK, gin.H{"success": false, "message": msg})
return
}
// 校验兑换类型
if redemption.Type != 0 && redemption.Type != common.RedemptionTypeQuota && redemption.Type != common.RedemptionTypeSubscription {
common.ApiErrorI18n(c, i18n.MsgRedemptionTypeInvalid)
return
}
if redemption.Type == common.RedemptionTypeSubscription {
if redemption.PlanId <= 0 {
common.ApiErrorI18n(c, i18n.MsgRedemptionPlanRequired)
return
}
plan, err := model.GetSubscriptionPlanById(redemption.PlanId)
if err != nil || plan == nil {
common.ApiErrorI18n(c, i18n.MsgRedemptionPlanNotFound)
return
}
}
// If you add more fields, please also update redemption.Update()
cleanRedemption.Name = redemption.Name
cleanRedemption.Quota = redemption.Quota
cleanRedemption.ExpiredTime = redemption.ExpiredTime
cleanRedemption.Type = redemption.Type
cleanRedemption.PlanId = redemption.PlanId
}
if statusOnly != "" {
cleanRedemption.Status = redemption.Status
}
err = cleanRedemption.Update()
if err != nil {
common.ApiError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"data": cleanRedemption,
})
return
}
func DeleteInvalidRedemption(c *gin.Context) {
rows, err := model.DeleteInvalidRedemptions()
if err != nil {
common.ApiError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"data": rows,
})
return
}
func validateExpiredTime(c *gin.Context, expired int64) (bool, string) {
if expired != 0 && expired < common.GetTimestamp() {
return false, i18n.T(c, i18n.MsgRedemptionExpireTimeInvalid)
}
return true, ""
}