From b4bb6654c5616069e88b7f1bb2cc5be98577d403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Wed, 9 Apr 2025 10:22:41 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E4=BC=98=E5=8C=96=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E9=80=BB=E8=BE=91=EF=BC=8C=E5=A2=9E=E5=BC=BA?= =?UTF-8?q?=E5=94=A4=E9=86=92=E7=99=BB=E5=BD=95=E4=BD=93=E9=AA=8C=EF=BC=9B?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E6=97=A7=E7=9A=84=E7=99=BB=E5=87=BA=E6=96=B9?= =?UTF-8?q?=E6=B3=95=EF=BC=8C=E7=AE=80=E5=8C=96=E4=BB=A3=E7=A0=81=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/docker/robot.go | 29 ----- internal/handler/robot.go | 66 ++++++++-- internal/view/robot/index.html | 12 -- internal/view/robot/login.html | 224 +++++++++++++++++++++++---------- internal/view/robot/show.html | 9 +- 5 files changed, 213 insertions(+), 127 deletions(-) diff --git a/internal/docker/robot.go b/internal/docker/robot.go index 5267ad8..61c61b1 100644 --- a/internal/docker/robot.go +++ b/internal/docker/robot.go @@ -259,35 +259,6 @@ func LogOut(ctx context.Context, wxid string, containerHost string) (*AutoHeartb return &response, nil } -// LogoutWechatBot 登出微信机器人(旧方法,使用HTTP请求替代) -func LogoutWechatBot(ctx context.Context, containerID string) error { - // 获取容器IP(简化处理,默认使用localhost) - hostIP := "localhost" - - client := newHTTPClient() - url := fmt.Sprintf("http://%s:3000/api/logout", hostIP) - - var response BaseResponse[any] - resp, err := client.R(). - SetContext(ctx). - SetResult(&response). - Post(url) - - if err != nil { - return fmt.Errorf("failed to execute logout command: %w", err) - } - - if resp.StatusCode() != 200 { - return fmt.Errorf("logout API returned non-200 status code: %d", resp.StatusCode()) - } - - if !response.Success { - return fmt.Errorf("logout failed: %s", response.Message) - } - - return nil -} - // GetWechatBotStatus 获取微信机器人状态(使用HTTP请求替代) func GetWechatBotStatus(ctx context.Context, containerID string) (model.RobotStatus, string, error) { // 检查容器状态 diff --git a/internal/handler/robot.go b/internal/handler/robot.go index fc6d0c0..55a107c 100644 --- a/internal/handler/robot.go +++ b/internal/handler/robot.go @@ -169,7 +169,7 @@ func DeleteRobot(c *fiber.Ctx) error { defer cancel() if robot.Status == model.RobotStatusOnline { - if err = docker.LogoutWechatBot(ctx, robot.ContainerID); err != nil { + if _, err = docker.LogOut(ctx, robot.WechatID, robot.ContainerHost); err != nil { log.Printf("登出机器人失败: %v", err) // 继续删除流程,不因登出失败而中断 } @@ -209,7 +209,7 @@ func DeleteRobot(c *fiber.Ctx) error { return c.Redirect("/admin/robots") } -// RobotLogin 显示微信登录二维码 +// RobotLogin 显示微信登录二维码或唤醒登录 func RobotLogin(c *fiber.Ctx) error { id, err := strconv.Atoi(c.Params("id")) if err != nil { @@ -229,6 +229,45 @@ func RobotLogin(c *fiber.Ctx) error { db.Save(&robot) } + // 检查机器人是否已在线,如果在线则无需登录 + if robot.Status == model.RobotStatusOnline { + // 渲染"已在线"页面 + return c.Render("robot/login", fiber.Map{ + "Title": "微信登录", + "Robot": robot, + "IsOnline": true, + }) + } + + // 检查是否指定使用二维码登录 + forceQrcode := c.Query("qrcode") == "1" + + // 如果存在WechatID且没有强制要求使用二维码,则使用唤醒登录 + if robot.WechatID != "" && !forceQrcode { + // 尝试唤醒登录 + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + awakenResp, err := docker.AwakenLogin(ctx, robot.WechatID, robot.ContainerHost) + if err != nil { + return c.Render("robot/login", fiber.Map{ + "Title": "微信登录", + "Robot": robot, + "Message": "唤醒登录失败: " + err.Error(), + "IsError": true, + }) + } + + // 渲染唤醒登录页面 + return c.Render("robot/login", fiber.Map{ + "Title": "微信登录", + "Robot": robot, + "UUID": awakenResp.Data.QrCodeResponse.Uuid, + "Expired": awakenResp.Data.QrCodeResponse.ExpiredTime, + "IsAwaken": true, + }) + } + // 获取登录二维码 ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() @@ -236,16 +275,22 @@ func RobotLogin(c *fiber.Ctx) error { // 使用新的GetQRCode接口获取二维码,并传递容器访问地址 qrcodeResp, err := docker.GetQRCode(ctx, robot.ContainerHost) if err != nil { - return fiber.NewError(fiber.StatusInternalServerError, "获取登录二维码失败: "+err.Error()) + return c.Render("robot/login", fiber.Map{ + "Title": "微信登录", + "Robot": robot, + "Message": "获取登录二维码失败: " + err.Error(), + "IsError": true, + }) } // 渲染登录页面,包含二维码和UUID(用于后续状态检查) return c.Render("robot/login", fiber.Map{ - "Title": "微信登录", - "Robot": robot, - "QRCode": qrcodeResp.Data.QRCodeURL, // 完整的data URL格式 - "UUID": qrcodeResp.Data.UUID, - "Expired": qrcodeResp.Data.ExpiredTime, + "Title": "微信登录", + "Robot": robot, + "QRCode": qrcodeResp.Data.QRCodeURL, + "UUID": qrcodeResp.Data.UUID, + "Expired": qrcodeResp.Data.ExpiredTime, + "IsAwaken": false, }) } @@ -275,11 +320,6 @@ func RobotLogout(c *fiber.Ctx) error { if _, err = docker.LogOut(ctx, robot.WechatID, robot.ContainerHost); err != nil { return fiber.NewError(fiber.StatusInternalServerError, "登出微信失败: "+err.Error()) } - } else { - // 如果没有WechatID,使用原来的方法 - if err = docker.LogoutWechatBot(ctx, robot.ContainerID); err != nil { - return fiber.NewError(fiber.StatusInternalServerError, "登出微信失败: "+err.Error()) - } } // 更新机器人状态 diff --git a/internal/view/robot/index.html b/internal/view/robot/index.html index 2648c12..c1369a9 100644 --- a/internal/view/robot/index.html +++ b/internal/view/robot/index.html @@ -116,18 +116,6 @@ - {{if eq .Status "offline"}} - - - - {{else}} -
- -
- {{end}} - diff --git a/internal/view/robot/login.html b/internal/view/robot/login.html index c537bba..f496385 100644 --- a/internal/view/robot/login.html +++ b/internal/view/robot/login.html @@ -8,43 +8,122 @@
-
-
-

{{.Robot.Nickname}}

-

请使用微信扫描二维码登录

+ {{if .IsError}} +
+
+
+ +
+
+

{{.Message}}

+
+
+ {{end}} +
-
- Login QR Code -
+ {{if .IsOnline}} + +
+
+
+ WeChat Avatar +
+
+ +
+
+

{{.Robot.Nickname}}

+
+ + + 微信已在线,无需登录 + +
+
-
- - - - + + {{else if .IsAwaken}} + +
+
+
+ WeChat Avatar +
+
+ +
+
+

{{.Robot.Nickname}}

+

请在手机上确认登录

+
+ +
+ + + + + + 正在唤醒 - 等待扫描 - -
+
-

- 请打开微信,使用"扫一扫"功能扫描上方二维码登录 -

+
+
+
+
+
+
+
+
+
-
- 二维码有效期: 120秒 -
+ + {{else}} + +
+ Login QR Code +
- +
+ + + + + + 等待扫描 + +
+ +

+ 请打开微信,使用"扫一扫"功能扫描上方二维码登录 +

+ +
+ 二维码有效期: 120秒 +
+ + + {{end}}
@@ -55,53 +134,31 @@ diff --git a/internal/view/robot/show.html b/internal/view/robot/show.html index 0476cf1..13f588d 100644 --- a/internal/view/robot/show.html +++ b/internal/view/robot/show.html @@ -16,9 +16,6 @@
- - 登录 - {{else}} - - 扫码登录 + + 登录 {{end}}
@@ -263,7 +260,7 @@ `确定要删除机器人"${name}"吗?此操作将永久删除容器及相关数据,无法恢复!`, { type: 'danger', title: '删除机器人' } ); - + if (confirmed) { deleteRobot(id); }