feat: 中间件多语言

This commit is contained in:
TsMask
2023-11-20 18:56:02 +08:00
parent 5604bd9b9d
commit 80978c96f0
3 changed files with 14 additions and 8 deletions

View File

@@ -6,6 +6,7 @@ import (
"time"
"ems.agt/src/framework/constants/cachekey"
"ems.agt/src/framework/i18n"
"ems.agt/src/framework/redis"
"ems.agt/src/framework/utils/ctx"
"ems.agt/src/framework/utils/ip2region"
@@ -42,6 +43,8 @@ type LimitOption struct {
// 以便获取登录用户信息,无用户信息时默认为 GLOBAL
func RateLimit(option LimitOption) gin.HandlerFunc {
return func(c *gin.Context) {
language := ctx.AcceptLanguage(c)
// 初始可选参数数据
if option.Time < 5 {
option.Time = 5
@@ -66,7 +69,7 @@ func RateLimit(option LimitOption) gin.HandlerFunc {
if err != nil {
c.JSON(401, result.Err(map[string]any{
"code": 401,
"msg": err.Error(),
"msg": i18n.TKey(language, err.Error()),
}))
c.Abort() // 停止执行后续的处理函数
return
@@ -91,7 +94,7 @@ func RateLimit(option LimitOption) gin.HandlerFunc {
if rateCount >= option.Count {
// 访问过于频繁,请稍候再试
c.JSON(200, result.ErrMsg("Visits are too frequent. Please try again later"))
c.JSON(200, i18n.TKey(language, "app.common.rateLimitTip"))
c.Abort() // 停止执行后续的处理函数
return
}