Skip to content

Commit

Permalink
feat: 修改密码接口
Browse files Browse the repository at this point in the history
  • Loading branch information
RockChinQ committed Apr 11, 2024
1 parent c055415 commit f008164
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
29 changes: 29 additions & 0 deletions backend/controller/accapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func NewAccountRouter(rg *gin.RouterGroup, as service.AccountService) *AccountRo
group.POST("/create", ar.CreateAccount)
group.POST("/login", ar.LoginAccount)
group.PUT("/reset", ar.ResetPassword)
group.PUT("/change-pwd", ar.ChangePassword)

return ar
}
Expand Down Expand Up @@ -93,3 +94,31 @@ func (ar *AccountRouter) ResetPassword(c *gin.Context) {
"passwd": pwd,
})
}

// 修改密码
func (ar *AccountRouter) ChangePassword(c *gin.Context) {
uin, err := ar.GetUin(c)

if err != nil {
ar.StatusCode(c, 401, err.Error())
return
}

// 取body的json里的uin和pwd
var body AccountChangePasswordBody

if err := c.ShouldBindJSON(&body); err != nil {
ar.Fail(c, 1, err.Error())
return
}

// 修改密码
err = ar.AccountService.ChangePassword(uin, body.NewPasswd)

if err != nil {
ar.Fail(c, 1, err.Error())
return
}

ar.Success(c, nil)
}
5 changes: 5 additions & 0 deletions backend/controller/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ type AccountLoginBody struct {
Passwd string `json:"passwd" binding:"required"`
}

type AccountChangePasswordBody struct {
// 新密码
NewPasswd string `json:"new_passwd" binding:"required"`
}

type PostNewBody struct {

// UUID UUID 必须
Expand Down
22 changes: 22 additions & 0 deletions backend/service/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,25 @@ func (as *AccountService) ResetPassword(uin int64) (string, error) {

return newPwd, err
}

// 修改密码
func (as *AccountService) ChangePassword(uin int64, newPwd string) error {
acc, err := as.DB.GetAccountByUIN(uin)

if err != nil {
return err
}

if acc == nil {
return ErrAccountNotFound
}

salt := util.GenerateRandomSalt()

encryptedPwd := util.EncryptPassword(newPwd, salt)

// 更新密码
err = as.DB.UpdatePassword(uin, encryptedPwd, salt)

return err
}

0 comments on commit f008164

Please sign in to comment.