diff --git a/gateway/handlers/auth.go b/gateway/handlers/auth.go index 64a6407..0471ccb 100644 --- a/gateway/handlers/auth.go +++ b/gateway/handlers/auth.go @@ -202,3 +202,52 @@ func GetUserInfo(db *gorm.DB) gin.HandlerFunc { }) } } + +// 忘记密码表单页面 +func GetForgotPassword(db *gorm.DB) gin.HandlerFunc { + return func(c *gin.Context) { + regions, _ := getRegions(db) + c.HTML(200, "forgot_password.html", gin.H{ + "regions": regions, + "error": "", + }) + } +} + +// 处理忘记密码表单提交 +func PostForgotPassword(db *gorm.DB) gin.HandlerFunc { + return func(c *gin.Context) { + regionID := c.PostForm("region") + fullname := c.PostForm("fullname") + mobile := c.PostForm("mobile") + password := c.PostForm("password") + confirmPassword := c.PostForm("confirmPassword") + + if password != confirmPassword { + c.HTML(200, "login.html", gin.H{ + "error": "两次输入的密码不一致", + }) + return + } + var user models.User + dbErr := db.Where("region_id = ? AND full_name = ? AND mobile = ?", regionID, fullname, mobile).First(&user).Error + if dbErr != nil { + c.HTML(200, "login.html", gin.H{ + "error": "用户信息不正确,请检查地区、姓名和手机号", + }) + return + } + hash, hashErr := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) + if hashErr != nil { + c.HTML(200, "login.html", gin.H{ + "error": "密码加密失败,请重试", + }) + return + } + user.Password = string(hash) + db.Save(&user) + c.HTML(200, "login.html", gin.H{ + "error": "密码重置成功,请用新密码登录", + }) + } +} diff --git a/gateway/main.go b/gateway/main.go index e8f1816..ac4fbca 100644 --- a/gateway/main.go +++ b/gateway/main.go @@ -69,6 +69,9 @@ func main() { r.GET("/register", handlers.GetRegister(db)) r.POST("/register", handlers.PostRegister(db)) r.GET("/api/user/info", handlers.GetUserInfo(db)) + // 新增忘记密码相关路由 + r.GET("/forgot-password", handlers.GetForgotPassword(db)) + r.POST("/forgot-password", handlers.PostForgotPassword(db)) // 文档页面路由 r.GET("/", handlers.ServeIndex(db)) diff --git a/gateway/templates/forgot_password.html b/gateway/templates/forgot_password.html new file mode 100644 index 0000000..c43b0b2 --- /dev/null +++ b/gateway/templates/forgot_password.html @@ -0,0 +1,167 @@ + + +
+ + +