封装response

master
ZGGSONG 3 years ago
parent a70efc8203
commit c61aebab73

@ -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))}, "")
}

@ -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}, "登录成功")
}

@ -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 {

Loading…
Cancel
Save