token.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 GetTokenStatus(c *gin.Context) {
  74. tokenId := c.GetInt("token_id")
  75. userId := c.GetInt("id")
  76. token, err := model.GetTokenByIds(tokenId, userId)
  77. if err != nil {
  78. c.JSON(http.StatusOK, gin.H{
  79. "success": false,
  80. "message": err.Error(),
  81. })
  82. return
  83. }
  84. expiredAt := token.ExpiredTime
  85. if expiredAt == -1 {
  86. expiredAt = 0
  87. }
  88. c.JSON(http.StatusOK, gin.H{
  89. "object": "credit_summary",
  90. "total_granted": token.RemainQuota,
  91. "total_used": 0, // not supported currently
  92. "total_available": token.RemainQuota,
  93. "expires_at": expiredAt * 1000,
  94. })
  95. }
  96. func AddToken(c *gin.Context) {
  97. token := model.Token{}
  98. err := c.ShouldBindJSON(&token)
  99. if err != nil {
  100. c.JSON(http.StatusOK, gin.H{
  101. "success": false,
  102. "message": err.Error(),
  103. })
  104. return
  105. }
  106. if len(token.Name) == 0 || len(token.Name) > 20 {
  107. c.JSON(http.StatusOK, gin.H{
  108. "success": false,
  109. "message": "令牌名称长度必须在1-20之间",
  110. })
  111. return
  112. }
  113. cleanToken := model.Token{
  114. UserId: c.GetInt("id"),
  115. Name: token.Name,
  116. Key: common.GetUUID(),
  117. CreatedTime: common.GetTimestamp(),
  118. AccessedTime: common.GetTimestamp(),
  119. ExpiredTime: token.ExpiredTime,
  120. RemainQuota: token.RemainQuota,
  121. UnlimitedQuota: token.UnlimitedQuota,
  122. }
  123. err = cleanToken.Insert()
  124. if err != nil {
  125. c.JSON(http.StatusOK, gin.H{
  126. "success": false,
  127. "message": err.Error(),
  128. })
  129. return
  130. }
  131. c.JSON(http.StatusOK, gin.H{
  132. "success": true,
  133. "message": "",
  134. })
  135. return
  136. }
  137. func DeleteToken(c *gin.Context) {
  138. id, _ := strconv.Atoi(c.Param("id"))
  139. userId := c.GetInt("id")
  140. err := model.DeleteTokenById(id, userId)
  141. if err != nil {
  142. c.JSON(http.StatusOK, gin.H{
  143. "success": false,
  144. "message": err.Error(),
  145. })
  146. return
  147. }
  148. c.JSON(http.StatusOK, gin.H{
  149. "success": true,
  150. "message": "",
  151. })
  152. return
  153. }
  154. func UpdateToken(c *gin.Context) {
  155. isAdmin := c.GetInt("role") >= common.RoleAdminUser
  156. userId := c.GetInt("id")
  157. statusOnly := c.Query("status_only")
  158. token := model.Token{}
  159. err := c.ShouldBindJSON(&token)
  160. if err != nil {
  161. c.JSON(http.StatusOK, gin.H{
  162. "success": false,
  163. "message": err.Error(),
  164. })
  165. return
  166. }
  167. cleanToken, err := model.GetTokenByIds(token.Id, userId)
  168. if err != nil {
  169. c.JSON(http.StatusOK, gin.H{
  170. "success": false,
  171. "message": err.Error(),
  172. })
  173. return
  174. }
  175. if token.Status == common.TokenStatusEnabled {
  176. if cleanToken.Status == common.TokenStatusExpired && cleanToken.ExpiredTime <= common.GetTimestamp() {
  177. c.JSON(http.StatusOK, gin.H{
  178. "success": false,
  179. "message": "令牌已过期,无法启用,请先修改令牌过期时间",
  180. })
  181. return
  182. }
  183. if cleanToken.Status == common.TokenStatusExhausted && cleanToken.RemainQuota <= 0 && !cleanToken.UnlimitedQuota {
  184. c.JSON(http.StatusOK, gin.H{
  185. "success": false,
  186. "message": "令牌可用次数已用尽,无法启用,请先修改令牌剩余次数,或者设置为无限次数",
  187. })
  188. return
  189. }
  190. }
  191. if statusOnly != "" {
  192. cleanToken.Status = token.Status
  193. } else {
  194. // If you add more fields, please also update token.Update()
  195. cleanToken.Name = token.Name
  196. cleanToken.ExpiredTime = token.ExpiredTime
  197. if isAdmin {
  198. cleanToken.RemainQuota = token.RemainQuota
  199. cleanToken.UnlimitedQuota = token.UnlimitedQuota
  200. }
  201. }
  202. err = cleanToken.Update()
  203. if err != nil {
  204. c.JSON(http.StatusOK, gin.H{
  205. "success": false,
  206. "message": err.Error(),
  207. })
  208. return
  209. }
  210. c.JSON(http.StatusOK, gin.H{
  211. "success": true,
  212. "message": "",
  213. "data": cleanToken,
  214. })
  215. return
  216. }