评论表单校验
This commit is contained in:
parent
72bad0fa36
commit
345cdcd4e0
|
@ -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,8 +39,18 @@ func PostComment(c *gin.Context) {
|
|||
if err != nil {
|
||||
c.Writer.WriteHeader(http.StatusConflict)
|
||||
c.Writer.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
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()
|
||||
if err != nil {
|
||||
|
@ -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 = `
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
<meta name="viewport" content="width=device-width">
|
||||
<meta name='robots' content='max-image-preview:large, noindex, follow' />
|
||||
<title>评论提交失败</title>
|
||||
<style type="text/css">
|
||||
html {
|
||||
background: #f1f1f1;
|
||||
}
|
||||
body {
|
||||
background: #fff;
|
||||
border: 1px solid #ccd0d4;
|
||||
color: #444;
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
|
||||
margin: 2em auto;
|
||||
padding: 1em 2em;
|
||||
max-width: 700px;
|
||||
-webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, .04);
|
||||
box-shadow: 0 1px 1px rgba(0, 0, 0, .04);
|
||||
}
|
||||
h1 {
|
||||
border-bottom: 1px solid #dadada;
|
||||
clear: both;
|
||||
color: #666;
|
||||
font-size: 24px;
|
||||
margin: 30px 0 0 0;
|
||||
padding: 0;
|
||||
padding-bottom: 7px;
|
||||
}
|
||||
#error-page {
|
||||
margin-top: 50px;
|
||||
}
|
||||
#error-page p,
|
||||
#error-page .wp-die-message {
|
||||
font-size: 14px;
|
||||
line-height: 1.5;
|
||||
margin: 25px 0 20px;
|
||||
}
|
||||
#error-page code {
|
||||
font-family: Consolas, Monaco, monospace;
|
||||
}
|
||||
ul li {
|
||||
margin-bottom: 10px;
|
||||
font-size: 14px ;
|
||||
}
|
||||
a {
|
||||
color: #0073aa;
|
||||
}
|
||||
a:hover,
|
||||
a:active {
|
||||
color: #006799;
|
||||
}
|
||||
a:focus {
|
||||
color: #124964;
|
||||
-webkit-box-shadow:
|
||||
0 0 0 1px #5b9dd9,
|
||||
0 0 2px 1px rgba(30, 140, 190, 0.8);
|
||||
box-shadow:
|
||||
0 0 0 1px #5b9dd9,
|
||||
0 0 2px 1px rgba(30, 140, 190, 0.8);
|
||||
outline: none;
|
||||
}
|
||||
.button {
|
||||
background: #f3f5f6;
|
||||
border: 1px solid #016087;
|
||||
color: #016087;
|
||||
display: inline-block;
|
||||
text-decoration: none;
|
||||
font-size: 13px;
|
||||
line-height: 2;
|
||||
height: 28px;
|
||||
margin: 0;
|
||||
padding: 0 10px 1px;
|
||||
cursor: pointer;
|
||||
-webkit-border-radius: 3px;
|
||||
-webkit-appearance: none;
|
||||
border-radius: 3px;
|
||||
white-space: nowrap;
|
||||
-webkit-box-sizing: border-box;
|
||||
-moz-box-sizing: border-box;
|
||||
box-sizing: border-box;
|
||||
|
||||
vertical-align: top;
|
||||
}
|
||||
|
||||
.button.button-large {
|
||||
line-height: 2.30769231;
|
||||
min-height: 32px;
|
||||
padding: 0 12px;
|
||||
}
|
||||
|
||||
.button:hover,
|
||||
.button:focus {
|
||||
background: #f1f1f1;
|
||||
}
|
||||
|
||||
.button:focus {
|
||||
background: #f3f5f6;
|
||||
border-color: #007cba;
|
||||
-webkit-box-shadow: 0 0 0 1px #007cba;
|
||||
box-shadow: 0 0 0 1px #007cba;
|
||||
color: #016087;
|
||||
outline: 2px solid transparent;
|
||||
outline-offset: 0;
|
||||
}
|
||||
|
||||
.button:active {
|
||||
background: #f3f5f6;
|
||||
border-color: #7e8993;
|
||||
-webkit-box-shadow: none;
|
||||
box-shadow: none;
|
||||
}
|
||||
|
||||
</style>
|
||||
</head>
|
||||
<body id="error-page">
|
||||
<div class="wp-die-message"><p><strong>错误:</strong>%s</p></div>
|
||||
<p><a href='javascript:history.back()'>« 返回</a></p></body>
|
||||
</html>
|
||||
|
||||
`
|
||||
|
|
|
@ -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
|
||||
|
|
43
internal/pkg/config/validationtraslaor.go
Normal file
43
internal/pkg/config/validationtraslaor.go
Normal file
|
@ -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
|
||||
}
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user