- 管理后台帖子与用户审核操作中添加失败错误重定向处理 - 管理后台帖子、用户、评论删除操作中添加错误检查及提示 - 用户角色更新操作失败时添加错误重定向 - 用户封禁通知失败时添加相应错误提示 - 登录登出时session保存加入错误处理 - 讨论区上传目录创建失败时显示错误提示 - 移除admin_dashboard.html多余样式及修正侧边栏当前页高亮 - admin_posts.html和admin_users.html添加状态样式动态使用的隐藏span元素 - admin_users.html为角色选择添加label以提升无障碍性 - 升级项目依赖版本,包含gin、gorm、validator等核心库版本更新
107 lines
2.7 KiB
Go
107 lines
2.7 KiB
Go
package controllers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"lv8girl/internal/services"
|
|
|
|
"github.com/gin-contrib/sessions"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type AuthController struct {
|
|
authService *services.AuthService
|
|
}
|
|
|
|
func NewAuthController() *AuthController {
|
|
return &AuthController{
|
|
authService: services.NewAuthService(),
|
|
}
|
|
}
|
|
|
|
func (c *AuthController) ShowLogin(ctx *gin.Context) {
|
|
session := sessions.Default(ctx)
|
|
if session.Get("user_id") != nil {
|
|
ctx.Redirect(http.StatusFound, "/")
|
|
return
|
|
}
|
|
ctx.HTML(http.StatusOK, "login.html", gin.H{"Error": ""})
|
|
}
|
|
|
|
func (c *AuthController) Login(ctx *gin.Context) {
|
|
session := sessions.Default(ctx)
|
|
if session.Get("user_id") != nil {
|
|
ctx.Redirect(http.StatusFound, "/")
|
|
return
|
|
}
|
|
|
|
login := ctx.PostForm("login")
|
|
password := ctx.PostForm("password")
|
|
|
|
if login == "" || password == "" {
|
|
ctx.HTML(http.StatusOK, "login.html", gin.H{"Error": "请输入用户名/邮箱和密码"})
|
|
return
|
|
}
|
|
|
|
result := c.authService.Login(login, password)
|
|
if !result.Success {
|
|
ctx.HTML(http.StatusOK, "login.html", gin.H{"Error": result.Error})
|
|
return
|
|
}
|
|
|
|
session.Set("user_id", result.User.ID)
|
|
session.Set("username", result.User.Username)
|
|
session.Set("user_role", result.User.Role)
|
|
if err := session.Save(); err != nil {
|
|
ctx.HTML(http.StatusOK, "login.html", gin.H{"Error": "登录失败,请重试"})
|
|
return
|
|
}
|
|
|
|
ctx.Redirect(http.StatusFound, "/")
|
|
}
|
|
|
|
func (c *AuthController) ShowRegister(ctx *gin.Context) {
|
|
session := sessions.Default(ctx)
|
|
if session.Get("user_id") != nil {
|
|
ctx.Redirect(http.StatusFound, "/")
|
|
return
|
|
}
|
|
ctx.HTML(http.StatusOK, "register.html", gin.H{"Error": "", "Success": ""})
|
|
}
|
|
|
|
func (c *AuthController) Register(ctx *gin.Context) {
|
|
session := sessions.Default(ctx)
|
|
if session.Get("user_id") != nil {
|
|
ctx.Redirect(http.StatusFound, "/")
|
|
return
|
|
}
|
|
|
|
username := ctx.PostForm("username")
|
|
email := ctx.PostForm("email")
|
|
password := ctx.PostForm("password")
|
|
confirmPassword := ctx.PostForm("confirm_password")
|
|
|
|
if password != confirmPassword {
|
|
ctx.HTML(http.StatusOK, "register.html", gin.H{"Error": "两次输入的密码不一致", "Success": ""})
|
|
return
|
|
}
|
|
|
|
result := c.authService.Register(username, email, password)
|
|
if !result.Success {
|
|
ctx.HTML(http.StatusOK, "register.html", gin.H{"Error": result.Error, "Success": ""})
|
|
return
|
|
}
|
|
|
|
ctx.HTML(http.StatusOK, "register.html", gin.H{"Error": "", "Success": "注册成功!您的账号正在等待管理员审核,请耐心等待。"})
|
|
}
|
|
|
|
func (c *AuthController) Logout(ctx *gin.Context) {
|
|
session := sessions.Default(ctx)
|
|
session.Clear()
|
|
if err := session.Save(); err != nil {
|
|
ctx.String(http.StatusInternalServerError, "退出失败")
|
|
return
|
|
}
|
|
ctx.Redirect(http.StatusFound, "/")
|
|
}
|