From fb94ce31a60cc32a62db478ce26c953e6afdd56e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Tue, 22 Apr 2025 20:59:25 +0800 Subject: [PATCH] =?UTF-8?q?:feature:=20=E6=9B=B4=E6=96=B0=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E7=8A=B6=E6=80=81=E6=98=BE=E7=A4=BA=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E8=AE=BE=E5=A4=87=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=EF=BC=8C=E5=B9=B6=E4=BC=98=E5=8C=96=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/handler/robot.go | 6 ++---- internal/model/message.go | 14 +------------- internal/view/robot/index.html | 8 +++++++- internal/view/robot/show.html | 34 +++++++++++++++++++--------------- 4 files changed, 29 insertions(+), 33 deletions(-) diff --git a/internal/handler/robot.go b/internal/handler/robot.go index 4f18c99..ab6edf3 100644 --- a/internal/handler/robot.go +++ b/internal/handler/robot.go @@ -95,6 +95,8 @@ func CreateRobot(c *fiber.Ctx) error { Nickname: robotName, Status: model.RobotStatusOffline, } + // 处理一下设备 Id 和设备名称,防止后面出现空值 + robot.CheckDevice() db := model.GetDB() if err = db.Create(&robot).Error; err != nil { @@ -230,10 +232,6 @@ func RobotLogin(c *fiber.Ctx) error { } return fiber.NewError(fiber.StatusInternalServerError, "查询数据库失败") } - // 处理一下设备信息 - if robot.CheckDevice() { - db.Save(&robot) - } // 检查机器人是否已在线,如果在线则无需登录 if robot.Status == model.RobotStatusOnline { diff --git a/internal/model/message.go b/internal/model/message.go index 812257f..a020a50 100644 --- a/internal/model/message.go +++ b/internal/model/message.go @@ -5,19 +5,6 @@ import ( "time" ) -// ContentType 表示消息类型的枚举 -type ContentType string - -const ( - ContentTypeText ContentType = "text" - ContentTypeImage ContentType = "image" - ContentTypeVoice ContentType = "voice" - ContentTypeVideo ContentType = "video" - ContentTypeFile ContentType = "file" - ContentTypeLocation ContentType = "location" - ContentTypeSystem ContentType = "system" -) - // Message 表示微信消息 type Message struct { BaseModel @@ -32,6 +19,7 @@ type Message struct { FromUser string // 发送者 GroupUser string // 群成员 ToUser string // 接收者 + FileUrl string `gorm:"column:file_url"` // 文件地址 } // TableName 指定表名 diff --git a/internal/view/robot/index.html b/internal/view/robot/index.html index a4340fd..f79cfe7 100644 --- a/internal/view/robot/index.html +++ b/internal/view/robot/index.html @@ -114,7 +114,13 @@
- 微信状态: {{if eq .Status "online"}}在线{{else}}离线{{end}} + + {{if .WechatID}} + 微信状态: {{if eq .Status "online"}}在线{{else}}离线{{end}} + {{else}} + 初始化完成,待绑定微信账号 + {{end}} +
diff --git a/internal/view/robot/show.html b/internal/view/robot/show.html index f6241a3..924e8d4 100644 --- a/internal/view/robot/show.html +++ b/internal/view/robot/show.html @@ -56,7 +56,11 @@
状态 - {{if eq .Robot.Status "online"}}在线{{else if eq .Robot.Status "error"}}错误{{else}}离线{{end}} + {{if .robot.WechatID}} + {{if eq .Robot.Status "online"}}在线{{else if eq .Robot.Status "error"}}错误{{else}}离线{{end}} + {{else}} + 待绑定微信号 + {{end}}
@@ -190,7 +194,7 @@ // 容器ID可能长度不一致,确保使用完整ID const containerId = "{{.Robot.ContainerID}}"; - + // WebSocket连接 let logsSocket = null; @@ -198,43 +202,43 @@ function connectLogsWebSocket() { logsLoading.style.display = 'flex'; containerLogs.textContent = '正在连接到WebSocket...'; - + // 关闭已存在的WebSocket连接 if (logsSocket) { logsSocket.close(); } - + // 创建新的WebSocket连接 const wsProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; const wsUrl = `${wsProtocol}//${window.location.host}/api/v1/ws/containers/${containerId}/logs`; - + logsSocket = new WebSocket(wsUrl); - + // 连接打开时 logsSocket.onopen = function() { containerLogs.textContent = '已连接,等待日志数据...'; logsLoading.style.display = 'none'; }; - + // 收到消息时 logsSocket.onmessage = function(event) { // 如果是第一条消息,清空容器 if (containerLogs.textContent === '已连接,等待日志数据...') { containerLogs.textContent = ''; } - + // 添加新日志 containerLogs.textContent += event.data; - + // 自动滚动到底部 containerLogs.scrollTop = containerLogs.scrollHeight; }; - + // 连接关闭时 logsSocket.onclose = function() { containerLogs.textContent += '\n\n连接已关闭。点击刷新按钮重新连接。'; }; - + // 发生错误时 logsSocket.onerror = function(error) { containerLogs.textContent = '连接错误: ' + JSON.stringify(error); @@ -252,7 +256,7 @@ if (data.success && data.logs) { containerLogs.textContent = data.logs; containerLogs.scrollTop = containerLogs.scrollHeight; - + // 加载历史日志后连接WebSocket获取实时日志 connectLogsWebSocket(); } else { @@ -327,7 +331,7 @@ // 重新加载初始日志,然后会自动重连WebSocket loadInitialLogs(); }); - + logLines.addEventListener('change', function() { // 先断开WebSocket连接 if (logsSocket) { @@ -336,7 +340,7 @@ // 重新加载初始日志,然后会自动重连WebSocket loadInitialLogs(); }); - + refreshBtn.addEventListener('click', function() { this.classList.add('animate-spin'); loadStats(); @@ -347,7 +351,7 @@ // 定期更新状态 setInterval(loadStats, 5000); // 每5秒更新一次 - + // 页面关闭时断开WebSocket连接 window.addEventListener('beforeunload', function() { if (logsSocket) {