feat(auth): 添加忘记密码功能
- 新增忘记密码表单页面和处理逻辑 - 实现用户通过地区、姓名和手机号找回密码的功能 - 添加密码重置成功后的提示信息 - 优化登录页面,增加忘记密码链接
This commit is contained in:
parent
866fa22461
commit
0edabc54b0
@ -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": "密码重置成功,请用新密码登录",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -69,6 +69,9 @@ func main() {
|
|||||||
r.GET("/register", handlers.GetRegister(db))
|
r.GET("/register", handlers.GetRegister(db))
|
||||||
r.POST("/register", handlers.PostRegister(db))
|
r.POST("/register", handlers.PostRegister(db))
|
||||||
r.GET("/api/user/info", handlers.GetUserInfo(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))
|
r.GET("/", handlers.ServeIndex(db))
|
||||||
|
|||||||
167
gateway/templates/forgot_password.html
Normal file
167
gateway/templates/forgot_password.html
Normal file
@ -0,0 +1,167 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>重置密码 - 余氏族谱</title>
|
||||||
|
<style>
|
||||||
|
:root {
|
||||||
|
--primary-color: #2c3e50;
|
||||||
|
--accent-color: #42b983;
|
||||||
|
--error-color: #e74c3c;
|
||||||
|
}
|
||||||
|
body {
|
||||||
|
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
|
||||||
|
background: #f5f5f5;
|
||||||
|
min-height: 100vh;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
padding: 2rem;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
.login-container {
|
||||||
|
background: white;
|
||||||
|
padding: 2.5rem;
|
||||||
|
border-radius: 12px;
|
||||||
|
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
||||||
|
width: 100%;
|
||||||
|
max-width: 500px;
|
||||||
|
margin-top: 4rem;
|
||||||
|
}
|
||||||
|
h1 {
|
||||||
|
color: var(--primary-color);
|
||||||
|
text-align: center;
|
||||||
|
margin-bottom: 2rem;
|
||||||
|
font-size: 1.8rem;
|
||||||
|
}
|
||||||
|
.form-group {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 0.5rem;
|
||||||
|
margin-bottom: 1rem;
|
||||||
|
}
|
||||||
|
label {
|
||||||
|
color: var(--primary-color);
|
||||||
|
font-weight: 500;
|
||||||
|
}
|
||||||
|
input, select {
|
||||||
|
padding: 0.8rem;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
border-radius: 6px;
|
||||||
|
font-size: 1rem;
|
||||||
|
transition: border-color 0.3s ease;
|
||||||
|
}
|
||||||
|
input:focus, select:focus {
|
||||||
|
outline: none;
|
||||||
|
border-color: var(--accent-color);
|
||||||
|
box-shadow: 0 0 0 2px rgba(66, 185, 131, 0.2);
|
||||||
|
}
|
||||||
|
button {
|
||||||
|
background: var(--accent-color);
|
||||||
|
color: white;
|
||||||
|
padding: 0.8rem;
|
||||||
|
border: none;
|
||||||
|
border-radius: 6px;
|
||||||
|
font-size: 1rem;
|
||||||
|
font-weight: 500;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background 0.3s ease;
|
||||||
|
width: 100%;
|
||||||
|
margin-top: 1rem;
|
||||||
|
}
|
||||||
|
button:hover {
|
||||||
|
background: #3aa876;
|
||||||
|
}
|
||||||
|
.error-message {
|
||||||
|
background: #fee;
|
||||||
|
color: var(--error-color);
|
||||||
|
padding: 1rem;
|
||||||
|
border-radius: 6px;
|
||||||
|
border: 1px solid #ffd6d6;
|
||||||
|
margin-top: 1rem;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
.password-match-error {
|
||||||
|
color: var(--error-color);
|
||||||
|
font-size: 0.875rem;
|
||||||
|
margin-top: 0.25rem;
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
.password-match-error.show {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
@media (max-width: 480px) {
|
||||||
|
.login-container {
|
||||||
|
padding: 1.5rem;
|
||||||
|
margin-top: 2rem;
|
||||||
|
width: calc(100% - 2rem);
|
||||||
|
}
|
||||||
|
body {
|
||||||
|
padding: 1rem;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="login-container">
|
||||||
|
<h1>重置密码</h1>
|
||||||
|
<form action="/forgot-password" method="post" id="forgotPasswordForm">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="region">所在地区</label>
|
||||||
|
<select name="region" id="region" required>
|
||||||
|
<option value="">请选择所在地区</option>
|
||||||
|
{{ range .regions }}
|
||||||
|
<option value="{{ .ID }}">{{ .Name }}</option>
|
||||||
|
{{ end }}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="fullname">真实姓名</label>
|
||||||
|
<input type="text" id="fullname" name="fullname" required placeholder="请输入真实姓名">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="mobile">手机号码</label>
|
||||||
|
<input type="tel" id="mobile" name="mobile" required pattern="[0-9]{11}" placeholder="请输入11位手机号">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="password">新密码</label>
|
||||||
|
<input type="password" id="password" name="password" required minlength="6" placeholder="至少6位密码">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="confirmPassword">确认新密码</label>
|
||||||
|
<input type="password" id="confirmPassword" name="confirmPassword" required minlength="6" placeholder="请再次输入新密码">
|
||||||
|
<div class="password-match-error">两次输入的密码不一致</div>
|
||||||
|
</div>
|
||||||
|
<button type="submit">提交</button>
|
||||||
|
<div class="login-link" style="text-align:center;margin-top:1.5rem;">
|
||||||
|
<a href="/login">返回登录</a>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
{{ if .error }}
|
||||||
|
<p class="error-message">{{ .error }}</p>
|
||||||
|
{{ end }}
|
||||||
|
</div>
|
||||||
|
<script>
|
||||||
|
const form = document.getElementById('forgotPasswordForm');
|
||||||
|
const password = document.getElementById('password');
|
||||||
|
const confirmPassword = document.getElementById('confirmPassword');
|
||||||
|
const errorDiv = document.querySelector('.password-match-error');
|
||||||
|
function checkPasswords() {
|
||||||
|
if (confirmPassword.value && password.value !== confirmPassword.value) {
|
||||||
|
errorDiv.classList.add('show');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
errorDiv.classList.remove('show');
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
confirmPassword.addEventListener('input', checkPasswords);
|
||||||
|
password.addEventListener('input', checkPasswords);
|
||||||
|
form.addEventListener('submit', function(e) {
|
||||||
|
if (!checkPasswords()) {
|
||||||
|
e.preventDefault();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@ -127,6 +127,7 @@
|
|||||||
</form>
|
</form>
|
||||||
<div class="login-link">
|
<div class="login-link">
|
||||||
没有账号?<a href="/register">立即注册</a>
|
没有账号?<a href="/register">立即注册</a>
|
||||||
|
<a href="/forgot-password" id="forgotPasswordLink">忘记密码?</a>
|
||||||
</div>
|
</div>
|
||||||
{{ if .error }}
|
{{ if .error }}
|
||||||
<p class="error-message">{{ .error }}</p>
|
<p class="error-message">{{ .error }}</p>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user