From 6b1fafb258e0e9bea6077572898646a2d1233856 Mon Sep 17 00:00:00 2001 From: "vilet.yy" Date: Tue, 6 Apr 2021 18:19:45 +0800 Subject: [PATCH] fix --- controller/api/v1/basic/vendor.go | 15 +++++++++---- controller/api/v1/user.go | 37 ++++++++++++++++++++++--------- docs/docs.go | 2 ++ docs/swagger.json | 2 ++ docs/swagger.yaml | 2 ++ global/global.go | 2 +- models/user.go | 4 ++-- 7 files changed, 46 insertions(+), 18 deletions(-) diff --git a/controller/api/v1/basic/vendor.go b/controller/api/v1/basic/vendor.go index 0242f0a..11c77d0 100644 --- a/controller/api/v1/basic/vendor.go +++ b/controller/api/v1/basic/vendor.go @@ -109,10 +109,17 @@ func UpdateVendor(c *gin.Context) { } if err := c.ShouldBindJSON(&vendor); err != nil { - c.JSON(http.StatusBadRequest, gin.H{ - "error": err.Error(), - }) - return + if errs, ok := err.(validator.ValidationErrors); !ok { + c.JSON(http.StatusBadRequest, gin.H{ + "msg": err.Error(), + }) + return + } else { + c.JSON(http.StatusBadRequest, gin.H{ + "msg": errs.Translate(utils.Trans), + }) + return + } } if exist := basic.ExistVendorByName(vendor.Name); exist { diff --git a/controller/api/v1/user.go b/controller/api/v1/user.go index f6551c3..1dfe6e6 100644 --- a/controller/api/v1/user.go +++ b/controller/api/v1/user.go @@ -1,7 +1,7 @@ /* * @Date: 2021-03-21 19:54:57 * @LastEditors: viletyy - * @LastEditTime: 2021-04-06 17:02:38 + * @LastEditTime: 2021-04-06 18:01:36 * @FilePath: /potato/controller/api/v1/user.go */ package v1 @@ -10,6 +10,7 @@ import ( "net/http" "github.com/gin-gonic/gin" + "github.com/go-playground/validator/v10" "github.com/viletyy/potato/global" "github.com/viletyy/potato/models" "github.com/viletyy/potato/utils" @@ -24,12 +25,12 @@ type AuthResponse struct { type AuthRequest struct { Username string `json:"username" validate:"required"` - Password string `json:"password" validate:"required,gte=6` + Password string `json:"password" validate:"required,gte=6"` } type RegisterRequest struct { Username string `json:"username" validate:"required"` - Password string `json:"password" validate:"required,gte=6` + Password string `json:"password" validate:"required,gte=6"` Nickname string `json:"nickname"` } @@ -43,10 +44,17 @@ type RegisterRequest struct { func Auth(c *gin.Context) { var user AuthRequest if err := c.ShouldBindJSON(&user); err != nil { - c.JSON(http.StatusBadRequest, gin.H{ - "error": err.Error(), - }) - return + if errs, ok := err.(validator.ValidationErrors); !ok { + c.JSON(http.StatusBadRequest, gin.H{ + "msg": err.Error(), + }) + return + } else { + c.JSON(http.StatusBadRequest, gin.H{ + "msg": errs.Translate(utils.Trans), + }) + return + } } mUser, gErr := models.GetUserByUsername(user.Username) @@ -86,10 +94,17 @@ func Auth(c *gin.Context) { func Register(c *gin.Context) { var user RegisterRequest if err := c.ShouldBindJSON(&user); err != nil { - c.JSON(http.StatusBadRequest, gin.H{ - "error": err.Error(), - }) - return + if errs, ok := err.(validator.ValidationErrors); !ok { + c.JSON(http.StatusBadRequest, gin.H{ + "msg": err.Error(), + }) + return + } else { + c.JSON(http.StatusBadRequest, gin.H{ + "msg": errs.Translate(utils.Trans), + }) + return + } } if isExsit := models.ExistUserByUsername(user.Username); isExsit { diff --git a/docs/docs.go b/docs/docs.go index cfc7922..bd61d4e 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -284,6 +284,7 @@ var doc = `{ "v1.AuthRequest": { "type": "object", "required": [ + "password", "username" ], "properties": { @@ -298,6 +299,7 @@ var doc = `{ "v1.RegisterRequest": { "type": "object", "required": [ + "password", "username" ], "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index d63ac98..e58d692 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -268,6 +268,7 @@ "v1.AuthRequest": { "type": "object", "required": [ + "password", "username" ], "properties": { @@ -282,6 +283,7 @@ "v1.RegisterRequest": { "type": "object", "required": [ + "password", "username" ], "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 5d93389..f59589b 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -23,6 +23,7 @@ definitions: username: type: string required: + - password - username type: object v1.RegisterRequest: @@ -34,6 +35,7 @@ definitions: username: type: string required: + - password - username type: object info: diff --git a/global/global.go b/global/global.go index f2de624..6aec6c4 100644 --- a/global/global.go +++ b/global/global.go @@ -1,7 +1,7 @@ /* * @Date: 2021-03-22 09:42:09 * @LastEditors: viletyy - * @LastEditTime: 2021-03-22 10:08:22 + * @LastEditTime: 2021-04-06 18:18:15 * @FilePath: /potato/global/global.go */ package global diff --git a/models/user.go b/models/user.go index b9ebc27..b33d4da 100644 --- a/models/user.go +++ b/models/user.go @@ -20,8 +20,8 @@ type UserSearch struct { type User struct { global.Model - Username string `json:"username" binding:"required"` - Password string `json:"-" binding:"required"` + Username string `json:"username"` + Password string `json:"-"` Nickname string `json:"nickname"` IsAdmin bool `json:"is_admin" gorm:"default: false"` }