李寻欢 080fdb66ab
All checks were successful
BuildImage / build-image (push) Successful in 2m2s
🎨 添加定时任务调度,优化消息同步逻辑,更新相关API接口调用
2025-04-07 16:08:55 +08:00

106 lines
2.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package handler
import (
"context"
"encoding/json"
"errors"
"gitee.ltd/lxh/wechat-robot/internal/config"
"gitee.ltd/lxh/wechat-robot/internal/tasks"
"github.com/gofiber/fiber/v2/log"
"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 errors.Is(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, uuid, robot.ContainerHost)
if err != nil {
return c.JSON(fiber.Map{
"success": false,
"message": "检查二维码状态失败: " + err.Error(),
})
}
if cfg, _ := config.Load(); cfg.Server.Env == "development" {
bs, _ := json.Marshal(response)
log.Debugf("扫码返回结果: %s", bs)
}
// 如果返回status=1表示已扫码暂存一下昵称和头像
if response.Data.Status == 1 {
robot.Nickname = response.Data.NickName
robot.Avatar = response.Data.HeadImgUrl
db.Save(&robot)
}
// 如果检测到已登录,更新机器人状态
if response.Success && response.Data.AcctSectResp != nil {
response.Data.Status = 99
robot.WechatID = response.Data.AcctSectResp["userName"].(string)
// 开启自动心跳,传递容器访问地址
if robot.WechatID != "" {
_, _ = docker.AutoHeartbeatStart(ctx, robot.WechatID, robot.ContainerHost)
}
// 更新机器人状态
robot.Status = model.RobotStatusOnline
now := time.Now()
robot.LastLoginAt = &now
db.Save(&robot)
// 添加定时任务
tasks.AddJob(robot)
}
return c.JSON(fiber.Map{
"success": response.Success,
"status": response.Data.Status,
"message": response.Message,
"userInfo": response.Data.AcctSectResp,
})
}