:feature: 更新微信状态显示逻辑,添加设备绑定提示,并优化消息结构
Some checks failed
BuildImage / build-image (push) Has been cancelled

This commit is contained in:
李寻欢 2025-04-22 20:59:25 +08:00
parent b256d82855
commit fb94ce31a6
4 changed files with 29 additions and 33 deletions

View File

@ -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 {

View File

@ -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 指定表名

View File

@ -114,7 +114,13 @@
<!-- 操作区域 -->
<div class="bg-gray-50 px-6 py-3 flex justify-between items-center">
<span class="text-xs text-gray-500">微信状态: {{if eq .Status "online"}}在线{{else}}离线{{end}}</span>
<span class="text-xs text-gray-500">
{{if .WechatID}}
微信状态: {{if eq .Status "online"}}在线{{else}}离线{{end}}
{{else}}
初始化完成,待绑定微信账号
{{end}}
</span>
<div class="flex space-x-2">
<a href="/admin/robots/{{.ID}}" class="p-1.5 rounded-md text-gray-600 hover:bg-gray-100 hover:text-gray-900" title="查看详情">
<i class="fas fa-eye"></i>

View File

@ -56,7 +56,11 @@
<div class="flex justify-between text-sm">
<span class="text-gray-600">状态</span>
<span class="{{if eq .Robot.Status "online"}}text-emerald-600{{else if eq .Robot.Status "error"}}text-red-600{{else}}text-gray-600{{end}}">
{{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}}
</span>
</div>
<div class="flex justify-between text-sm">
@ -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) {