| package app |
|
|
| import ( |
| "fmt" |
| "net/http" |
| "strconv" |
| "time" |
|
|
| "github.com/gin-gonic/gin" |
| ) |
|
|
| |
| |
|
|
| |
| func (s *Server) HandleSetChannelCooldown(c *gin.Context) { |
| id, err := ParseInt64Param(c, "id") |
| if err != nil { |
| RespondErrorMsg(c, http.StatusBadRequest, "invalid channel ID") |
| return |
| } |
|
|
| var req CooldownRequest |
| if err := c.ShouldBindJSON(&req); err != nil { |
| RespondError(c, http.StatusBadRequest, err) |
| return |
| } |
|
|
| until := time.Now().Add(time.Duration(req.DurationMs) * time.Millisecond) |
| err = s.store.SetChannelCooldown(c.Request.Context(), id, until) |
| if err != nil { |
| RespondError(c, http.StatusInternalServerError, err) |
| return |
| } |
|
|
| |
|
|
| RespondJSON(c, http.StatusOK, gin.H{"message": fmt.Sprintf("渠道已冷却 %d 毫秒", req.DurationMs)}) |
| } |
|
|
| |
| func (s *Server) HandleSetKeyCooldown(c *gin.Context) { |
| id, err := ParseInt64Param(c, "id") |
| if err != nil { |
| RespondErrorMsg(c, http.StatusBadRequest, "invalid channel ID") |
| return |
| } |
|
|
| keyIndexStr := c.Param("keyIndex") |
| keyIndex, err := strconv.Atoi(keyIndexStr) |
| if err != nil || keyIndex < 0 { |
| RespondErrorMsg(c, http.StatusBadRequest, "invalid key index") |
| return |
| } |
|
|
| var req CooldownRequest |
| if err := c.ShouldBindJSON(&req); err != nil { |
| RespondError(c, http.StatusBadRequest, err) |
| return |
| } |
|
|
| until := time.Now().Add(time.Duration(req.DurationMs) * time.Millisecond) |
| err = s.store.SetKeyCooldown(c.Request.Context(), id, keyIndex, until) |
| if err != nil { |
| RespondError(c, http.StatusInternalServerError, err) |
| return |
| } |
|
|
| |
| s.InvalidateAPIKeysCache(id) |
|
|
| RespondJSON(c, http.StatusOK, gin.H{"message": fmt.Sprintf("Key #%d 已冷却 %d 毫秒", keyIndex+1, req.DurationMs)}) |
| } |
|
|