redemption.go 4.3 KB

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