Files
ai_wht_wechat/go_backend/service/xhs_service.go

166 lines
4.1 KiB
Go
Raw Normal View History

2025-12-19 22:36:48 +08:00
package service
import (
"bytes"
"encoding/json"
"fmt"
"log"
"os/exec"
"path/filepath"
)
type XHSService struct{}
// SendCodeRequest 发送验证码请求
type SendCodeRequest struct {
Phone string `json:"phone" binding:"required"`
CountryCode string `json:"country_code"`
}
// SendCodeResponse 发送验证码响应
type SendCodeResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Data map[string]interface{} `json:"data"`
}
// LoginRequest 登录请求
type LoginRequest struct {
Phone string `json:"phone" binding:"required"`
Code string `json:"code" binding:"required"`
CountryCode string `json:"country_code"`
}
// LoginResponse 登录响应
type LoginResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Data map[string]interface{} `json:"data"`
}
// SendVerificationCode 调用Python脚本发送验证码
func (s *XHSService) SendVerificationCode(phone, countryCode string) (*SendCodeResponse, error) {
// 如果没有传国家码,默认使用+86
if countryCode == "" {
countryCode = "+86"
}
// 获取Python脚本路径和venv中的Python解释器
backendDir := filepath.Join("..", "backend")
pythonScript := filepath.Join(backendDir, "xhs_cli.py")
// 使用venv中的Python解释器 (跨平台)
pythonCmd := getPythonPath(backendDir)
// 执行Python脚本
cmd := exec.Command(pythonCmd, pythonScript, "send_code", phone, countryCode)
// 设置工作目录为Python脚本所在目录
cmd.Dir = backendDir
// 捕获输出
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
// 执行命令
err := cmd.Run()
// 打印Python脚本的日志输出stderr
if stderr.Len() > 0 {
log.Printf("[Python日志-发送验证码] %s", stderr.String())
}
if err != nil {
return nil, fmt.Errorf("执行Python脚本失败: %w, stderr: %s", err, stderr.String())
}
// 获取UTF-8编码的输出
outputStr := stdout.String()
// 解析JSON输出
var result SendCodeResponse
if err := json.Unmarshal([]byte(outputStr), &result); err != nil {
return nil, fmt.Errorf("解析Python输出失败: %w, output: %s", err, outputStr)
}
// 检查Python脚本返回的success字段
if !result.Data["success"].(bool) {
return &SendCodeResponse{
Code: 1,
Message: result.Data["error"].(string),
}, nil
}
return &SendCodeResponse{
Code: 0,
Message: "验证码已发送",
Data: result.Data,
}, nil
}
// VerifyLogin 调用Python脚本验证登录
func (s *XHSService) VerifyLogin(phone, code, countryCode string) (*LoginResponse, error) {
// 如果没有传国家码,默认使用+86
if countryCode == "" {
countryCode = "+86"
}
// 获取Python脚本路径和venv中的Python解释器
backendDir := filepath.Join("..", "backend")
pythonScript := filepath.Join(backendDir, "xhs_cli.py")
// 使用venv中的Python解释器 (跨平台)
pythonCmd := getPythonPath(backendDir)
// 执行Python脚本
cmd := exec.Command(pythonCmd, pythonScript, "login", phone, code, countryCode)
// 设置工作目录
cmd.Dir = backendDir
// 捕获输出
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
// 执行命令
err := cmd.Run()
// 打印Python脚本的日志输出stderr
if stderr.Len() > 0 {
log.Printf("[Python日志-登录] %s", stderr.String())
}
if err != nil {
return nil, fmt.Errorf("执行Python脚本失败: %w, stderr: %s", err, stderr.String())
}
// 获取UTF-8编码的输出
outputStr := stdout.String()
// 解析JSON输出
var result LoginResponse
if err := json.Unmarshal([]byte(outputStr), &result); err != nil {
return nil, fmt.Errorf("解析Python输出失败: %w, output: %s", err, outputStr)
}
// 检查Python脚本返回的success字段
if !result.Data["success"].(bool) {
errorMsg := "登录失败"
if errStr, ok := result.Data["error"].(string); ok {
errorMsg = errStr
}
return &LoginResponse{
Code: 1,
Message: errorMsg,
}, nil
}
return &LoginResponse{
Code: 0,
Message: "登录成功",
Data: result.Data,
}, nil
}