token.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. isAdmin := c.GetInt("role") >= common.RoleAdminUser
  98. token := model.Token{}
  99. err := c.ShouldBindJSON(&token)
  100. if err != nil {
  101. c.JSON(http.StatusOK, gin.H{
  102. "success": false,
  103. "message": err.Error(),
  104. })
  105. return
  106. }
  107. if len(token.Name) == 0 || len(token.Name) > 20 {
  108. c.JSON(http.StatusOK, gin.H{
  109. "success": false,
  110. "message": "令牌名称长度必须在1-20之间",
  111. })
  112. return
  113. }
  114. cleanToken := model.Token{
  115. UserId: c.GetInt("id"),
  116. Name: token.Name,
  117. Key: common.GetUUID(),
  118. CreatedTime: common.GetTimestamp(),
  119. AccessedTime: common.GetTimestamp(),
  120. ExpiredTime: token.ExpiredTime,
  121. }
  122. if isAdmin {
  123. cleanToken.RemainQuota = token.RemainQuota
  124. cleanToken.UnlimitedQuota = token.UnlimitedQuota
  125. } else {
  126. userId := c.GetInt("id")
  127. quota, err := model.GetUserQuota(userId)
  128. if err != nil {
  129. c.JSON(http.StatusOK, gin.H{
  130. "success": false,
  131. "message": err.Error(),
  132. })
  133. return
  134. }
  135. if quota > 0 {
  136. cleanToken.RemainQuota = quota
  137. }
  138. }
  139. err = cleanToken.Insert()
  140. if err != nil {
  141. c.JSON(http.StatusOK, gin.H{
  142. "success": false,
  143. "message": err.Error(),
  144. })
  145. return
  146. }
  147. if !isAdmin {
  148. // update user quota
  149. err = model.DecreaseUserQuota(c.GetInt("id"), cleanToken.RemainQuota)
  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. isAdmin := c.GetInt("role") >= common.RoleAdminUser
  176. userId := c.GetInt("id")
  177. statusOnly := c.Query("status_only")
  178. token := model.Token{}
  179. err := c.ShouldBindJSON(&token)
  180. if err != nil {
  181. c.JSON(http.StatusOK, gin.H{
  182. "success": false,
  183. "message": err.Error(),
  184. })
  185. return
  186. }
  187. cleanToken, err := model.GetTokenByIds(token.Id, userId)
  188. if err != nil {
  189. c.JSON(http.StatusOK, gin.H{
  190. "success": false,
  191. "message": err.Error(),
  192. })
  193. return
  194. }
  195. if token.Status == common.TokenStatusEnabled {
  196. if cleanToken.Status == common.TokenStatusExpired && cleanToken.ExpiredTime <= common.GetTimestamp() {
  197. c.JSON(http.StatusOK, gin.H{
  198. "success": false,
  199. "message": "令牌已过期,无法启用,请先修改令牌过期时间",
  200. })
  201. return
  202. }
  203. if cleanToken.Status == common.TokenStatusExhausted && cleanToken.RemainQuota <= 0 && !cleanToken.UnlimitedQuota {
  204. c.JSON(http.StatusOK, gin.H{
  205. "success": false,
  206. "message": "令牌可用次数已用尽,无法启用,请先修改令牌剩余次数,或者设置为无限次数",
  207. })
  208. return
  209. }
  210. }
  211. if statusOnly != "" {
  212. cleanToken.Status = token.Status
  213. } else {
  214. // If you add more fields, please also update token.Update()
  215. cleanToken.Name = token.Name
  216. cleanToken.ExpiredTime = token.ExpiredTime
  217. if isAdmin {
  218. cleanToken.RemainQuota = token.RemainQuota
  219. cleanToken.UnlimitedQuota = token.UnlimitedQuota
  220. }
  221. }
  222. err = cleanToken.Update()
  223. if err != nil {
  224. c.JSON(http.StatusOK, gin.H{
  225. "success": false,
  226. "message": err.Error(),
  227. })
  228. return
  229. }
  230. c.JSON(http.StatusOK, gin.H{
  231. "success": true,
  232. "message": "",
  233. "data": cleanToken,
  234. })
  235. return
  236. }
  237. type topUpRequest struct {
  238. Id int `json:"id"`
  239. Key string `json:"key"`
  240. }
  241. func TopUp(c *gin.Context) {
  242. req := topUpRequest{}
  243. err := c.ShouldBindJSON(&req)
  244. if err != nil {
  245. c.JSON(http.StatusOK, gin.H{
  246. "success": false,
  247. "message": err.Error(),
  248. })
  249. return
  250. }
  251. quota, err := model.Redeem(req.Key, req.Id)
  252. if err != nil {
  253. c.JSON(http.StatusOK, gin.H{
  254. "success": false,
  255. "message": err.Error(),
  256. })
  257. return
  258. }
  259. c.JSON(http.StatusOK, gin.H{
  260. "success": true,
  261. "message": "",
  262. "data": quota,
  263. })
  264. return
  265. }