From 80a3de99062a9718c98bea628b16b8ac425d28e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Wed, 9 Apr 2025 14:47:04 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=B8=85=E7=90=86=E4=B8=BB=E9=A1=B5?= =?UTF-8?q?=E5=92=8C=E6=9C=8D=E5=8A=A1=E5=99=A8=E4=BB=A3=E7=A0=81=EF=BC=8C?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E6=9C=AA=E4=BD=BF=E7=94=A8=E7=9A=84=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=92=8C=E8=B0=83=E8=AF=95=E6=97=A5=E5=BF=97=EF=BC=9B?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/handler/home.go | 130 -------------------------------------- internal/server/server.go | 28 ++------ 2 files changed, 4 insertions(+), 154 deletions(-) diff --git a/internal/handler/home.go b/internal/handler/home.go index 1e5126f..f16e682 100644 --- a/internal/handler/home.go +++ b/internal/handler/home.go @@ -1,144 +1,14 @@ package handler import ( - "fmt" - "time" - "github.com/gofiber/fiber/v2" - - "gitee.ltd/lxh/wechat-robot/internal/middleware" - "gitee.ltd/lxh/wechat-robot/internal/model" ) // Home 主页处理函数 func Home(c *fiber.Ctx) error { - // 如果用户已登录,重定向到机器人列表页面 - if middleware.IsAuthenticated(c) { - return c.Redirect("/admin/robots") - } - // 未登录用户显示官网页面 return c.Render("home/index", fiber.Map{ "Title": "微信机器人管理系统", "NoLayout": true, // 使用特殊的官网布局,而非后台布局 }) } - -// SystemStats 系统统计数据 -type SystemStats struct { - TotalRobots int64 - OnlineRobots int64 - OfflineRobots int64 - ErrorRobots int64 - TotalContacts int64 - TotalMessages int64 - TotalGroups int64 - SystemUptime string -} - -// 获取系统统计信息 -func getSystemStats() SystemStats { - db := model.GetDB() - stats := SystemStats{} - - // 计算机器人总数 - db.Model(&model.Robot{}).Count(&stats.TotalRobots) - - // 计算在线机器人数 - db.Model(&model.Robot{}).Where("status = ?", model.RobotStatusOnline).Count(&stats.OnlineRobots) - - // 计算离线机器人数 - db.Model(&model.Robot{}).Where("status = ?", model.RobotStatusOffline).Count(&stats.OfflineRobots) - - // 计算错误状态机器人数 - db.Model(&model.Robot{}).Where("status = ?", model.RobotStatusError).Count(&stats.ErrorRobots) - - // 计算联系人总数 - db.Model(&model.Contact{}).Count(&stats.TotalContacts) - - // 计算消息总数 - db.Model(&model.Message{}).Count(&stats.TotalMessages) - - // 计算群组总数 - db.Model(&model.Contact{}).Where("type = ?", model.ContactTypeGroup).Count(&stats.TotalGroups) - - // 模拟系统运行时间 - stats.SystemUptime = "3天12小时" - - return stats -} - -// Activity 活动记录 -type Activity struct { - Title string - Description string - Time string - Icon string -} - -// 获取最近活动 -func getRecentActivities() []Activity { - db := model.GetDB() - activities := []Activity{} - - // 查询最近登录的机器人 - var recentRobots []model.Robot - db.Where("last_login_at IS NOT NULL").Order("last_login_at desc").Limit(3).Find(&recentRobots) - - for _, robot := range recentRobots { - loginTime := "未知时间" - if robot.LastLoginAt != nil { - loginTime = timeSince(*robot.LastLoginAt) - } - - activities = append(activities, Activity{ - Title: robot.Nickname + " 登录成功", - Description: "机器人 " + robot.Nickname + " 已成功登录微信", - Time: loginTime, - Icon: "sign-in-alt", - }) - } - - // 如果活动太少,添加一些默认活动 - if len(activities) < 3 { - activities = append(activities, Activity{ - Title: "系统更新", - Description: "微信机器人管理系统已更新到最新版本", - Time: "2天前", - Icon: "sync", - }) - } - - return activities -} - -// TimeSince 计算过去的时间(多久以前) - 改为导出函数(大写开头) -func TimeSince(t time.Time) string { - now := time.Now() - duration := now.Sub(t) - - if duration.Hours() < 24 { - if duration.Hours() < 1 { - return "刚刚" - } - return fmt.Sprintf("%.0f小时前", duration.Hours()) - } - - days := int(duration.Hours() / 24) - if days < 30 { - return fmt.Sprintf("%d天前", days) - } - - months := days / 30 - if months < 12 { - return fmt.Sprintf("%d个月前", months) - } - - years := months / 12 - return fmt.Sprintf("%d年前", years) -} - -// 保留旧的私有函数以兼容现有调用 -func timeSince(t time.Time) string { - return TimeSince(t) // 调用公开版本 -} diff --git a/internal/server/server.go b/internal/server/server.go index 62c5823..07b855c 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -1,10 +1,10 @@ package server import ( + "errors" "fmt" "log" "os" - "path/filepath" "strings" "github.com/goccy/go-json" @@ -34,24 +34,6 @@ func New(cfg *config.Config) *Server { log.Fatalf("视图目录不存在: %s", viewsDir) } - // 调试模式下输出所有模板文件 - if cfg.Server.Env == "development" { - log.Println("正在加载模板文件...") - err := filepath.Walk(viewsDir, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - if !info.IsDir() && strings.HasSuffix(path, ".html") { - relPath, _ := filepath.Rel(viewsDir, path) - log.Printf("找到模板: %s", relPath) - } - return nil - }) - if err != nil { - log.Printf("遍历模板文件失败: %v", err) - } - } - // 初始化模板引擎 engine := html.New(viewsDir, ".html") engine.Reload(cfg.Server.Env == "development") // 开发环境下启用热重载 @@ -62,9 +44,6 @@ func New(cfg *config.Config) *Server { return a - b }) - // 添加时间格式化函数 - engine.AddFunc("timeSince", handler.TimeSince) - // 添加map函数,用于在模板中创建映射 engine.AddFunc("map", func(values ...interface{}) map[string]interface{} { if len(values)%2 != 0 { @@ -93,7 +72,8 @@ func New(cfg *config.Config) *Server { log.Printf("错误: %+v\n请求路径: %s\n", err, c.Path()) code := fiber.StatusInternalServerError - if e, ok := err.(*fiber.Error); ok { + var e *fiber.Error + if errors.As(err, &e) { code = e.Code } @@ -118,7 +98,7 @@ func New(cfg *config.Config) *Server { // 注册中间件 app.Use(recover.New()) app.Use(logger.New(logger.Config{ - Format: "[${time}] ${status} - ${ips} - ${latency} ${method} ${path}\n", + Format: "[${time}] ${status} - ${ip} - ${latency} ${method} ${path}\n", TimeFormat: "2006-01-02 15:04:05", })) app.Use(cors.New())