增加admin相关页面
This commit is contained in:
@@ -12,6 +12,10 @@ import (
|
||||
"go_blog/pkg/jwt"
|
||||
"go_blog/pkg/util"
|
||||
"go_blog/serializers"
|
||||
"go_blog/themes"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -24,25 +28,43 @@ func UsersLoginHandler(ctx *gin.Context) {
|
||||
response.BadRequest("请求参数错误: " + err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
// 修正:通过数据库查询获取用户记录(原逻辑直接使用 loginUser.GetUser() 未查询数据库)
|
||||
user := &models.Account{Username: loginUser.Username}
|
||||
if err := models.DB.Where("username = ?", user.Username).First(user).Error; err != nil {
|
||||
response.BadRequest("用户不存在")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
// 修正:使用 IsPasswordEqual 验证密码
|
||||
if !user.IsPasswordEqual(loginUser.Password) {
|
||||
response.BadRequest("密码错误")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
token, err := jwt.GenerateToken(user)
|
||||
if err != nil {
|
||||
response.ServerError("生成令牌失败: " + err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 添加 Authorization 响应头(格式与 auth.go 的 AuthRequired 方法一致)
|
||||
ctx.Header("Authorization", "Bearer " + token)
|
||||
|
||||
// 表单提交场景:设置Cookie并跳转(需配合前端使用Cookie存储JWT)
|
||||
if ctx.ContentType() == "application/x-www-form-urlencoded" {
|
||||
http.SetCookie(ctx.Writer, &http.Cookie{
|
||||
Name: "token",
|
||||
Value: token,
|
||||
Path: "/",
|
||||
Expires: time.Now().Add(24 * time.Hour),
|
||||
HttpOnly: true, // 防止XSS
|
||||
})
|
||||
ctx.Redirect(http.StatusFound, "/admin/index")
|
||||
return
|
||||
}
|
||||
|
||||
// API请求场景:返回JSON
|
||||
data, _ := util.PrecisionLost(user)
|
||||
data["token"] = token
|
||||
response.Response(data, nil)
|
||||
@@ -94,7 +116,7 @@ func UsersSetInfoHandler(ctx *gin.Context) {
|
||||
response.ServerError("用户类型错误")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
models.DB.Model(currentUser).Updates(jsonData)
|
||||
response.Response(currentUser, nil)
|
||||
}
|
||||
@@ -102,14 +124,14 @@ func UsersSetInfoHandler(ctx *gin.Context) {
|
||||
// 修改密码
|
||||
func UsersSetPwdHandler(ctx *gin.Context) {
|
||||
response := Response{Ctx: ctx}
|
||||
|
||||
|
||||
// 从上下文中获取用户(替换原 jwt.AssertUser 调用)
|
||||
user, exists := ctx.Get("user")
|
||||
ctxuser, exists := ctx.Get("user")
|
||||
if !exists {
|
||||
response.Unauthenticated("未验证登录")
|
||||
return
|
||||
}
|
||||
currentUser, ok := user.(*models.Account)
|
||||
currentUser, ok := ctxuser.(*models.Account)
|
||||
if !ok {
|
||||
response.ServerError("用户类型错误")
|
||||
return
|
||||
@@ -148,14 +170,64 @@ func UsersListHandler(ctx *gin.Context) {
|
||||
var pager serializers.Pager
|
||||
pager.InitPager(ctx)
|
||||
var users []models.Account
|
||||
|
||||
|
||||
// 先查询总记录数
|
||||
var totalCount int64
|
||||
models.DB.Model(&models.Account{}).Count(&totalCount)
|
||||
pager.Total = int(totalCount) // 正确设置总数
|
||||
|
||||
|
||||
// 分页查询
|
||||
models.DB.Offset(pager.OffSet()).Limit(pager.PageSize).Find(&users)
|
||||
// 由于 pager.OffSet 是 int 类型,直接使用该变量,无需调用函数
|
||||
models.DB.Offset(pager.OffSet).Limit(pager.PageSize).Find(&users)
|
||||
pager.GetPager()
|
||||
response.Response(users, pager)
|
||||
}
|
||||
|
||||
// ShowLoginPage 渲染登录页面
|
||||
func ShowLoginPage(c *gin.Context) {
|
||||
// 直接加载 web/admin 目录下的 login.tmpl 模板(需确保文件存在)
|
||||
tpl, err := template.ParseFiles("web/admin/login.tmpl")
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "加载模板失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusOK)
|
||||
c.Header("Content-Type", "text/html; charset=utf-8")
|
||||
err = tpl.Execute(c.Writer, gin.H{
|
||||
"Title": "用户登录",
|
||||
})
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "渲染模板错误: "+err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// ShowRegisterPage 渲染注册页面
|
||||
func ShowRegisterPage(c *gin.Context) {
|
||||
tm, exists := c.Get("ThemeManager")
|
||||
if !exists {
|
||||
c.String(http.StatusInternalServerError, "Theme manager not found")
|
||||
return
|
||||
}
|
||||
themeManager, ok := tm.(*themes.ThemeManager)
|
||||
if !ok {
|
||||
c.String(http.StatusInternalServerError, "Invalid theme manager type")
|
||||
return
|
||||
}
|
||||
|
||||
// 假设主题中存在 register.tmpl 模板(或使用后台固定模板)
|
||||
tpl := themeManager.GetTemplate("register")
|
||||
if tpl == nil {
|
||||
c.String(http.StatusInternalServerError, "Template 'register' not found in current theme. Make sure 'register.html' or 'register.tmpl' exists.")
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusOK)
|
||||
c.Header("Content-Type", "text/html; charset=utf-8")
|
||||
err := tpl.Execute(c.Writer, gin.H{
|
||||
"Title": "用户注册",
|
||||
})
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "Error rendering template: "+err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user