From c61aebab73f3c4161e2e7862823fd02734090ccb Mon Sep 17 00:00:00 2001 From: ZGGSONG Date: Tue, 10 May 2022 10:43:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=81=E8=A3=85response?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/info.go | 9 ++------- controller/login.go | 32 +++++++------------------------- controller/register.go | 26 ++++++-------------------- 3 files changed, 15 insertions(+), 52 deletions(-) diff --git a/controller/info.go b/controller/info.go index 13e92ee..fa84d54 100644 --- a/controller/info.go +++ b/controller/info.go @@ -1,18 +1,13 @@ package controller import ( - "net/http" - "github.com/gin-gonic/gin" "github.com/zggsong/gin-vue-demo/dto" "github.com/zggsong/gin-vue-demo/model" + "github.com/zggsong/gin-vue-demo/response" ) func Info(ctx *gin.Context) { user, _ := ctx.Get("user") - - ctx.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "data": gin.H{"user": dto.ToUserDto(user.(model.User))}, - }) + response.Success(ctx, gin.H{"user": dto.ToUserDto(user.(model.User))}, "") } diff --git a/controller/login.go b/controller/login.go index c819f73..9104ce8 100644 --- a/controller/login.go +++ b/controller/login.go @@ -6,6 +6,7 @@ import ( "github.com/gin-gonic/gin" "github.com/zggsong/gin-vue-demo/common" "github.com/zggsong/gin-vue-demo/model" + "github.com/zggsong/gin-vue-demo/response" "golang.org/x/crypto/bcrypt" ) @@ -17,17 +18,11 @@ func Login(ctx *gin.Context) { // 数据验证 if len(telephone) != 11 { - ctx.JSON(http.StatusUnprocessableEntity, gin.H{ - "code": http.StatusUnprocessableEntity, - "message": "手机号必须是11位", - }) + response.Response(ctx, http.StatusUnprocessableEntity, 400, nil, "手机号必须是11位") return } if len(password) < 6 { - ctx.JSON(http.StatusUnprocessableEntity, gin.H{ - "code": http.StatusUnprocessableEntity, - "message": "密码必须大于6位", - }) + response.Response(ctx, http.StatusUnprocessableEntity, 400, nil, "密码必须大于6位") return } @@ -35,36 +30,23 @@ func Login(ctx *gin.Context) { var user model.User DB.Where("telephone = ?", telephone).First(&user) if user.ID == 0 { - ctx.JSON(http.StatusUnprocessableEntity, gin.H{ - "code": http.StatusUnprocessableEntity, - "message": "用户不存在", - }) + response.Response(ctx, http.StatusUnprocessableEntity, 400, nil, "用户不存在") return } // 密码验证 if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)); err != nil { - ctx.JSON(http.StatusBadRequest, gin.H{ - "code": http.StatusBadRequest, - "message": "密码错误", - }) + response.Response(ctx, http.StatusUnprocessableEntity, 400, nil, "密码错误") return } // 发放Token token, err := common.ReleaseToken(user) if err != nil { - ctx.JSON(http.StatusInternalServerError, gin.H{ - "code": http.StatusInternalServerError, - "message": "发放Token失败", - }) + response.Response(ctx, http.StatusInternalServerError, 500, nil, "发放Token失败") return } // 返回结果 - ctx.JSON(200, gin.H{ - "code": http.StatusOK, - "data": gin.H{"token": token}, - "message": "登录成功", - }) + response.Success(ctx, gin.H{"token": token}, "登录成功") } diff --git a/controller/register.go b/controller/register.go index 18dfe92..6bc169f 100644 --- a/controller/register.go +++ b/controller/register.go @@ -6,6 +6,7 @@ import ( "github.com/gin-gonic/gin" "github.com/zggsong/gin-vue-demo/common" "github.com/zggsong/gin-vue-demo/model" + "github.com/zggsong/gin-vue-demo/response" "github.com/zggsong/gin-vue-demo/util" "golang.org/x/crypto/bcrypt" "gorm.io/gorm" @@ -20,17 +21,11 @@ func Register(ctx *gin.Context) { // 数据验证 if len(telephone) != 11 { - ctx.JSON(http.StatusUnprocessableEntity, gin.H{ - "code": http.StatusUnprocessableEntity, - "message": "手机号必须是11位", - }) + response.Response(ctx, http.StatusUnprocessableEntity, 400, nil, "手机号必须是11位") return } if len(password) < 6 { - ctx.JSON(http.StatusUnprocessableEntity, gin.H{ - "code": http.StatusUnprocessableEntity, - "message": "密码必须大于6位", - }) + response.Response(ctx, http.StatusUnprocessableEntity, 400, nil, "密码必须大于6位") return } // 如果名字为空则返回10为随机字符串 @@ -40,20 +35,14 @@ func Register(ctx *gin.Context) { // 判断手机号是否存在 if isExistTelephone(DB, telephone) { - ctx.JSON(http.StatusUnprocessableEntity, gin.H{ - "code": http.StatusUnprocessableEntity, - "message": "手机号已存在", - }) + response.Response(ctx, http.StatusUnprocessableEntity, 400, nil, "用户已存在") return } // 创建用户 hasedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) if err != nil { - ctx.JSON(http.StatusInternalServerError, gin.H{ - "code": http.StatusInternalServerError, - "message": "密码加密失败", - }) + response.Response(ctx, http.StatusInternalServerError, 500, nil, "密码加密失败") return } newUser := model.User{ @@ -64,10 +53,7 @@ func Register(ctx *gin.Context) { DB.Create(&newUser) // 返回结果 - ctx.JSON(200, gin.H{ - "code": http.StatusOK, - "message": "注册成功", - }) + response.Success(ctx, nil, "注册成功") } func isExistTelephone(db *gorm.DB, telephone string) bool {