redemption.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package controller
  2. import (
  3. "errors"
  4. "net/http"
  5. "strconv"
  6. "unicode/utf8"
  7. "github.com/QuantumNous/new-api/common"
  8. "github.com/QuantumNous/new-api/model"
  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. common.SysError("failed to insert redemption: " + err.Error())
  100. c.JSON(http.StatusOK, gin.H{
  101. "success": false,
  102. "message": "创建兑换码失败,请稍后重试",
  103. "data": keys,
  104. })
  105. return
  106. }
  107. keys = append(keys, key)
  108. }
  109. c.JSON(http.StatusOK, gin.H{
  110. "success": true,
  111. "message": "",
  112. "data": keys,
  113. })
  114. return
  115. }
  116. func DeleteRedemption(c *gin.Context) {
  117. id, _ := strconv.Atoi(c.Param("id"))
  118. err := model.DeleteRedemptionById(id)
  119. if err != nil {
  120. common.ApiError(c, err)
  121. return
  122. }
  123. c.JSON(http.StatusOK, gin.H{
  124. "success": true,
  125. "message": "",
  126. })
  127. return
  128. }
  129. func UpdateRedemption(c *gin.Context) {
  130. statusOnly := c.Query("status_only")
  131. redemption := model.Redemption{}
  132. err := c.ShouldBindJSON(&redemption)
  133. if err != nil {
  134. common.ApiError(c, err)
  135. return
  136. }
  137. cleanRedemption, err := model.GetRedemptionById(redemption.Id)
  138. if err != nil {
  139. common.ApiError(c, err)
  140. return
  141. }
  142. if statusOnly == "" {
  143. if err := validateExpiredTime(redemption.ExpiredTime); err != nil {
  144. c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()})
  145. return
  146. }
  147. // If you add more fields, please also update redemption.Update()
  148. cleanRedemption.Name = redemption.Name
  149. cleanRedemption.Quota = redemption.Quota
  150. cleanRedemption.ExpiredTime = redemption.ExpiredTime
  151. }
  152. if statusOnly != "" {
  153. cleanRedemption.Status = redemption.Status
  154. }
  155. err = cleanRedemption.Update()
  156. if err != nil {
  157. common.ApiError(c, err)
  158. return
  159. }
  160. c.JSON(http.StatusOK, gin.H{
  161. "success": true,
  162. "message": "",
  163. "data": cleanRedemption,
  164. })
  165. return
  166. }
  167. func DeleteInvalidRedemption(c *gin.Context) {
  168. rows, err := model.DeleteInvalidRedemptions()
  169. if err != nil {
  170. common.ApiError(c, err)
  171. return
  172. }
  173. c.JSON(http.StatusOK, gin.H{
  174. "success": true,
  175. "message": "",
  176. "data": rows,
  177. })
  178. return
  179. }
  180. func validateExpiredTime(expired int64) error {
  181. if expired != 0 && expired < common.GetTimestamp() {
  182. return errors.New("过期时间不能早于当前时间")
  183. }
  184. return nil
  185. }