token_search.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package controller
  2. import (
  3. "net/http"
  4. "one-api/common"
  5. "one-api/model"
  6. "one-api/setting/ratio_setting"
  7. "strings"
  8. "github.com/gin-gonic/gin"
  9. )
  10. type TokenSearchRequest struct {
  11. Token string `json:"token" binding:"required"`
  12. }
  13. type TokenInfoResponse struct {
  14. TokenName string `json:"token_name"`
  15. RemainQuota int `json:"remain_quota"`
  16. UsedQuota int `json:"used_quota"`
  17. UnlimitedQuota bool `json:"unlimited_quota"`
  18. ExpiredTime int64 `json:"expired_time"`
  19. Status int `json:"status"`
  20. ModelRatio float64 `json:"model_ratio"`
  21. }
  22. func SearchTokenByToken(c *gin.Context) {
  23. var req TokenSearchRequest
  24. if err := c.ShouldBindJSON(&req); err != nil {
  25. common.ApiError(c, err)
  26. return
  27. }
  28. // 去掉前缀"sk-"后再查询数据库
  29. tokenKey := strings.TrimPrefix(req.Token, "sk-")
  30. // 获取token信息
  31. token, err := model.GetTokenByKey(tokenKey, false)
  32. if err != nil {
  33. common.ApiError(c, err)
  34. return
  35. }
  36. // 获取配比关系,默认使用gpt-3.5-turbo的配比
  37. modelRatio, _, _ := ratio_setting.GetModelRatio("gpt-3.5-turbo")
  38. // 构造返回信息
  39. response := TokenInfoResponse{
  40. TokenName: token.Name,
  41. RemainQuota: token.RemainQuota,
  42. UsedQuota: token.UsedQuota,
  43. UnlimitedQuota: token.UnlimitedQuota,
  44. ExpiredTime: token.ExpiredTime,
  45. Status: token.Status,
  46. ModelRatio: modelRatio,
  47. }
  48. c.JSON(http.StatusOK, gin.H{
  49. "success": true,
  50. "message": "",
  51. "data": response,
  52. })
  53. }