This repository has been archived on 2026-06-21. You can view files and clone it, but cannot push or open issues or pull requests.
Files
MetaLab/internal/common/helper.go
Victor_Jay 3937945b43 feat(admin): 管理首页标题修正、角色中文显示及用户总数补全
- 修正 admin layout title 后缀为「MetaLab 管理面板」
- 当前角色从原始字符串改为中文显示(注入 RoleDisplayName)
- 补全管理首页「用户总数」统计,新增 CountUsers 方法
- 遵循 ISP 最小接口,Service 复用已有 CountSearchUsers
2026-05-26 23:21:32 +08:00

50 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package common
import (
"metazone.cc/metalab/internal/model"
"github.com/gin-gonic/gin"
)
// BuildPageData 构建页面模板数据,自动注入登录状态与 CSRF token
func BuildPageData(c *gin.Context, extra gin.H) gin.H {
data := gin.H{}
for k, v := range extra {
data[k] = v
}
if username, exists := c.Get("username"); exists {
data["IsLoggedIn"] = true
data["Username"] = username
}
// 注入 CSRF token由 CSRF 中间件设置到上下文中)
if token, exists := c.Get("csrf_token"); exists {
data["CSRFToken"] = token
}
return data
}
// BuildAdminPageData 构建管理后台模板数据
// 额外注入 UID、Role、CanManageUsers、CurrentPath 等权限信息
func BuildAdminPageData(c *gin.Context, extra gin.H) gin.H {
data := BuildPageData(c, extra)
data["IsAdmin"] = true
data["CurrentPath"] = c.Request.URL.Path
if uid, exists := c.Get("uid"); exists {
data["UID"] = uid
}
if role, exists := c.Get("role"); exists {
roleStr := role.(string)
data["Role"] = roleStr
if name, ok := model.RoleDisplayNames[roleStr]; ok {
data["RoleDisplayName"] = name
} else {
data["RoleDisplayName"] = roleStr
}
data["CanManageUsers"] = model.HasMinRole(roleStr, model.RoleAdmin)
data["CanManageSettings"] = model.HasMinRole(roleStr, model.RoleOwner)
data["IsOwner"] = model.HasMinRole(roleStr, model.RoleOwner)
}
return data
}