first commit
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()
|
||||
}
|
||||
}
|
||||
40
internal/pkg/gin/gu/response.go
Normal file
40
internal/pkg/gin/gu/response.go
Normal file
@@ -0,0 +1,40 @@
|
||||
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 int32 `json:"page_id"`
|
||||
PageSize int32 `json:"page_size"`
|
||||
Result any `json:"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,
|
||||
})
|
||||
}
|
||||
57
internal/pkg/gin/gu/validator.go
Normal file
57
internal/pkg/gin/gu/validator.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package gu
|
||||
|
||||
import (
|
||||
"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
|
||||
|
||||
// loca 通常取决于 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) {
|
||||
if errors, ok := err.(validator.ValidationErrors); ok {
|
||||
errs := gin.H{}
|
||||
for _, e := range errors {
|
||||
errs[e.StructField()] = strings.Replace(e.Translate(trans), e.StructField(), "", -1)
|
||||
}
|
||||
ctx.JSON(http.StatusBadRequest, errs)
|
||||
} else {
|
||||
ctx.JSON(http.StatusBadRequest, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user