mirror of
https://github.com/skyle1995/NetworkAuth.git
synced 2026-05-25 02:24:05 +08:00
修改项目为前后端分离方案
This commit is contained in:
@@ -6,9 +6,7 @@ import (
|
||||
"NetworkAuth/middleware"
|
||||
"NetworkAuth/models"
|
||||
"NetworkAuth/services"
|
||||
"NetworkAuth/utils"
|
||||
"NetworkAuth/utils/timeutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/spf13/viper"
|
||||
@@ -42,86 +40,12 @@ func formatDBType(dbType string) string {
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 页面处理器
|
||||
// ============================================================================
|
||||
|
||||
// AdminIndexHandler 后台首页处理器/admin 与 /admin/ 根路径入口
|
||||
// - 未登录:重定向到 /admin/login
|
||||
// - 已登录:渲染后台布局页(或重定向到 /admin/layout)
|
||||
// - 自动清理失效的JWT Cookie
|
||||
func AdminIndexHandler(c *gin.Context) {
|
||||
if IsAdminAuthenticatedWithCleanup(c) {
|
||||
// 直接渲染布局页,保持URL为 /admin
|
||||
AdminLayoutHandler(c)
|
||||
return
|
||||
}
|
||||
c.Redirect(http.StatusFound, "/admin/login")
|
||||
}
|
||||
|
||||
// AdminLayoutHandler 后台布局页渲染
|
||||
// - 渲染 layout.html,包含顶部导航、侧边栏与动态内容容器
|
||||
func AdminLayoutHandler(c *gin.Context) {
|
||||
// 获取或生成CSRF令牌
|
||||
var token string
|
||||
if existingToken := utils.GetCSRFTokenFromCookie(c); existingToken != "" {
|
||||
// 重用现有的Cookie令牌
|
||||
token = existingToken
|
||||
} else {
|
||||
// 生成新的CSRF令牌并设置到Cookie
|
||||
newToken, err := utils.GenerateCSRFToken()
|
||||
if err != nil {
|
||||
handlersBaseController.HandleInternalError(c, "生成CSRF令牌失败", err)
|
||||
return
|
||||
}
|
||||
token = newToken
|
||||
utils.SetCSRFToken(c, token)
|
||||
}
|
||||
|
||||
// 准备模板数据
|
||||
data := handlersBaseController.GetDefaultTemplateData()
|
||||
data["CSRFToken"] = token
|
||||
|
||||
// 从数据库读取站点标题,如果失败则使用默认值
|
||||
settingsSvc := services.GetSettingsService()
|
||||
data["Title"] = settingsSvc.GetString("site_title", "后台管理")
|
||||
|
||||
// 合并其他数据(如果有的话)
|
||||
extraData := gin.H{}
|
||||
for key, value := range extraData {
|
||||
data[key] = value
|
||||
}
|
||||
|
||||
c.HTML(http.StatusOK, "layout.html", data)
|
||||
}
|
||||
|
||||
// DashboardFragmentHandler 仪表盘片段渲染
|
||||
// - 展示系统信息:版本、开发模式、数据库类型、启动时长
|
||||
func DashboardFragmentHandler(c *gin.Context) {
|
||||
version := constants.AppVersion
|
||||
mode := middleware.IsDevModeFromContext(c)
|
||||
dbType := viper.GetString("database.type")
|
||||
if dbType == "" {
|
||||
dbType = "sqlite"
|
||||
}
|
||||
uptime := timeutil.GetServerUptimeString()
|
||||
|
||||
data := gin.H{
|
||||
"Version": version,
|
||||
"Mode": mode,
|
||||
"DBType": formatDBType(dbType),
|
||||
"Uptime": uptime,
|
||||
}
|
||||
|
||||
c.HTML(http.StatusOK, "dashboard.html", data)
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// API处理器
|
||||
// ============================================================================
|
||||
|
||||
// SystemInfoHandler 系统信息API接口
|
||||
// - 返回系统运行状态的JSON数据,用于前端定时刷新
|
||||
// 返回系统运行状态的JSON数据,用于前端定时刷新
|
||||
func SystemInfoHandler(c *gin.Context) {
|
||||
version := constants.AppVersion
|
||||
mode := middleware.IsDevModeFromContext(c)
|
||||
@@ -130,19 +54,21 @@ func SystemInfoHandler(c *gin.Context) {
|
||||
dbType = "sqlite"
|
||||
}
|
||||
uptime := timeutil.GetServerUptimeString()
|
||||
uptimeSeconds := int64(timeutil.GetServerUptime().Seconds())
|
||||
|
||||
data := gin.H{
|
||||
"version": version,
|
||||
"mode": mode,
|
||||
"db_type": formatDBType(dbType),
|
||||
"uptime": uptime,
|
||||
"version": version,
|
||||
"mode": mode,
|
||||
"db_type": formatDBType(dbType),
|
||||
"uptime": uptime,
|
||||
"uptime_seconds": uptimeSeconds,
|
||||
}
|
||||
|
||||
handlersBaseController.HandleSuccess(c, "ok", data)
|
||||
}
|
||||
|
||||
// DashboardStatsHandler 仪表盘统计数据API接口
|
||||
// - 返回应用统计数据的JSON数据,包括全部/启用/禁用/变量数量
|
||||
// - 返回应用统计数据的JSON数据,包括全部/启用/变量数量
|
||||
func DashboardStatsHandler(c *gin.Context) {
|
||||
// 获取数据库连接
|
||||
db, ok := handlersBaseController.GetDB(c)
|
||||
@@ -152,8 +78,7 @@ func DashboardStatsHandler(c *gin.Context) {
|
||||
|
||||
// 统计应用数据
|
||||
var totalApps int64
|
||||
var enabledApps int64
|
||||
var disabledApps int64
|
||||
var totalFunctions int64
|
||||
var totalVariables int64
|
||||
|
||||
// 统计全部应用数量
|
||||
@@ -162,15 +87,9 @@ func DashboardStatsHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 统计启用应用数量
|
||||
if err := db.Model(&models.App{}).Where("status = ?", 1).Count(&enabledApps).Error; err != nil {
|
||||
handlersBaseController.HandleInternalError(c, "统计启用应用数量失败", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 统计禁用应用数量
|
||||
if err := db.Model(&models.App{}).Where("status = ?", 0).Count(&disabledApps).Error; err != nil {
|
||||
handlersBaseController.HandleInternalError(c, "统计禁用应用数量失败", err)
|
||||
// 统计函数数量
|
||||
if err := db.Model(&models.Function{}).Count(&totalFunctions).Error; err != nil {
|
||||
handlersBaseController.HandleInternalError(c, "统计函数数量失败", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -182,8 +101,7 @@ func DashboardStatsHandler(c *gin.Context) {
|
||||
|
||||
data := gin.H{
|
||||
"total_apps": totalApps,
|
||||
"enabled_apps": enabledApps,
|
||||
"disabled_apps": disabledApps,
|
||||
"total_functions": totalFunctions,
|
||||
"total_variables": totalVariables,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user