Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

75 lignes
1.5 KiB

  1. package controller
  2. import (
  3. "strconv"
  4. "github.com/QuantumNous/new-api/common"
  5. "github.com/QuantumNous/new-api/model"
  6. "github.com/gin-gonic/gin"
  7. )
  8. // GetUserRateLimits GET /api/user/:id/rate-limits
  9. // 查询指定用户的所有模型 RPM 限制配置(管理员权限)
  10. func GetUserRateLimits(c *gin.Context) {
  11. userId, err := strconv.Atoi(c.Param("id"))
  12. if err != nil {
  13. common.ApiErrorMsg(c, "invalid user id")
  14. return
  15. }
  16. list, err := model.GetUserModelRateLimits(userId)
  17. if err != nil {
  18. common.ApiError(c, err)
  19. return
  20. }
  21. // 保证返回空列表而非 null
  22. if list == nil {
  23. list = []model.UserModelRateLimit{}
  24. }
  25. c.JSON(200, gin.H{
  26. "success": true,
  27. "message": "",
  28. "data": list,
  29. })
  30. }
  31. // SetUserRateLimits PUT /api/user/:id/rate-limits
  32. // 覆盖式写入指定用户的所有模型 RPM 限制配置(管理员权限)
  33. func SetUserRateLimits(c *gin.Context) {
  34. userId, err := strconv.Atoi(c.Param("id"))
  35. if err != nil {
  36. common.ApiErrorMsg(c, "invalid user id")
  37. return
  38. }
  39. var items []model.UserModelRateLimit
  40. if err := c.ShouldBindJSON(&items); err != nil {
  41. common.ApiErrorMsg(c, err.Error())
  42. return
  43. }
  44. for _, item := range items {
  45. if item.Rpm < 0 {
  46. common.ApiErrorMsg(c, "rpm must be >= 0")
  47. return
  48. }
  49. }
  50. if err := model.SetUserModelRateLimits(userId, items); err != nil {
  51. common.ApiError(c, err)
  52. return
  53. }
  54. // 删除 Redis 缓存
  55. if common.RedisEnabled {
  56. _ = common.RedisDel("user_model_rate_limit:" + strconv.Itoa(userId))
  57. }
  58. c.JSON(200, gin.H{
  59. "success": true,
  60. "message": "",
  61. })
  62. }