redemption.go 4.3 KB

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