2023-04-26 09:02:26 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-01-28 11:38:58 +00:00
|
|
|
"github.com/songquanpeng/one-api/common/config"
|
2024-04-21 11:43:23 +00:00
|
|
|
"github.com/songquanpeng/one-api/common/ctxkey"
|
2024-01-28 11:38:58 +00:00
|
|
|
"github.com/songquanpeng/one-api/common/helper"
|
2024-04-05 17:02:35 +00:00
|
|
|
"github.com/songquanpeng/one-api/common/random"
|
2024-01-28 11:38:58 +00:00
|
|
|
"github.com/songquanpeng/one-api/model"
|
2023-04-26 09:02:26 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetAllRedemptions(c *gin.Context) {
|
|
|
|
p, _ := strconv.Atoi(c.Query("p"))
|
|
|
|
if p < 0 {
|
|
|
|
p = 0
|
|
|
|
}
|
2024-01-21 15:21:42 +00:00
|
|
|
redemptions, err := model.GetAllRedemptions(p*config.ItemsPerPage, config.ItemsPerPage)
|
2023-04-26 09:02:26 +00:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": true,
|
|
|
|
"message": "",
|
|
|
|
"data": redemptions,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func SearchRedemptions(c *gin.Context) {
|
|
|
|
keyword := c.Query("keyword")
|
|
|
|
redemptions, err := model.SearchRedemptions(keyword)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": true,
|
|
|
|
"message": "",
|
|
|
|
"data": redemptions,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetRedemption(c *gin.Context) {
|
|
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
redemption, err := model.GetRedemptionById(id)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": true,
|
|
|
|
"message": "",
|
|
|
|
"data": redemption,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddRedemption(c *gin.Context) {
|
|
|
|
redemption := model.Redemption{}
|
|
|
|
err := c.ShouldBindJSON(&redemption)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(redemption.Name) == 0 || len(redemption.Name) > 20 {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": "兑换码名称长度必须在1-20之间",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if redemption.Count <= 0 {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": "兑换码个数必须大于0",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if redemption.Count > 100 {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": "一次兑换码批量生成的个数不能大于 100",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var keys []string
|
|
|
|
for i := 0; i < redemption.Count; i++ {
|
2024-04-05 17:02:35 +00:00
|
|
|
key := random.GetUUID()
|
2023-04-26 09:02:26 +00:00
|
|
|
cleanRedemption := model.Redemption{
|
2024-04-21 11:43:23 +00:00
|
|
|
UserId: c.GetInt(ctxkey.Id),
|
2023-04-26 09:02:26 +00:00
|
|
|
Name: redemption.Name,
|
|
|
|
Key: key,
|
2024-01-21 15:21:42 +00:00
|
|
|
CreatedTime: helper.GetTimestamp(),
|
2023-04-26 09:02:26 +00:00
|
|
|
Quota: redemption.Quota,
|
|
|
|
}
|
|
|
|
err = cleanRedemption.Insert()
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": err.Error(),
|
|
|
|
"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 {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": err.Error(),
|
|
|
|
})
|
|
|
|
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 {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cleanRedemption, err := model.GetRedemptionById(redemption.Id)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if statusOnly != "" {
|
|
|
|
cleanRedemption.Status = redemption.Status
|
|
|
|
} else {
|
|
|
|
// If you add more fields, please also update redemption.Update()
|
|
|
|
cleanRedemption.Name = redemption.Name
|
|
|
|
cleanRedemption.Quota = redemption.Quota
|
|
|
|
}
|
|
|
|
err = cleanRedemption.Update()
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": true,
|
|
|
|
"message": "",
|
|
|
|
"data": cleanRedemption,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|