Skip to content

Commit

Permalink
feat:add 注册接口增加性别和邮箱
Browse files Browse the repository at this point in the history
  • Loading branch information
mao888 committed Apr 25, 2023
1 parent bfe48e2 commit 1cf409a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
3 changes: 1 addition & 2 deletions bluebell_backend/controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
func SignUpHandler(c *gin.Context) {
// 1.获取请求参数
var fo *models.RegisterForm

// 2.校验数据有效性
if err := c.ShouldBindJSON(&fo); err != nil {
// 请求参数有误,直接返回响应
Expand All @@ -34,7 +33,7 @@ func SignUpHandler(c *gin.Context) {
ResponseErrorWithMsg(c, CodeInvalidParams, removeTopStruct(errs.Translate(trans)))
return // 翻译错误
}

fmt.Printf("fo: %v\n", fo)
// 3.业务处理 —— 注册用户
if err := logic.SignUp(fo); err != nil {
zap.L().Error("logic.signup failed", zap.Error(err))
Expand Down
4 changes: 2 additions & 2 deletions bluebell_backend/dao/mysql/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func InsertUser(user models.User) (error error) {
// 对密码进行加密
user.Password = encryptPassword([]byte(user.Password))
// 执行SQL语句入库
sqlstr := `insert into user(user_id,username,password) values(?,?,?)`
_, err := db.Exec(sqlstr, user.UserID, user.UserName, user.Password)
sqlstr := `insert into user(user_id,username,password,email,gender) values(?,?,?,?,?)`
_, err := db.Exec(sqlstr, user.UserID, user.UserName, user.Password, user.Email, user.Gender)
return err
}

Expand Down
2 changes: 2 additions & 0 deletions bluebell_backend/logic/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func SignUp(p *models.RegisterForm) (error error) {
UserID: userId,
UserName: p.UserName,
Password: p.Password,
Email: p.Email,
Gender: p.Gender,
}
// 3、保存进数据库
return mysql.InsertUser(u)
Expand Down
26 changes: 18 additions & 8 deletions bluebell_backend/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import (
"errors"
)

/**
* @Author huchao
* @Description //TODO 定义请求参数结构体
* @Date 22:09 2022/2/10
**/
// User 定义请求参数结构体
type User struct {
UserID uint64 `json:"user_id,string" db:"user_id"` // 指定json序列化/反序列化时使用小写user_id
UserName string `json:"username" db:"username"`
Password string `json:"password" db:"password"`
Email string `json:"email" db:"gender"` // 邮箱
Gender int `json:"gender" db:"gender"` // 性别
AccessToken string
RefreshToken string
}
Expand All @@ -23,6 +21,8 @@ func (u *User) UnmarshalJSON(data []byte) (err error) {
required := struct {
UserName string `json:"username" db:"username"`
Password string `json:"password" db:"password"`
Email string `json:"email" db:"gender"` // 邮箱
Gender int `json:"gender" db:"gender"` // 性别
}{}
err = json.Unmarshal(data, &required)
if err != nil {
Expand All @@ -34,14 +34,18 @@ func (u *User) UnmarshalJSON(data []byte) (err error) {
} else {
u.UserName = required.UserName
u.Password = required.Password
u.Email = required.Email
u.Gender = required.Gender
}
return
}

// RegisterForm 注册请求参数
type RegisterForm struct {
UserName string `json:"username" binding:"required"`
Password string `json:"password" binding:"required"`
UserName string `json:"username" binding:"required"` // 用户名
Email string `json:"email" binding:"required"` // 邮箱
Gender int `json:"gender" binding:"oneof=0 1 2"` // 性别 0:未知 1:男 2:女
Password string `json:"password" binding:"required"` // 密码
ConfirmPassword string `json:"confirm_password" binding:"required,eqfield=Password"`
}

Expand All @@ -55,7 +59,9 @@ type LoginForm struct {
func (r *RegisterForm) UnmarshalJSON(data []byte) (err error) {
required := struct {
UserName string `json:"username"`
Password string `json:"password"`
Email string `json:"email"` // 邮箱
Gender int `json:"gender"` // 性别 0:未知 1:男 2:女
Password string `json:"password"` // 密码
ConfirmPassword string `json:"confirm_password"`
}{}
err = json.Unmarshal(data, &required)
Expand All @@ -65,10 +71,14 @@ func (r *RegisterForm) UnmarshalJSON(data []byte) (err error) {
err = errors.New("缺少必填字段username")
} else if len(required.Password) == 0 {
err = errors.New("缺少必填字段password")
} else if len(required.Email) == 0 {
err = errors.New("缺少必填字段email")
} else if required.Password != required.ConfirmPassword {
err = errors.New("两次密码不一致")
} else {
r.UserName = required.UserName
r.Email = required.Email
r.Gender = required.Gender
r.Password = required.Password
r.ConfirmPassword = required.ConfirmPassword
}
Expand Down

0 comments on commit 1cf409a

Please sign in to comment.