token.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. "one-api/common"
  6. "one-api/model"
  7. "strconv"
  8. )
  9. func GetAllTokens(c *gin.Context) {
  10. userId := c.GetInt("id")
  11. p, _ := strconv.Atoi(c.Query("p"))
  12. if p < 0 {
  13. p = 0
  14. }
  15. tokens, err := model.GetAllUserTokens(userId, p*common.ItemsPerPage, common.ItemsPerPage)
  16. if err != nil {
  17. c.JSON(http.StatusOK, gin.H{
  18. "success": false,
  19. "message": err.Error(),
  20. })
  21. return
  22. }
  23. c.JSON(http.StatusOK, gin.H{
  24. "success": true,
  25. "message": "",
  26. "data": tokens,
  27. })
  28. return
  29. }
  30. func SearchTokens(c *gin.Context) {
  31. userId := c.GetInt("id")
  32. keyword := c.Query("keyword")
  33. tokens, err := model.SearchUserTokens(userId, keyword)
  34. if err != nil {
  35. c.JSON(http.StatusOK, gin.H{
  36. "success": false,
  37. "message": err.Error(),
  38. })
  39. return
  40. }
  41. c.JSON(http.StatusOK, gin.H{
  42. "success": true,
  43. "message": "",
  44. "data": tokens,
  45. })
  46. return
  47. }
  48. func GetToken(c *gin.Context) {
  49. id, err := strconv.Atoi(c.Param("id"))
  50. userId := c.GetInt("id")
  51. if err != nil {
  52. c.JSON(http.StatusOK, gin.H{
  53. "success": false,
  54. "message": err.Error(),
  55. })
  56. return
  57. }
  58. token, err := model.GetTokenByIds(id, userId)
  59. if err != nil {
  60. c.JSON(http.StatusOK, gin.H{
  61. "success": false,
  62. "message": err.Error(),
  63. })
  64. return
  65. }
  66. c.JSON(http.StatusOK, gin.H{
  67. "success": true,
  68. "message": "",
  69. "data": token,
  70. })
  71. return
  72. }
  73. func AddToken(c *gin.Context) {
  74. token := model.Token{}
  75. err := c.ShouldBindJSON(&token)
  76. if err != nil {
  77. c.JSON(http.StatusOK, gin.H{
  78. "success": false,
  79. "message": err.Error(),
  80. })
  81. return
  82. }
  83. if len(token.Name) == 0 || len(token.Name) > 20 {
  84. c.JSON(http.StatusOK, gin.H{
  85. "success": false,
  86. "message": "令牌名称长度必须在1-20之间",
  87. })
  88. return
  89. }
  90. cleanToken := model.Token{
  91. UserId: c.GetInt("id"),
  92. Name: token.Name,
  93. Key: common.GetUUID(),
  94. CreatedTime: common.GetTimestamp(),
  95. AccessedTime: common.GetTimestamp(),
  96. ExpiredTime: token.ExpiredTime,
  97. RemainTimes: token.RemainTimes,
  98. }
  99. err = cleanToken.Insert()
  100. if err != nil {
  101. c.JSON(http.StatusOK, gin.H{
  102. "success": false,
  103. "message": err.Error(),
  104. })
  105. return
  106. }
  107. c.JSON(http.StatusOK, gin.H{
  108. "success": true,
  109. "message": "",
  110. })
  111. return
  112. }
  113. func DeleteToken(c *gin.Context) {
  114. id, _ := strconv.Atoi(c.Param("id"))
  115. userId := c.GetInt("id")
  116. err := model.DeleteTokenById(id, userId)
  117. if err != nil {
  118. c.JSON(http.StatusOK, gin.H{
  119. "success": false,
  120. "message": err.Error(),
  121. })
  122. return
  123. }
  124. c.JSON(http.StatusOK, gin.H{
  125. "success": true,
  126. "message": "",
  127. })
  128. return
  129. }
  130. func UpdateToken(c *gin.Context) {
  131. userId := c.GetInt("id")
  132. token := model.Token{}
  133. err := c.ShouldBindJSON(&token)
  134. if err != nil {
  135. c.JSON(http.StatusOK, gin.H{
  136. "success": false,
  137. "message": err.Error(),
  138. })
  139. return
  140. }
  141. cleanToken, err := model.GetTokenByIds(token.Id, userId)
  142. if err != nil {
  143. c.JSON(http.StatusOK, gin.H{
  144. "success": false,
  145. "message": err.Error(),
  146. })
  147. return
  148. }
  149. if token.Status == common.TokenStatusEnabled {
  150. if cleanToken.Status == common.TokenStatusExpired && cleanToken.ExpiredTime <= common.GetTimestamp() {
  151. c.JSON(http.StatusOK, gin.H{
  152. "success": false,
  153. "message": "令牌已过期,无法启用,请先修改令牌过期时间",
  154. })
  155. return
  156. }
  157. if cleanToken.Status == common.TokenStatusExhausted && cleanToken.RemainTimes == 0 {
  158. c.JSON(http.StatusOK, gin.H{
  159. "success": false,
  160. "message": "令牌可用次数已用尽,无法启用,请先修改令牌剩余次数",
  161. })
  162. return
  163. }
  164. }
  165. cleanToken.Name = token.Name
  166. cleanToken.Status = token.Status
  167. cleanToken.ExpiredTime = token.ExpiredTime
  168. cleanToken.RemainTimes = token.RemainTimes
  169. err = cleanToken.Update()
  170. if err != nil {
  171. c.JSON(http.StatusOK, gin.H{
  172. "success": false,
  173. "message": err.Error(),
  174. })
  175. return
  176. }
  177. c.JSON(http.StatusOK, gin.H{
  178. "success": true,
  179. "message": "",
  180. "data": cleanToken,
  181. })
  182. return
  183. }