redemption.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package controller
  2. import (
  3. "errors"
  4. "net/http"
  5. "one-api/common"
  6. "one-api/model"
  7. "strconv"
  8. "github.com/gin-gonic/gin"
  9. )
  10. func GetAllRedemptions(c *gin.Context) {
  11. pageInfo := common.GetPageQuery(c)
  12. redemptions, total, err := model.GetAllRedemptions(pageInfo.GetStartIdx(), pageInfo.GetPageSize())
  13. if err != nil {
  14. common.ApiError(c, err)
  15. return
  16. }
  17. pageInfo.SetTotal(int(total))
  18. pageInfo.SetItems(redemptions)
  19. common.ApiSuccess(c, pageInfo)
  20. return
  21. }
  22. func SearchRedemptions(c *gin.Context) {
  23. keyword := c.Query("keyword")
  24. pageInfo := common.GetPageQuery(c)
  25. redemptions, total, err := model.SearchRedemptions(keyword, pageInfo.GetStartIdx(), pageInfo.GetPageSize())
  26. if err != nil {
  27. common.ApiError(c, err)
  28. return
  29. }
  30. pageInfo.SetTotal(int(total))
  31. pageInfo.SetItems(redemptions)
  32. common.ApiSuccess(c, pageInfo)
  33. return
  34. }
  35. func GetRedemption(c *gin.Context) {
  36. id, err := strconv.Atoi(c.Param("id"))
  37. if err != nil {
  38. common.ApiError(c, err)
  39. return
  40. }
  41. redemption, err := model.GetRedemptionById(id)
  42. if err != nil {
  43. common.ApiError(c, err)
  44. return
  45. }
  46. c.JSON(http.StatusOK, gin.H{
  47. "success": true,
  48. "message": "",
  49. "data": redemption,
  50. })
  51. return
  52. }
  53. func AddRedemption(c *gin.Context) {
  54. redemption := model.Redemption{}
  55. err := c.ShouldBindJSON(&redemption)
  56. if err != nil {
  57. common.ApiError(c, err)
  58. return
  59. }
  60. if len(redemption.Name) == 0 || len(redemption.Name) > 20 {
  61. c.JSON(http.StatusOK, gin.H{
  62. "success": false,
  63. "message": "兑换码名称长度必须在1-20之间",
  64. })
  65. return
  66. }
  67. if redemption.Count <= 0 {
  68. c.JSON(http.StatusOK, gin.H{
  69. "success": false,
  70. "message": "兑换码个数必须大于0",
  71. })
  72. return
  73. }
  74. if redemption.Count > 100 {
  75. c.JSON(http.StatusOK, gin.H{
  76. "success": false,
  77. "message": "一次兑换码批量生成的个数不能大于 100",
  78. })
  79. return
  80. }
  81. if err := validateExpiredTime(redemption.ExpiredTime); err != nil {
  82. c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()})
  83. return
  84. }
  85. var keys []string
  86. for i := 0; i < redemption.Count; i++ {
  87. key := common.GetUUID()
  88. cleanRedemption := model.Redemption{
  89. UserId: c.GetInt("id"),
  90. Name: redemption.Name,
  91. Key: key,
  92. CreatedTime: common.GetTimestamp(),
  93. Quota: redemption.Quota,
  94. ExpiredTime: redemption.ExpiredTime,
  95. }
  96. err = cleanRedemption.Insert()
  97. if err != nil {
  98. c.JSON(http.StatusOK, gin.H{
  99. "success": false,
  100. "message": err.Error(),
  101. "data": keys,
  102. })
  103. return
  104. }
  105. keys = append(keys, key)
  106. }
  107. c.JSON(http.StatusOK, gin.H{
  108. "success": true,
  109. "message": "",
  110. "data": keys,
  111. })
  112. return
  113. }
  114. func DeleteRedemption(c *gin.Context) {
  115. id, _ := strconv.Atoi(c.Param("id"))
  116. err := model.DeleteRedemptionById(id)
  117. if err != nil {
  118. common.ApiError(c, err)
  119. return
  120. }
  121. c.JSON(http.StatusOK, gin.H{
  122. "success": true,
  123. "message": "",
  124. })
  125. return
  126. }
  127. func UpdateRedemption(c *gin.Context) {
  128. statusOnly := c.Query("status_only")
  129. redemption := model.Redemption{}
  130. err := c.ShouldBindJSON(&redemption)
  131. if err != nil {
  132. common.ApiError(c, err)
  133. return
  134. }
  135. cleanRedemption, err := model.GetRedemptionById(redemption.Id)
  136. if err != nil {
  137. common.ApiError(c, err)
  138. return
  139. }
  140. if statusOnly == "" {
  141. if err := validateExpiredTime(redemption.ExpiredTime); err != nil {
  142. c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()})
  143. return
  144. }
  145. // If you add more fields, please also update redemption.Update()
  146. cleanRedemption.Name = redemption.Name
  147. cleanRedemption.Quota = redemption.Quota
  148. cleanRedemption.ExpiredTime = redemption.ExpiredTime
  149. }
  150. if statusOnly != "" {
  151. cleanRedemption.Status = redemption.Status
  152. }
  153. err = cleanRedemption.Update()
  154. if err != nil {
  155. common.ApiError(c, err)
  156. return
  157. }
  158. c.JSON(http.StatusOK, gin.H{
  159. "success": true,
  160. "message": "",
  161. "data": cleanRedemption,
  162. })
  163. return
  164. }
  165. func DeleteInvalidRedemption(c *gin.Context) {
  166. rows, err := model.DeleteInvalidRedemptions()
  167. if err != nil {
  168. common.ApiError(c, err)
  169. return
  170. }
  171. c.JSON(http.StatusOK, gin.H{
  172. "success": true,
  173. "message": "",
  174. "data": rows,
  175. })
  176. return
  177. }
  178. func validateExpiredTime(expired int64) error {
  179. if expired != 0 && expired < common.GetTimestamp() {
  180. return errors.New("过期时间不能早于当前时间")
  181. }
  182. return nil
  183. }