封装response

master
ZGGSONG 3 years ago
parent a70efc8203
commit c61aebab73

@ -1,18 +1,13 @@
package controller package controller
import ( import (
"net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/zggsong/gin-vue-demo/dto" "github.com/zggsong/gin-vue-demo/dto"
"github.com/zggsong/gin-vue-demo/model" "github.com/zggsong/gin-vue-demo/model"
"github.com/zggsong/gin-vue-demo/response"
) )
func Info(ctx *gin.Context) { func Info(ctx *gin.Context) {
user, _ := ctx.Get("user") user, _ := ctx.Get("user")
response.Success(ctx, gin.H{"user": dto.ToUserDto(user.(model.User))}, "")
ctx.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"data": gin.H{"user": dto.ToUserDto(user.(model.User))},
})
} }

@ -6,6 +6,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/zggsong/gin-vue-demo/common" "github.com/zggsong/gin-vue-demo/common"
"github.com/zggsong/gin-vue-demo/model" "github.com/zggsong/gin-vue-demo/model"
"github.com/zggsong/gin-vue-demo/response"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
) )
@ -17,17 +18,11 @@ func Login(ctx *gin.Context) {
// 数据验证 // 数据验证
if len(telephone) != 11 { if len(telephone) != 11 {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{ response.Response(ctx, http.StatusUnprocessableEntity, 400, nil, "手机号必须是11位")
"code": http.StatusUnprocessableEntity,
"message": "手机号必须是11位",
})
return return
} }
if len(password) < 6 { if len(password) < 6 {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{ response.Response(ctx, http.StatusUnprocessableEntity, 400, nil, "密码必须大于6位")
"code": http.StatusUnprocessableEntity,
"message": "密码必须大于6位",
})
return return
} }
@ -35,36 +30,23 @@ func Login(ctx *gin.Context) {
var user model.User var user model.User
DB.Where("telephone = ?", telephone).First(&user) DB.Where("telephone = ?", telephone).First(&user)
if user.ID == 0 { if user.ID == 0 {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{ response.Response(ctx, http.StatusUnprocessableEntity, 400, nil, "用户不存在")
"code": http.StatusUnprocessableEntity,
"message": "用户不存在",
})
return return
} }
// 密码验证 // 密码验证
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)); err != nil { if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{ response.Response(ctx, http.StatusUnprocessableEntity, 400, nil, "密码错误")
"code": http.StatusBadRequest,
"message": "密码错误",
})
return return
} }
// 发放Token // 发放Token
token, err := common.ReleaseToken(user) token, err := common.ReleaseToken(user)
if err != nil { if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{ response.Response(ctx, http.StatusInternalServerError, 500, nil, "发放Token失败")
"code": http.StatusInternalServerError,
"message": "发放Token失败",
})
return return
} }
// 返回结果 // 返回结果
ctx.JSON(200, gin.H{ response.Success(ctx, gin.H{"token": token}, "登录成功")
"code": http.StatusOK,
"data": gin.H{"token": token},
"message": "登录成功",
})
} }

@ -6,6 +6,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/zggsong/gin-vue-demo/common" "github.com/zggsong/gin-vue-demo/common"
"github.com/zggsong/gin-vue-demo/model" "github.com/zggsong/gin-vue-demo/model"
"github.com/zggsong/gin-vue-demo/response"
"github.com/zggsong/gin-vue-demo/util" "github.com/zggsong/gin-vue-demo/util"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
"gorm.io/gorm" "gorm.io/gorm"
@ -20,17 +21,11 @@ func Register(ctx *gin.Context) {
// 数据验证 // 数据验证
if len(telephone) != 11 { if len(telephone) != 11 {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{ response.Response(ctx, http.StatusUnprocessableEntity, 400, nil, "手机号必须是11位")
"code": http.StatusUnprocessableEntity,
"message": "手机号必须是11位",
})
return return
} }
if len(password) < 6 { if len(password) < 6 {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{ response.Response(ctx, http.StatusUnprocessableEntity, 400, nil, "密码必须大于6位")
"code": http.StatusUnprocessableEntity,
"message": "密码必须大于6位",
})
return return
} }
// 如果名字为空则返回10为随机字符串 // 如果名字为空则返回10为随机字符串
@ -40,20 +35,14 @@ func Register(ctx *gin.Context) {
// 判断手机号是否存在 // 判断手机号是否存在
if isExistTelephone(DB, telephone) { if isExistTelephone(DB, telephone) {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{ response.Response(ctx, http.StatusUnprocessableEntity, 400, nil, "用户已存在")
"code": http.StatusUnprocessableEntity,
"message": "手机号已存在",
})
return return
} }
// 创建用户 // 创建用户
hasedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) hasedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil { if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{ response.Response(ctx, http.StatusInternalServerError, 500, nil, "密码加密失败")
"code": http.StatusInternalServerError,
"message": "密码加密失败",
})
return return
} }
newUser := model.User{ newUser := model.User{
@ -64,10 +53,7 @@ func Register(ctx *gin.Context) {
DB.Create(&newUser) DB.Create(&newUser)
// 返回结果 // 返回结果
ctx.JSON(200, gin.H{ response.Success(ctx, nil, "注册成功")
"code": http.StatusOK,
"message": "注册成功",
})
} }
func isExistTelephone(db *gorm.DB, telephone string) bool { func isExistTelephone(db *gorm.DB, telephone string) bool {

Loading…
Cancel
Save