82 lines
1.9 KiB
Go
82 lines
1.9 KiB
Go
package handler
|
||
|
||
import (
|
||
"context"
|
||
"strconv"
|
||
"time"
|
||
|
||
"github.com/gofiber/fiber/v2"
|
||
"gorm.io/gorm"
|
||
|
||
"gitee.ltd/lxh/wechat-robot/internal/docker"
|
||
"gitee.ltd/lxh/wechat-robot/internal/model"
|
||
)
|
||
|
||
// CheckQRCodeStatus 检查二维码状态
|
||
func CheckQRCodeStatus(c *fiber.Ctx) error {
|
||
id, err := strconv.Atoi(c.Params("id"))
|
||
if err != nil {
|
||
return c.JSON(fiber.Map{
|
||
"success": false,
|
||
"message": "无效的机器人ID",
|
||
})
|
||
}
|
||
|
||
uuid := c.Query("uuid")
|
||
if uuid == "" {
|
||
return c.JSON(fiber.Map{
|
||
"success": false,
|
||
"message": "UUID参数缺失",
|
||
})
|
||
}
|
||
|
||
// 获取机器人实例
|
||
db := model.GetDB()
|
||
var robot model.Robot
|
||
if err := db.First(&robot, id).Error; err != nil {
|
||
if err == gorm.ErrRecordNotFound {
|
||
return c.JSON(fiber.Map{
|
||
"success": false,
|
||
"message": "机器人不存在",
|
||
})
|
||
}
|
||
return c.JSON(fiber.Map{
|
||
"success": false,
|
||
"message": "查询数据库失败",
|
||
})
|
||
}
|
||
|
||
// 调用CheckUuid API检查二维码状态
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||
defer cancel()
|
||
|
||
// 调用CheckUuid API检查二维码状态,传递容器访问地址
|
||
response, err := docker.CheckUuid(ctx, robot.ContainerID, uuid, robot.ContainerHost)
|
||
if err != nil {
|
||
return c.JSON(fiber.Map{
|
||
"success": false,
|
||
"message": "检查二维码状态失败: " + err.Error(),
|
||
})
|
||
}
|
||
|
||
// 如果检测到已登录,更新机器人状态
|
||
if response.Success && response.Data.Status == 2 {
|
||
// 开启自动心跳,传递容器访问地址
|
||
if robot.WechatID != "" {
|
||
_, _ = docker.AutoHeartbeatStart(ctx, robot.ContainerID, robot.WechatID, robot.ContainerHost)
|
||
}
|
||
|
||
// 更新机器人状态
|
||
robot.Status = model.RobotStatusOnline
|
||
now := time.Now()
|
||
robot.LastLoginAt = &now
|
||
db.Save(&robot)
|
||
}
|
||
|
||
return c.JSON(fiber.Map{
|
||
"success": response.Success,
|
||
"status": response.Data.Status,
|
||
"message": response.Message,
|
||
})
|
||
}
|