token.go 5.2 KB

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