2025-03-27 16:27:41 +08:00

147 lines
3.6 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"
)
// APIResponse API响应结构
type APIResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
// Success 返回成功响应
func Success(data interface{}) APIResponse {
return APIResponse{
Success: true,
Data: data,
}
}
// Error 返回错误响应
func Error(message string) APIResponse {
return APIResponse{
Success: false,
Message: message,
}
}
// APIListRobots API获取机器人列表
func APIListRobots(c *fiber.Ctx) error {
db := model.GetDB()
var robots []model.Robot
if err := db.Find(&robots).Error; err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(Error("获取机器人列表失败"))
}
return c.JSON(Success(robots))
}
// APIGetRobotStatus API获取机器人状态
func APIGetRobotStatus(c *fiber.Ctx) error {
id, err := strconv.Atoi(c.Params("id"))
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(Error("无效的ID"))
}
var robot model.Robot
db := model.GetDB()
if err := db.First(&robot, id).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return c.Status(fiber.StatusNotFound).JSON(Error("机器人不存在"))
}
return c.Status(fiber.StatusInternalServerError).JSON(Error("查询数据库失败"))
}
// 如果机器人在线,实时获取状态
if robot.Status == model.RobotStatusOnline {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
status, errMsg, err := docker.GetWechatBotStatus(ctx, robot.ContainerID)
if err != nil {
return c.JSON(Success(map[string]interface{}{
"robot": robot,
"status": string(robot.Status),
}))
}
robot.Status = status
robot.ErrorMessage = errMsg
// 更新状态到数据库
db.Save(&robot)
}
return c.JSON(Success(map[string]interface{}{
"robot": robot,
"status": string(robot.Status),
}))
}
// APIGetRobotContacts API获取机器人联系人
func APIGetRobotContacts(c *fiber.Ctx) error {
id, err := strconv.Atoi(c.Params("id"))
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(Error("无效的ID"))
}
var robot model.Robot
db := model.GetDB()
if err := db.First(&robot, id).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return c.Status(fiber.StatusNotFound).JSON(Error("机器人不存在"))
}
return c.Status(fiber.StatusInternalServerError).JSON(Error("查询数据库失败"))
}
var contacts []model.Contact
if err := db.Where("robot_id = ?", id).Find(&contacts).Error; err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(Error("获取联系人列表失败"))
}
return c.JSON(Success(contacts))
}
// APIGetMessages API获取聊天记录
func APIGetMessages(c *fiber.Ctx) error {
robotID, err := strconv.Atoi(c.Params("id"))
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(Error("无效的机器人ID"))
}
contactID, err := strconv.Atoi(c.Params("contactId"))
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(Error("无效的联系人ID"))
}
limit, _ := strconv.Atoi(c.Query("limit", "20"))
if limit > 100 {
limit = 100
}
db := model.GetDB()
var messages []model.Message
if err := db.Where("robot_id = ? AND contact_id = ?", robotID, contactID).
Order("send_time DESC").
Limit(limit).
Find(&messages).Error; err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(Error("获取聊天记录失败"))
}
return c.JSON(Success(messages))
}