token.go 5.6 KB

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