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 = ` + + + +
+ + + +