token.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. size, _ := strconv.Atoi(c.Query("size"))
  13. if p < 0 {
  14. p = 0
  15. }
  16. if size <= 0 {
  17. size = common.ItemsPerPage
  18. } else if size > 100 {
  19. size = 100
  20. }
  21. tokens, err := model.GetAllUserTokens(userId, p*size, size)
  22. if err != nil {
  23. c.JSON(http.StatusOK, gin.H{
  24. "success": false,
  25. "message": err.Error(),
  26. })
  27. return
  28. }
  29. c.JSON(http.StatusOK, gin.H{
  30. "success": true,
  31. "message": "",
  32. "data": tokens,
  33. })
  34. return
  35. }
  36. func SearchTokens(c *gin.Context) {
  37. userId := c.GetInt("id")
  38. keyword := c.Query("keyword")
  39. tokens, err := model.SearchUserTokens(userId, keyword)
  40. if err != nil {
  41. c.JSON(http.StatusOK, gin.H{
  42. "success": false,
  43. "message": err.Error(),
  44. })
  45. return
  46. }
  47. c.JSON(http.StatusOK, gin.H{
  48. "success": true,
  49. "message": "",
  50. "data": tokens,
  51. })
  52. return
  53. }
  54. func GetToken(c *gin.Context) {
  55. id, err := strconv.Atoi(c.Param("id"))
  56. userId := c.GetInt("id")
  57. if err != nil {
  58. c.JSON(http.StatusOK, gin.H{
  59. "success": false,
  60. "message": err.Error(),
  61. })
  62. return
  63. }
  64. token, err := model.GetTokenByIds(id, userId)
  65. if err != nil {
  66. c.JSON(http.StatusOK, gin.H{
  67. "success": false,
  68. "message": err.Error(),
  69. })
  70. return
  71. }
  72. c.JSON(http.StatusOK, gin.H{
  73. "success": true,
  74. "message": "",
  75. "data": token,
  76. })
  77. return
  78. }
  79. func GetTokenStatus(c *gin.Context) {
  80. tokenId := c.GetInt("token_id")
  81. userId := c.GetInt("id")
  82. token, err := model.GetTokenByIds(tokenId, userId)
  83. if err != nil {
  84. c.JSON(http.StatusOK, gin.H{
  85. "success": false,
  86. "message": err.Error(),
  87. })
  88. return
  89. }
  90. expiredAt := token.ExpiredTime
  91. if expiredAt == -1 {
  92. expiredAt = 0
  93. }
  94. c.JSON(http.StatusOK, gin.H{
  95. "object": "credit_summary",
  96. "total_granted": token.RemainQuota,
  97. "total_used": 0, // not supported currently
  98. "total_available": token.RemainQuota,
  99. "expires_at": expiredAt * 1000,
  100. })
  101. }
  102. func AddToken(c *gin.Context) {
  103. token := model.Token{}
  104. err := c.ShouldBindJSON(&token)
  105. if err != nil {
  106. c.JSON(http.StatusOK, gin.H{
  107. "success": false,
  108. "message": err.Error(),
  109. })
  110. return
  111. }
  112. if len(token.Name) > 30 {
  113. c.JSON(http.StatusOK, gin.H{
  114. "success": false,
  115. "message": "令牌名称过长",
  116. })
  117. return
  118. }
  119. cleanToken := model.Token{
  120. UserId: c.GetInt("id"),
  121. Name: token.Name,
  122. Key: common.GenerateKey(),
  123. CreatedTime: common.GetTimestamp(),
  124. AccessedTime: common.GetTimestamp(),
  125. ExpiredTime: token.ExpiredTime,
  126. RemainQuota: token.RemainQuota,
  127. UnlimitedQuota: token.UnlimitedQuota,
  128. }
  129. err = cleanToken.Insert()
  130. if err != nil {
  131. c.JSON(http.StatusOK, gin.H{
  132. "success": false,
  133. "message": err.Error(),
  134. })
  135. return
  136. }
  137. c.JSON(http.StatusOK, gin.H{
  138. "success": true,
  139. "message": "",
  140. })
  141. return
  142. }
  143. func DeleteToken(c *gin.Context) {
  144. id, _ := strconv.Atoi(c.Param("id"))
  145. userId := c.GetInt("id")
  146. err := model.DeleteTokenById(id, userId)
  147. if err != nil {
  148. c.JSON(http.StatusOK, gin.H{
  149. "success": false,
  150. "message": err.Error(),
  151. })
  152. return
  153. }
  154. c.JSON(http.StatusOK, gin.H{
  155. "success": true,
  156. "message": "",
  157. })
  158. return
  159. }
  160. func UpdateToken(c *gin.Context) {
  161. userId := c.GetInt("id")
  162. statusOnly := c.Query("status_only")
  163. token := model.Token{}
  164. err := c.ShouldBindJSON(&token)
  165. if err != nil {
  166. c.JSON(http.StatusOK, gin.H{
  167. "success": false,
  168. "message": err.Error(),
  169. })
  170. return
  171. }
  172. if len(token.Name) > 30 {
  173. c.JSON(http.StatusOK, gin.H{
  174. "success": false,
  175. "message": "令牌名称过长",
  176. })
  177. return
  178. }
  179. cleanToken, err := model.GetTokenByIds(token.Id, userId)
  180. if err != nil {
  181. c.JSON(http.StatusOK, gin.H{
  182. "success": false,
  183. "message": err.Error(),
  184. })
  185. return
  186. }
  187. if token.Status == common.TokenStatusEnabled {
  188. if cleanToken.Status == common.TokenStatusExpired && cleanToken.ExpiredTime <= common.GetTimestamp() && cleanToken.ExpiredTime != -1 {
  189. c.JSON(http.StatusOK, gin.H{
  190. "success": false,
  191. "message": "令牌已过期,无法启用,请先修改令牌过期时间,或者设置为永不过期",
  192. })
  193. return
  194. }
  195. if cleanToken.Status == common.TokenStatusExhausted && cleanToken.RemainQuota <= 0 && !cleanToken.UnlimitedQuota {
  196. c.JSON(http.StatusOK, gin.H{
  197. "success": false,
  198. "message": "令牌可用额度已用尽,无法启用,请先修改令牌剩余额度,或者设置为无限额度",
  199. })
  200. return
  201. }
  202. }
  203. if statusOnly != "" {
  204. cleanToken.Status = token.Status
  205. } else {
  206. // If you add more fields, please also update token.Update()
  207. cleanToken.Name = token.Name
  208. cleanToken.ExpiredTime = token.ExpiredTime
  209. cleanToken.RemainQuota = token.RemainQuota
  210. cleanToken.UnlimitedQuota = token.UnlimitedQuota
  211. }
  212. err = cleanToken.Update()
  213. if err != nil {
  214. c.JSON(http.StatusOK, gin.H{
  215. "success": false,
  216. "message": err.Error(),
  217. })
  218. return
  219. }
  220. c.JSON(http.StatusOK, gin.H{
  221. "success": true,
  222. "message": "",
  223. "data": cleanToken,
  224. })
  225. return
  226. }