|
4 | 4 | "context" |
5 | 5 | "encoding/json" |
6 | 6 | "fmt" |
7 | | - |
8 | 7 | "github.com/sirupsen/logrus" |
| 8 | + "strings" |
| 9 | + "time" |
9 | 10 | ) |
10 | 11 |
|
11 | 12 | // MCP 工具处理函数 |
@@ -34,6 +35,46 @@ func (s *AppServer) handleCheckLoginStatus(ctx context.Context) *MCPToolResult { |
34 | 35 | } |
35 | 36 | } |
36 | 37 |
|
| 38 | +// handleGetLoginQrcode 处理获取登录二维码请求。 |
| 39 | +// 返回二维码图片的 Base64 编码和超时时间,供前端展示扫码登录。 |
| 40 | +func (s *AppServer) handleGetLoginQrcode(ctx context.Context) *MCPToolResult { |
| 41 | + logrus.Info("MCP: 获取登录扫码图片") |
| 42 | + |
| 43 | + result, err := s.xiaohongshuService.GetLoginQrcode(ctx) |
| 44 | + if err != nil { |
| 45 | + return &MCPToolResult{ |
| 46 | + Content: []MCPContent{{Type: "text", Text: "获取登录扫码图片失败: " + err.Error()}}, |
| 47 | + IsError: true, |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if result.IsLoggedIn { |
| 52 | + return &MCPToolResult{ |
| 53 | + Content: []MCPContent{{Type: "text", Text: "你当前已处于登录状态"}}, |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + now := time.Now() |
| 58 | + deadline := func() string { |
| 59 | + d, err := time.ParseDuration(result.Timeout) |
| 60 | + if err != nil { |
| 61 | + return now.Format("2006-01-02 15:04:05") |
| 62 | + } |
| 63 | + return now.Add(d).Format("2006-01-02 15:04:05") |
| 64 | + }() |
| 65 | + |
| 66 | + // 已登录:文本 + 图片 |
| 67 | + contents := []MCPContent{ |
| 68 | + {Type: "text", Text: "请用小红书 App 在 " + deadline + " 前扫码登录 👇"}, |
| 69 | + { |
| 70 | + Type: "image", |
| 71 | + MimeType: "image/png", |
| 72 | + Data: strings.TrimPrefix(result.Img, "data:image/png;base64,"), |
| 73 | + }, |
| 74 | + } |
| 75 | + return &MCPToolResult{Content: contents} |
| 76 | +} |
| 77 | + |
37 | 78 | // handlePublishContent 处理发布内容 |
38 | 79 | func (s *AppServer) handlePublishContent(ctx context.Context, args map[string]interface{}) *MCPToolResult { |
39 | 80 | logrus.Info("MCP: 发布内容") |
|
0 commit comments