caipin 4 năm trước cách đây
mục cha
commit
87178f568b

+ 47 - 0
services/project_account_service.go

@@ -28,6 +28,7 @@ import (
 //定义项目用户Service接口
 type ProjectAccountService interface {
 	ValidRule(ctx iris.Context) (viewmodels.ProjectAccount, error)
+	ValidRuleChangePassword(ctx iris.Context) (viewmodels.AccountPassword, error)
 	ValidRulePermission(ctx iris.Context) (viewmodels.Permission, error)
 	ValidRuleAccount(ctx iris.Context) (viewmodels.ProjectAccount, error)
 
@@ -43,6 +44,8 @@ type ProjectAccountService interface {
 	GetProjectInfo(id int) (viewmodels.ProjectInfo, error)
 	Delete(id int, projectId int) error
 
+	ChangePassword(AccountData viewmodels.AccountPassword, projectId int, projectAccountId int) error
+
 	SaveAuth(permission viewmodels.Permission, projectId int, accountId int) error
 }
 
@@ -112,6 +115,25 @@ func (s *projectAccountService) ValidRule(ctx iris.Context) (viewmodels.ProjectA
 	return accountVaild, nil
 }
 
+// 验证密码
+func (s *projectAccountService) ValidRuleChangePassword(ctx iris.Context) (viewmodels.AccountPassword, error) {
+	accountVaild := viewmodels.AccountPassword{}
+	err := ctx.ReadJSON(&accountVaild)
+	if err != nil {
+		log.Println("account-ValidRule-ReadForm转换异常, error=", err)
+		return accountVaild, err
+	}
+
+	err = accountVaild.ValidateChangePassword()
+
+	if err != nil {
+		log.Println("权限验证, error=", err)
+		return accountVaild, err
+	}
+
+	return accountVaild, nil
+}
+
 // 用户规则验证
 func (s *projectAccountService) ValidRulePermission(ctx iris.Context) (viewmodels.Permission, error) {
 	accountVaild := viewmodels.Permission{}
@@ -314,6 +336,31 @@ func (s *projectAccountService) ChangeAccount(id int, projectId int, viewAccount
 	return nil
 }
 
+// 更换密码
+func (s *projectAccountService) ChangePassword(AccountData viewmodels.AccountPassword, projectId int, projectAccountId int) error {
+	// 1.获得账号
+	accountData := s.dao.Get(projectAccountId, projectId)
+	// 2.比对密码
+	password := comm.CreatePasswordSign(AccountData.Password, accountData.Account)
+
+	if accountData.Password != password {
+		return errors.New("原密码输入不正确")
+	}
+
+	// 3修改密码
+	field := []string{"Password"}
+	account := models.CmProjectAccount{}
+	account.Id = projectAccountId
+	account.ProjectId = projectId
+	account.Password = comm.CreatePasswordSign(AccountData.NewPassword, accountData.Account)
+
+	err := s.dao.Update(&account, field)
+	if err != nil {
+		return errors.New("密码更新失败")
+	}
+	return nil
+}
+
 func (s *projectAccountService) GetProjectInfo(id int) (viewmodels.ProjectInfo, error) {
 	projectInfo, err := s.dao.FindById(id)
 	return projectInfo, err

+ 48 - 0
web/api/project_account_api.go

@@ -192,3 +192,51 @@ func (c *ProjectAccountApi) PostAccountSave() {
 		c.Ctx.JSON(iris.Map{"code": 0, "msg": "编辑成功"})
 	}
 }
+
+// @Summary 修改密码
+// @Tags 项目账号相关
+// @Description 编辑账号
+// @Accept  json
+// @Produce  json
+// @Security ApiKeyAuth
+// @Param   password     body    string     true        "密码"
+// @Param   newPassword     body    string     true        "新密码"
+// @Param   confirmPassword     body    string     true        "确认新密码"
+// @Success 200 {string} string "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
+// @Router /api/projectAccount/change/password [post]
+func (c *ProjectAccountApi) PostChangePassword() {
+	// 验证内容
+	AccountData, err := c.ServiceProjectAccount.ValidRuleChangePassword(c.Ctx)
+	if err != nil {
+		ErrMsg := utils.FormValidError(err)
+		c.Ctx.JSON(iris.Map{"code": -1, "msg": ErrMsg})
+		return
+	} else {
+
+		if AccountData.ConfirmPassword != AccountData.NewPassword {
+			c.Ctx.JSON(iris.Map{"code": -1, "msg": "新密码和确认密码不一致"})
+			return
+		}
+
+		// 获得项目ID
+		projectId, err := utils.GetProjectId(c.Ctx)
+		if err != nil {
+			c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
+			return
+		}
+
+		// 获得项目账号ID
+		projectAccountId, err := utils.GetProjectAccountId(c.Ctx)
+		if err != nil {
+			c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
+			return
+		}
+
+		err = c.ServiceProjectAccount.ChangePassword(AccountData, projectId, projectAccountId)
+		if err != nil {
+			c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
+			return
+		}
+		c.Ctx.JSON(iris.Map{"code": 0, "msg": "更新成功"})
+	}
+}

+ 14 - 0
web/viewmodels/project_account.go

@@ -28,6 +28,12 @@ type ProjectAccount struct {
 	Enable       int    `form:"enable" json:"enable"`
 }
 
+type AccountPassword struct {
+	Password        string `form:"password" json:"password"`
+	NewPassword     string `form:"newPassword" json:"newPassword"`
+	ConfirmPassword string `form:"confirmPassword" json:"confirmPassword"`
+}
+
 func (l ProjectAccount) Validate() error {
 	return validation.ValidateStruct(&l,
 		validation.Field(&l.Account, validation.Required.Error("账号不能为空"), validation.Match(regexp.MustCompile("^[A-Za-z0-9]+$")).Error("只支持英文数字组合")),
@@ -69,6 +75,14 @@ func (l ProjectAccount) ValidateAccount() error {
 	)
 }
 
+func (l AccountPassword) ValidateChangePassword() error {
+	return validation.ValidateStruct(&l,
+		validation.Field(&l.Password, validation.Required.Error("密码不能为空")),
+		validation.Field(&l.NewPassword, validation.Required.Error("新密码不能为空")),
+		validation.Field(&l.ConfirmPassword, validation.Required.Error("确认新密码不能为空")),
+	)
+}
+
 //MoveId string `form:"moveId"`
 // LastLogin    int       `xorm:"comment('最后登录时间') INT(11)"`