diff --git a/internal/actions/comment.go b/internal/actions/comment.go index 1cf7b33..c9c4a41 100644 --- a/internal/actions/comment.go +++ b/internal/actions/comment.go @@ -6,13 +6,13 @@ import ( "errors" "fmt" "github.com/fthvgb1/wp-go/helper/slice" - str "github.com/fthvgb1/wp-go/helper/strings" "github.com/fthvgb1/wp-go/internal/mail" "github.com/fthvgb1/wp-go/internal/pkg/cache" "github.com/fthvgb1/wp-go/internal/pkg/config" "github.com/fthvgb1/wp-go/internal/pkg/logs" "github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/gin-gonic/gin" + "github.com/go-playground/validator/v10" "io" "net/http" "net/url" @@ -20,6 +20,13 @@ import ( "time" ) +type CommentForm struct { + CommentPostId uint64 `form:"comment_post_ID" binding:"required" json:"comment_post_ID"` + Author string `form:"author" binding:"required" label:"显示名称" json:"author"` + Email string `form:"email" binding:"required,email"` + Comment string `form:"comment" binding:"required" label:"评论" json:"comment"` +} + func PostComment(c *gin.Context) { cli := &http.Client{ Timeout: time.Second * 3, @@ -32,7 +39,17 @@ func PostComment(c *gin.Context) { if err != nil { c.Writer.WriteHeader(http.StatusConflict) c.Writer.Header().Set("Content-Type", "text/html; charset=utf-8") - c.Writer.WriteString("评论出错,请联系管理员或稍后再度") + var v validator.ValidationErrors + if errors.As(err, &v) { + e := v.Translate(config.GetZh()) + for _, v := range e { + fmt.Fprintf(c.Writer, fail, v) + return + } + } else { + c.Writer.WriteString("评论出错,请联系管理员或稍后再度") + } + } }() conf := config.GetConfig() @@ -41,10 +58,10 @@ func PostComment(c *gin.Context) { return } c.Request.Body = io.NopCloser(bytes.NewBuffer(data)) - i := c.PostForm("comment_post_ID") - author := c.PostForm("author") - m := c.PostForm("email") - comment := c.PostForm("comment") + var comment CommentForm + if err = c.ShouldBind(&comment); err != nil { + return + } c.Request.Body = io.NopCloser(bytes.NewBuffer(data)) req, err := http.NewRequest("POST", conf.PostCommentUrl, strings.NewReader(c.Request.PostForm.Encode())) if err != nil { @@ -93,9 +110,9 @@ func PostComment(c *gin.Context) { } cc := c.Copy() go func() { - id := str.ToInteger[uint64](i, 0) + id := comment.CommentPostId if id <= 0 { - logs.Error(errors.New("获取文档id错误"), "", i) + logs.Error(errors.New("获取文档id错误"), "", comment.CommentPostId) return } post, err := cache.GetPostById(cc, id) @@ -103,8 +120,8 @@ func PostComment(c *gin.Context) { logs.Error(err, "获取文档错误", id) return } - su := fmt.Sprintf("%s: %s[%s]发表了评论对文档[%v]的评论", wpconfig.GetOption("siteurl"), author, m, post.PostTitle) - err = mail.SendMail([]string{conf.Mail.User}, su, comment) + su := fmt.Sprintf("%s: %s[%s]发表了评论对文档[%v]的评论", wpconfig.GetOption("siteurl"), comment.Author, comment.Email, post.PostTitle) + err = mail.SendMail([]string{conf.Mail.User}, su, comment.Comment) logs.IfError(err, "发送邮件", conf.Mail.User, su, comment) }() @@ -137,3 +154,129 @@ func PostComment(c *gin.Context) { _, _ = c.Writer.Write(s) } + +var fail = ` + + + + + + + + 评论提交失败 + + + +

错误:%s

+

« 返回

+ + +` diff --git a/internal/cmd/main.go b/internal/cmd/main.go index d9d09b8..a3c79f6 100644 --- a/internal/cmd/main.go +++ b/internal/cmd/main.go @@ -53,6 +53,10 @@ func initConf(c string) (err error) { if err != nil { return } + err = config.InitTrans() + if err != nil { + return err + } err = logs.InitLogger() if err != nil { return err diff --git a/internal/pkg/config/validationtraslaor.go b/internal/pkg/config/validationtraslaor.go new file mode 100644 index 0000000..04c263e --- /dev/null +++ b/internal/pkg/config/validationtraslaor.go @@ -0,0 +1,43 @@ +package config + +import ( + "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" + enTrans "github.com/go-playground/validator/v10/translations/en" + zhTrans "github.com/go-playground/validator/v10/translations/zh" + "reflect" +) + +var enT ut.Translator +var zhT ut.Translator + +func GetZh() ut.Translator { + return zhT +} +func GetEn() ut.Translator { + return enT +} + +func InitTrans() error { + if validate, ok := binding.Validator.Engine().(*validator.Validate); ok { + ens := en.New() + uni := ut.New(ens, zh.New(), ens) + zhT, _ = uni.GetTranslator("zh") + enT, _ = uni.GetTranslator("en") + err := enTrans.RegisterDefaultTranslations(validate, enT) + if err != nil { + return err + } + validate.RegisterTagNameFunc(func(field reflect.StructField) string { + return field.Tag.Get("label") + }) + err = zhTrans.RegisterDefaultTranslations(validate, zhT) + if err != nil { + return err + } + } + return nil +} diff --git a/internal/theme/wp/pipe.go b/internal/theme/wp/pipe.go index a7cccf2..3c4d18d 100644 --- a/internal/theme/wp/pipe.go +++ b/internal/theme/wp/pipe.go @@ -65,7 +65,7 @@ func PipeHandle(pipeScene int, keyFn func(*Handle, int) string, fn func(*Handle, } func PipeKey(h *Handle, pipScene int) string { - return fmt.Sprintf("%d-%d-%d", pipScene, h.scene, h.scene) + return fmt.Sprintf("pipekey-%d-%d-%d", pipScene, h.scene, h.scene) } func PipeDataHandle(h *Handle, dataHandlers map[int][]HandleCall) (handlers []HandleCall) {