🎨 清理主页和服务器代码,移除未使用的函数和调试日志;优化错误处理逻辑
All checks were successful
BuildImage / build-image (push) Successful in 2m4s
All checks were successful
BuildImage / build-image (push) Successful in 2m4s
This commit is contained in:
parent
224885c439
commit
80a3de9906
@ -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) // 调用公开版本
|
||||
}
|
||||
|
@ -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())
|
||||
|
Loading…
x
Reference in New Issue
Block a user