From 372d70969bc1f938b8ba7a44bfcbc931315ff0f2 Mon Sep 17 00:00:00 2001 From: icechen Date: Thu, 13 Jan 2022 02:23:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8F=82=E6=95=B0=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=EF=BC=8C=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/zeus/api/lark/internal/user/error.go | 27 +++++++++ app/zeus/api/lark/internal/user/user.go | 7 +-- app/zeus/api/lark/internal/user/validator.go | 61 ++++++++++++++++++++ 3 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 app/zeus/api/lark/internal/user/error.go create mode 100644 app/zeus/api/lark/internal/user/validator.go diff --git a/app/zeus/api/lark/internal/user/error.go b/app/zeus/api/lark/internal/user/error.go new file mode 100644 index 0000000..75c6e7d --- /dev/null +++ b/app/zeus/api/lark/internal/user/error.go @@ -0,0 +1,27 @@ +package user + +import "github.com/gofiber/fiber/v2" + +var ErrorUser = fiber.NewError(0, "错误") + +func NewError(msg string) error { + return fiber.NewError(fiber.StatusInternalServerError, msg) +} + +func NewErrorWithCode(code int, msg string) error { + return fiber.NewError(code, msg) +} + +func WarpError(err error) error { + if err == nil { + return nil + } + return fiber.NewError(fiber.StatusInternalServerError, err.Error()) +} + +func WarpErrorWithCode(code int, err error) error { + if err == nil { + return nil + } + return fiber.NewError(code, err.Error()) +} diff --git a/app/zeus/api/lark/internal/user/user.go b/app/zeus/api/lark/internal/user/user.go index 24cc681..55b5a1e 100644 --- a/app/zeus/api/lark/internal/user/user.go +++ b/app/zeus/api/lark/internal/user/user.go @@ -8,13 +8,12 @@ import ( "go.uber.org/zap" ) -var ErrorUser = fiber.NewError(0, "错误") - func LoginHandle(c *fiber.Ctx) error { var p = struct { - Code string `json:"code"` + Code string `json:"code" validate:"required"` + Email string `json:"email" validate:"email"` }{} - err := c.BodyParser(&p) + err := Bind(c, &p) if err != nil { return err } diff --git a/app/zeus/api/lark/internal/user/validator.go b/app/zeus/api/lark/internal/user/validator.go new file mode 100644 index 0000000..785d395 --- /dev/null +++ b/app/zeus/api/lark/internal/user/validator.go @@ -0,0 +1,61 @@ +package user + +import ( + "fmt" + "github.com/go-playground/locales/zh_Hans_CN" + ut "github.com/go-playground/universal-translator" + "github.com/go-playground/validator/v10" + zh_translations "github.com/go-playground/validator/v10/translations/zh" + "github.com/gofiber/fiber/v2" + "reflect" + "strings" +) + +var Validate *validator.Validate +var trans ut.Translator + +func init() { + Validate = validator.New() + zh := zh_Hans_CN.New() + uni := ut.New(zh, zh) + var ok bool + trans, ok = uni.GetTranslator("zh_Hans_CN") + if !ok { + panic(fmt.Errorf("uni.GetTranslator(%s) failed", "zh")) + } + Validate.RegisterTagNameFunc(func(field reflect.StructField) string { + label := field.Tag.Get("json") + if label == "" { + return field.Name + } + return label + }) + err := zh_translations.RegisterDefaultTranslations(Validate, trans) + if err != nil { + panic(err) + } +} + +func Bind(c *fiber.Ctx, out interface{}) error { + if reflect.TypeOf(out).Kind() != reflect.Ptr { + return NewError("bind must ptr") + } + + if err := c.BodyParser(out); err != nil { + return WarpError(err) + } + + err := Validate.Struct(out) + + if err != nil { + if errs, ok := err.(validator.ValidationErrors); ok { + errString := make([]string, 0, len(errs)) + for _, fieldError := range errs.Translate(trans) { + errString = append(errString, fieldError) + } + err = fmt.Errorf("%s", strings.Join(errString, ",")) + } + return WarpErrorWithCode(fiber.StatusBadRequest, err) + } + return nil +}