v1
This commit is contained in:
26
internal/pkg/gin/gu/cors.go
Normal file
26
internal/pkg/gin/gu/cors.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package gu
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Cors() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
method := c.Request.Method
|
||||
|
||||
c.Header("Access-Control-Allow-Origin", "*")
|
||||
c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token")
|
||||
c.Header("Access-Control-Allow-Methods", "GET,POST,PUT,PATCH,DELETE,OPTIONS")
|
||||
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
|
||||
c.Header("Access-Control-Allow-Credentials", "true")
|
||||
|
||||
//放行所有OPTIONS方法
|
||||
if method == "OPTIONS" {
|
||||
c.AbortWithStatus(http.StatusNoContent)
|
||||
}
|
||||
// 处理请求
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
49
internal/pkg/gin/gu/response.go
Normal file
49
internal/pkg/gin/gu/response.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package gu
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type response struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data any `json:"data"`
|
||||
}
|
||||
|
||||
type PageData struct {
|
||||
Total int64 `json:"total"`
|
||||
PageID int `json:"page_id"`
|
||||
PageSize int `json:"page_size"`
|
||||
Result any `json:"result"`
|
||||
}
|
||||
|
||||
func NewPageData(total int64, pageID, pageSize int, result any) PageData {
|
||||
return PageData{
|
||||
Total: total,
|
||||
PageID: pageID,
|
||||
PageSize: pageSize,
|
||||
Result: result,
|
||||
}
|
||||
}
|
||||
|
||||
func Ok(ctx *gin.Context, data any) {
|
||||
ResponseJson(ctx, http.StatusOK, "ok", data)
|
||||
}
|
||||
|
||||
func Failed(ctx *gin.Context, message string) {
|
||||
ResponseJson(ctx, http.StatusInternalServerError, message, nil)
|
||||
}
|
||||
|
||||
func FailedWithCode(ctx *gin.Context, code int, message string) {
|
||||
ResponseJson(ctx, code, message, nil)
|
||||
}
|
||||
|
||||
func ResponseJson(ctx *gin.Context, code int, message string, data any) {
|
||||
ctx.JSON(code, response{
|
||||
Code: code,
|
||||
Message: message,
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
58
internal/pkg/gin/gu/validator.go
Normal file
58
internal/pkg/gin/gu/validator.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package gu
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
"github.com/go-playground/locales/en"
|
||||
"github.com/go-playground/locales/zh"
|
||||
ut "github.com/go-playground/universal-translator"
|
||||
"github.com/go-playground/validator/v10"
|
||||
enTranslations "github.com/go-playground/validator/v10/translations/en"
|
||||
chTranslations "github.com/go-playground/validator/v10/translations/zh"
|
||||
)
|
||||
|
||||
var trans ut.Translator
|
||||
|
||||
// SetValidatorTrans
|
||||
// local 通常取决于 http 请求头的 'Accept-Language'
|
||||
func SetValidatorTrans(local string) (err error) {
|
||||
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
|
||||
zhT := zh.New() //chinese
|
||||
enT := en.New() //english
|
||||
uni := ut.New(enT, zhT, enT)
|
||||
|
||||
var o bool
|
||||
trans, o = uni.GetTranslator(local)
|
||||
if !o {
|
||||
return fmt.Errorf("uni.GetTranslator(%s) failed", local)
|
||||
}
|
||||
//register translate
|
||||
// 注册翻译器
|
||||
switch local {
|
||||
case "en":
|
||||
err = enTranslations.RegisterDefaultTranslations(v, trans)
|
||||
case "zh":
|
||||
err = chTranslations.RegisterDefaultTranslations(v, trans)
|
||||
default:
|
||||
err = enTranslations.RegisterDefaultTranslations(v, trans)
|
||||
}
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ValidatorErrors(ctx *gin.Context, err error) {
|
||||
var errs validator.ValidationErrors
|
||||
if errors.As(err, &errs) {
|
||||
es := gin.H{}
|
||||
for _, e := range errs {
|
||||
es[e.StructField()] = strings.Replace(e.Translate(trans), e.StructField(), "", -1)
|
||||
}
|
||||
ctx.JSON(http.StatusBadRequest, es)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user