|
- package controller
-
- import (
- "strconv"
-
- "github.com/QuantumNous/new-api/common"
- "github.com/QuantumNous/new-api/model"
- "github.com/gin-gonic/gin"
- )
-
- // GetUserRateLimits GET /api/user/:id/rate-limits
- // 查询指定用户的所有模型 RPM 限制配置(管理员权限)
- func GetUserRateLimits(c *gin.Context) {
- userId, err := strconv.Atoi(c.Param("id"))
- if err != nil {
- common.ApiErrorMsg(c, "invalid user id")
- return
- }
-
- list, err := model.GetUserModelRateLimits(userId)
- if err != nil {
- common.ApiError(c, err)
- return
- }
-
- // 保证返回空列表而非 null
- if list == nil {
- list = []model.UserModelRateLimit{}
- }
-
- c.JSON(200, gin.H{
- "success": true,
- "message": "",
- "data": list,
- })
- }
-
- // SetUserRateLimits PUT /api/user/:id/rate-limits
- // 覆盖式写入指定用户的所有模型 RPM 限制配置(管理员权限)
- func SetUserRateLimits(c *gin.Context) {
- userId, err := strconv.Atoi(c.Param("id"))
- if err != nil {
- common.ApiErrorMsg(c, "invalid user id")
- return
- }
-
- var items []model.UserModelRateLimit
- if err := c.ShouldBindJSON(&items); err != nil {
- common.ApiErrorMsg(c, err.Error())
- return
- }
-
- for _, item := range items {
- if item.Rpm < 0 {
- common.ApiErrorMsg(c, "rpm must be >= 0")
- return
- }
- }
-
- if err := model.SetUserModelRateLimits(userId, items); err != nil {
- common.ApiError(c, err)
- return
- }
-
- // 删除 Redis 缓存
- if common.RedisEnabled {
- _ = common.RedisDel("user_model_rate_limit:" + strconv.Itoa(userId))
- }
-
- c.JSON(200, gin.H{
- "success": true,
- "message": "",
- })
- }
|