This commit is contained in:
0xJacky 2021-10-18 23:13:47 +08:00
parent 953f943e01
commit 27c4b82d54
22 changed files with 97 additions and 85 deletions

View file

@ -1,15 +1,42 @@
package api
import (
"github.com/gin-gonic/gin"
"log"
"bytes"
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/locales/zh"
"log"
"net/http"
"regexp"
"strings"
ut "github.com/go-playground/universal-translator"
val "github.com/go-playground/validator/v10"
val "github.com/go-playground/validator/v10"
zhTranslations "github.com/go-playground/validator/v10/translations/zh"
)
type JsonSnakeCase struct {
Value interface{}
}
func (c JsonSnakeCase) MarshalJSON() ([]byte, error) {
// Regexp definitions
var keyMatchRegex = regexp.MustCompile(`\"(\w+)\":`)
var wordBarrierRegex = regexp.MustCompile(`(\w)([A-Z])`)
marshalled, err := json.Marshal(c.Value)
converted := keyMatchRegex.ReplaceAllFunc(
marshalled,
func(match []byte) []byte {
return bytes.ToLower(wordBarrierRegex.ReplaceAll(
match,
[]byte(`${1}_${2}`),
))
},
)
return converted, err
}
func ErrorHandler(c *gin.Context, err error) {
log.Println(err)
c.JSON(http.StatusInternalServerError, gin.H{
@ -22,29 +49,41 @@ type ValidError struct {
Message string
}
type ValidErrors gin.H
func BindAndValid(c *gin.Context, v interface{}) (bool, ValidErrors) {
errs := make(ValidErrors)
err := c.ShouldBind(v)
if err != nil {
func BindAndValid(c *gin.Context, v interface{}) bool {
errs := make(map[string]string)
err := c.ShouldBindJSON(v)
if err != nil {
log.Println(err)
v := c.Value("trans")
trans, _ := v.(ut.Translator)
uni := ut.New(zh.New())
trans, _ := uni.GetTranslator("zh")
v, ok := binding.Validator.Engine().(*val.Validate)
verrs, ok := err.(val.ValidationErrors)
if !ok {
return false, errs
}
if ok {
_ = zhTranslations.RegisterDefaultTranslations(v, trans)
}
for key, value := range verrs.Translate(trans) {
k := strings.Split(key, ".")
sub := strings.ToLower(k[1])
errs[sub] = value
}
verrs, ok := err.(val.ValidationErrors)
if !ok {
log.Println(verrs)
c.JSON(http.StatusNotAcceptable, gin.H{
"message": "请求参数错误",
"code": http.StatusNotAcceptable,
})
return false
}
return false, errs
}
for key, value := range verrs.Translate(trans) {
errs[key[strings.Index(key, ".")+1:]] = value
}
return true, nil
c.JSON(http.StatusNotAcceptable, gin.H{
"errors": JsonSnakeCase{errs},
"message": "请求参数错误",
"code": http.StatusNotAcceptable,
})
return false
}
return true
}

View file

@ -15,11 +15,8 @@ type LoginUser struct {
func Login(c *gin.Context) {
var user LoginUser
ok, verrs := BindAndValid(c, &user)
ok := BindAndValid(c, &user)
if !ok {
c.JSON(http.StatusNotAcceptable, gin.H{
"errors": verrs,
})
return
}

View file

@ -15,30 +15,6 @@ import (
func CertInfo(c *gin.Context) {
domain := c.Param("domain")
/*sslCertificatePath := tool.GetNginxConfPath("ssl/" + domain +"/fullchain.cer")
content, err := ioutil.ReadFile(sslCertificatePath)
if err != nil {
ErrorHandler(c, err)
return
}
certDERBlock, _ := pem.Decode(content)
if certDERBlock == nil {
ErrorHandler(c, errors.New("pem decode error"))
return
}
var key *x509.Certificate
key, err = x509.ParseCertificate(certDERBlock.Bytes)
if err != nil {
ErrorHandler(c, err)
return
}*/
ts := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}

View file

@ -40,11 +40,8 @@ func InstallNginxUI(c *gin.Context) {
return
}
var json InstallJson
ok, verrs := BindAndValid(c, &json)
ok := BindAndValid(c, &json)
if !ok {
c.JSON(http.StatusNotAcceptable, gin.H{
"errors": verrs,
})
return
}

View file

@ -45,11 +45,8 @@ type UserJson struct {
func AddUser(c *gin.Context) {
var json UserJson
ok, verrs := BindAndValid(c, &json)
ok := BindAndValid(c, &json)
if !ok {
c.JSON(http.StatusNotAcceptable, gin.H{
"errors": verrs,
})
return
}
curd := model.NewCurd(&model.Auth{})
@ -79,11 +76,9 @@ func AddUser(c *gin.Context) {
func EditUser(c *gin.Context) {
var json UserJson
ok, verrs := BindAndValid(c, &json)
ok := BindAndValid(c, &json)
if !ok {
c.JSON(http.StatusNotAcceptable, gin.H{
"errors": verrs,
})
return
}
curd := model.NewCurd(&model.Auth{})