145 lines
3.5 KiB
Go
145 lines
3.5 KiB
Go
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/landing", 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) // 调用公开版本
|
|
}
|