Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

190 lignes
4.4 KiB

  1. package controller
  2. import (
  3. "net/http"
  4. "strconv"
  5. "unicode/utf8"
  6. "github.com/QuantumNous/new-api/common"
  7. "github.com/QuantumNous/new-api/i18n"
  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. common.ApiErrorI18n(c, i18n.MsgRedemptionNameLength)
  63. return
  64. }
  65. if redemption.Count <= 0 {
  66. common.ApiErrorI18n(c, i18n.MsgRedemptionCountPositive)
  67. return
  68. }
  69. if redemption.Count > 100 {
  70. common.ApiErrorI18n(c, i18n.MsgRedemptionCountMax)
  71. return
  72. }
  73. if valid, msg := validateExpiredTime(c, redemption.ExpiredTime); !valid {
  74. c.JSON(http.StatusOK, gin.H{"success": false, "message": msg})
  75. return
  76. }
  77. var keys []string
  78. for i := 0; i < redemption.Count; i++ {
  79. key := common.GetUUID()
  80. cleanRedemption := model.Redemption{
  81. UserId: c.GetInt("id"),
  82. Name: redemption.Name,
  83. Remark: redemption.Remark,
  84. Key: key,
  85. CreatedTime: common.GetTimestamp(),
  86. Quota: redemption.Quota,
  87. ExpiredTime: redemption.ExpiredTime,
  88. }
  89. err = cleanRedemption.Insert()
  90. if err != nil {
  91. common.SysError("failed to insert redemption: " + err.Error())
  92. c.JSON(http.StatusOK, gin.H{
  93. "success": false,
  94. "message": i18n.T(c, i18n.MsgRedemptionCreateFailed),
  95. "data": keys,
  96. })
  97. return
  98. }
  99. keys = append(keys, key)
  100. }
  101. c.JSON(http.StatusOK, gin.H{
  102. "success": true,
  103. "message": "",
  104. "data": keys,
  105. })
  106. return
  107. }
  108. func DeleteRedemption(c *gin.Context) {
  109. id, _ := strconv.Atoi(c.Param("id"))
  110. err := model.DeleteRedemptionById(id)
  111. if err != nil {
  112. common.ApiError(c, err)
  113. return
  114. }
  115. c.JSON(http.StatusOK, gin.H{
  116. "success": true,
  117. "message": "",
  118. })
  119. return
  120. }
  121. func UpdateRedemption(c *gin.Context) {
  122. statusOnly := c.Query("status_only")
  123. redemption := model.Redemption{}
  124. err := c.ShouldBindJSON(&redemption)
  125. if err != nil {
  126. common.ApiError(c, err)
  127. return
  128. }
  129. cleanRedemption, err := model.GetRedemptionById(redemption.Id)
  130. if err != nil {
  131. common.ApiError(c, err)
  132. return
  133. }
  134. if statusOnly == "" {
  135. if valid, msg := validateExpiredTime(c, redemption.ExpiredTime); !valid {
  136. c.JSON(http.StatusOK, gin.H{"success": false, "message": msg})
  137. return
  138. }
  139. // If you add more fields, please also update redemption.Update()
  140. cleanRedemption.Name = redemption.Name
  141. cleanRedemption.Remark = redemption.Remark
  142. cleanRedemption.Quota = redemption.Quota
  143. cleanRedemption.ExpiredTime = redemption.ExpiredTime
  144. }
  145. if statusOnly != "" {
  146. cleanRedemption.Status = redemption.Status
  147. }
  148. err = cleanRedemption.Update()
  149. if err != nil {
  150. common.ApiError(c, err)
  151. return
  152. }
  153. c.JSON(http.StatusOK, gin.H{
  154. "success": true,
  155. "message": "",
  156. "data": cleanRedemption,
  157. })
  158. return
  159. }
  160. func DeleteInvalidRedemption(c *gin.Context) {
  161. rows, err := model.DeleteInvalidRedemptions()
  162. if err != nil {
  163. common.ApiError(c, err)
  164. return
  165. }
  166. c.JSON(http.StatusOK, gin.H{
  167. "success": true,
  168. "message": "",
  169. "data": rows,
  170. })
  171. return
  172. }
  173. func validateExpiredTime(c *gin.Context, expired int64) (bool, string) {
  174. if expired != 0 && expired < common.GetTimestamp() {
  175. return false, i18n.T(c, i18n.MsgRedemptionExpireTimeInvalid)
  176. }
  177. return true, ""
  178. }