ziwen пре 1 година
родитељ
комит
da76978483
4 измењених фајлова са 189 додато и 6 уклоњено
  1. 166 4
      controller/auth.go
  2. 1 0
      models/english_report_email/email.go
  3. 4 1
      routers/auth.go
  4. 18 1
      services/user.go

+ 166 - 4
controller/auth.go

@@ -141,20 +141,20 @@ func (a *AuthController) ModifyPwd(c *gin.Context) {
 		return
 	}
 
-	if req.OldPwd != userinfo.Password{
+	if req.OldPwd != userinfo.Password {
 		resp.Fail("旧密码错误,请重新输入", c)
 		return
 	}
 
 	emailitem := english_report_email.Email{
-		Id:           userinfo.Id,
-		Password:     req.NewPwd,
+		Id:       userinfo.Id,
+		Password: req.NewPwd,
 	}
 	emailitem.ModifyTime = time.Now()
 
 	err = emailitem.Update([]string{"Password"})
 	if err != nil {
-		resp.FailMsg("修改密码失败", "修改密码失败,Err:" + err.Error(), c)
+		resp.FailMsg("修改密码失败", "修改密码失败,Err:"+err.Error(), c)
 		return
 	}
 
@@ -228,3 +228,165 @@ func (a *AuthController) GetEmailCode(c *gin.Context) {
 
 	resp.Ok("获取验证码成功", c)
 }
+
+func (a *AuthController) BindMobile(c *gin.Context) {
+	req := new(services.BindMobileReq)
+	err := c.ShouldBind(&req)
+	if err != nil {
+		errs, ok := err.(validator.ValidationErrors)
+		if !ok {
+			resp.FailData("参数解析失败", "Err:"+err.Error(), c)
+			return
+		}
+		resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
+		return
+	}
+	userinfo := services.GetInfoByClaims(c)
+
+	if req.Mobile == "" {
+		resp.Fail("手机号不能为空", c)
+		return
+	}
+
+	if req.SmsCode == "" {
+		resp.Fail("验证码不能为空", c)
+		return
+	}
+
+	item, err := msg_code.GetMsgCode(req.Mobile, req.SmsCode)
+	if err != nil {
+		if err == utils.ErrNoRow {
+			resp.Fail("校验验证码失败,Err:"+err.Error(), c)
+			return
+		} else {
+			resp.Fail("校验验证码失败,Err:"+err.Error(), c)
+			return
+		}
+	}
+	if item == nil {
+		resp.Fail("验证码错误,请重新输入", c)
+		return
+	}
+
+	user := english_report_email.Email{
+		Id:     userinfo.Id,
+		Mobile: req.Mobile,
+	}
+	user.ModifyTime = time.Now()
+
+	err = user.Update([]string{"Mobile"})
+	if err != nil {
+		resp.FailMsg("绑定手机号失败", "修改手机号失败,Err:"+err.Error(), c)
+		return
+	}
+
+	resp.Ok("绑定成功", c)
+}
+
+func (a *AuthController) ForgetPwd(c *gin.Context) {
+	req := new(services.ForgetPwdReq)
+	err := c.ShouldBind(&req)
+	if err != nil {
+		errs, ok := err.(validator.ValidationErrors)
+		if !ok {
+			resp.FailData("参数解析失败", "Err:"+err.Error(), c)
+			return
+		}
+		resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
+		return
+	}
+	userinfo := services.GetInfoByClaims(c)
+
+	if req.Account == "" {
+		resp.Fail("手机号不能为空", c)
+		return
+	}
+
+	if req.SmsCode == "" {
+		resp.Fail("验证码不能为空", c)
+		return
+	}
+
+	if req.Password == "" {
+		resp.Fail("新密码不能为空", c)
+		return
+	}
+
+	item, err := msg_code.GetMsgCode(req.Account, req.SmsCode)
+	if err != nil {
+		if err == utils.ErrNoRow {
+			resp.Fail("校验验证码失败,Err:"+err.Error(), c)
+			return
+		} else {
+			resp.Fail("校验验证码失败,Err:"+err.Error(), c)
+			return
+		}
+	}
+	if item == nil {
+		resp.Fail("验证码错误,请重新输入", c)
+		return
+	}
+
+	emailitem := english_report_email.Email{
+		Id:       userinfo.Id,
+		Password: req.Password,
+	}
+	emailitem.ModifyTime = time.Now()
+
+	err = emailitem.Update([]string{"Password"})
+	if err != nil {
+		resp.FailMsg("修改密码失败", "修改密码失败,Err:"+err.Error(), c)
+		return
+	}
+
+	resp.Ok("修改成功", c)
+}
+
+func (a *AuthController) ModifyMobile(c *gin.Context) {
+	req := new(services.ModifyMobile)
+	err := c.ShouldBind(&req)
+	if err != nil {
+		errs, ok := err.(validator.ValidationErrors)
+		if !ok {
+			resp.FailData("参数解析失败", "Err:"+err.Error(), c)
+			return
+		}
+		resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
+		return
+	}
+	userinfo := services.GetInfoByClaims(c)
+
+	if req.NewMobile == "" {
+		resp.Fail("新密码不能为空", c)
+		return
+	}
+
+	if req.OldMobile == "" {
+		resp.Fail("旧密码不能为空", c)
+		return
+	}
+
+	if req.OldMobile != userinfo.Mobile {
+		resp.Fail("旧密码错误,请重新输入", c)
+		return
+	}
+
+	if req.SmsCode == "" {
+		resp.Fail("验证码不能为空", c)
+		return
+	}
+
+	emailitem := english_report_email.Email{
+		Id:     userinfo.Id,
+		Mobile: req.NewMobile,
+	}
+	emailitem.ModifyTime = time.Now()
+
+	err = emailitem.Update([]string{"Mobile"})
+	if err != nil {
+		resp.FailMsg("修改手机号失败", "修改手机号失败,Err:"+err.Error(), c)
+		return
+	}
+
+	resp.Ok("修改成功", c)
+}

+ 1 - 0
models/english_report_email/email.go

@@ -21,6 +21,7 @@ type Email struct {
 	Enable       int       `gorm:"column:enabled" json:"enabled"`
 	Status       int       `description:"1:正式,2:临时,3:终止"`
 	Password     string    `gorm:"column:password" json:"password"`
+	Mobile       string    `gorm:"column:mobile" json:"mobile"`
 	base.TimeBase
 }
 

+ 4 - 1
routers/auth.go

@@ -17,5 +17,8 @@ func InitAuth(baseGroup *gin.RouterGroup) {
 
 	authTokenController := new(controller.AuthController)
 	authTokenGroup := baseGroup.Group("auth/").Use(middleware.Token())
-	authTokenGroup.POST("modifyPwd", authTokenController.ModifyPwd).Use(middleware.Token())
+	authTokenGroup.POST("modifyPwd", authTokenController.ModifyPwd)
+	authTokenGroup.POST("bindMobile", authTokenController.BindMobile)
+	authTokenGroup.POST("forgetPwd", authTokenController.ForgetPwd)
+	authTokenGroup.POST("modifyMobile", authTokenController.ModifyMobile)
 }

+ 18 - 1
services/user.go

@@ -47,4 +47,21 @@ func GetInfoByClaims(c *gin.Context) (userInfo UserInfo) {
 	claims, _ := c.Get("userInfo")
 	userInfo = claims.(UserInfo)
 	return
-}
+}
+
+type BindMobileReq struct {
+	Mobile  string
+	SmsCode string //验证码
+}
+
+type ForgetPwdReq struct {
+	Account  string `description:"账号"`
+	Password string `description:"密码"`
+	SmsCode  string
+}
+
+type ModifyMobile struct {
+	OldMobile string
+	NewMobile string
+	SmsCode   string
+}