优化登录逻辑。
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"html/template"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
@@ -21,6 +22,7 @@ type AdminPageData struct {
|
||||
Menus []AdminMenuGroup // 当前角色可见菜单(登录接口同源)
|
||||
PageSet map[string]bool // 菜单中的页面标识集合,模板据此渲染对应页面框架
|
||||
DisplayName string // 侧边栏显示的用户名
|
||||
CurrentUser template.JS // JSON 序列化的当前用户信息(id/username/nickname/role),注入前端
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -92,10 +94,20 @@ func AdminView(c *gin.Context) {
|
||||
if displayName == "" {
|
||||
displayName = user.Username
|
||||
}
|
||||
|
||||
// 将用户基本信息注入前端,避免依赖 localStorage
|
||||
currentUserJSON, _ := json.Marshal(map[string]interface{}{
|
||||
"id": user.ID,
|
||||
"username": user.Username,
|
||||
"nickname": user.Nickname,
|
||||
"role": user.Role,
|
||||
})
|
||||
|
||||
data := AdminPageData{
|
||||
Menus: menus,
|
||||
PageSet: menuPageSet(menus),
|
||||
DisplayName: displayName,
|
||||
CurrentUser: template.JS(currentUserJSON),
|
||||
}
|
||||
|
||||
c.Header("Content-Type", "text/html; charset=utf-8")
|
||||
|
||||
Reference in New Issue
Block a user