2025-12-19 22:36:48 +08:00
|
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2026-01-07 22:55:12 +08:00
|
|
|
|
"ai_xhs/config"
|
2025-12-19 22:36:48 +08:00
|
|
|
|
"bytes"
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"fmt"
|
2026-01-07 22:55:12 +08:00
|
|
|
|
"io"
|
2025-12-19 22:36:48 +08:00
|
|
|
|
"log"
|
2026-01-07 22:55:12 +08:00
|
|
|
|
"net/http"
|
|
|
|
|
|
"time"
|
2025-12-19 22:36:48 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
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"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-07 22:55:12 +08:00
|
|
|
|
// SendVerificationCode 调用Python HTTP API发送验证码
|
2025-12-19 22:36:48 +08:00
|
|
|
|
func (s *XHSService) SendVerificationCode(phone, countryCode string) (*SendCodeResponse, error) {
|
|
|
|
|
|
// 如果没有传国家码,默认使用+86
|
|
|
|
|
|
if countryCode == "" {
|
|
|
|
|
|
countryCode = "+86"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-07 22:55:12 +08:00
|
|
|
|
// 获取Python服务地址
|
|
|
|
|
|
pythonURL := config.GetPythonServiceURL()
|
|
|
|
|
|
apiURL := fmt.Sprintf("%s/api/xhs/send-code", pythonURL)
|
2025-12-19 22:36:48 +08:00
|
|
|
|
|
2026-01-07 22:55:12 +08:00
|
|
|
|
// 构造请求体
|
|
|
|
|
|
reqData := map[string]interface{}{
|
|
|
|
|
|
"phone": phone,
|
|
|
|
|
|
"country_code": countryCode,
|
|
|
|
|
|
}
|
|
|
|
|
|
reqBody, _ := json.Marshal(reqData)
|
2025-12-19 22:36:48 +08:00
|
|
|
|
|
2026-01-07 22:55:12 +08:00
|
|
|
|
// 发送HTTP POST请求
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
|
|
Timeout: 60 * time.Second, // 60秒超时
|
2025-12-19 22:36:48 +08:00
|
|
|
|
}
|
2026-01-07 22:55:12 +08:00
|
|
|
|
resp, err := client.Post(apiURL, "application/json", bytes.NewBuffer(reqBody))
|
2025-12-19 22:36:48 +08:00
|
|
|
|
if err != nil {
|
2026-01-07 22:55:12 +08:00
|
|
|
|
return nil, fmt.Errorf("调用Python服务失败: %w", err)
|
2025-12-19 22:36:48 +08:00
|
|
|
|
}
|
2026-01-07 22:55:12 +08:00
|
|
|
|
defer resp.Body.Close()
|
2025-12-19 22:36:48 +08:00
|
|
|
|
|
2026-01-07 22:55:12 +08:00
|
|
|
|
// 读取响应
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, fmt.Errorf("读取响应失败: %w", err)
|
2025-12-19 22:36:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-07 22:55:12 +08:00
|
|
|
|
log.Printf("[Python API-发送验证码] 响应: %s", string(body))
|
|
|
|
|
|
|
|
|
|
|
|
// 解析JSON响应
|
|
|
|
|
|
var result SendCodeResponse
|
|
|
|
|
|
if err := json.Unmarshal(body, &result); err != nil {
|
|
|
|
|
|
return nil, fmt.Errorf("解析响应失败: %w, body: %s", err, string(body))
|
2025-12-19 22:36:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-07 22:55:12 +08:00
|
|
|
|
return &result, nil
|
2025-12-19 22:36:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-07 22:55:12 +08:00
|
|
|
|
// VerifyLogin 调用Python HTTP API验证登录
|
2025-12-19 22:36:48 +08:00
|
|
|
|
func (s *XHSService) VerifyLogin(phone, code, countryCode string) (*LoginResponse, error) {
|
|
|
|
|
|
// 如果没有传国家码,默认使用+86
|
|
|
|
|
|
if countryCode == "" {
|
|
|
|
|
|
countryCode = "+86"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-07 22:55:12 +08:00
|
|
|
|
// 获取Python服务地址
|
|
|
|
|
|
pythonURL := config.GetPythonServiceURL()
|
|
|
|
|
|
apiURL := fmt.Sprintf("%s/api/xhs/login", pythonURL)
|
2025-12-19 22:36:48 +08:00
|
|
|
|
|
2026-01-07 22:55:12 +08:00
|
|
|
|
// 构造请求体
|
|
|
|
|
|
reqData := map[string]interface{}{
|
|
|
|
|
|
"phone": phone,
|
|
|
|
|
|
"code": code,
|
|
|
|
|
|
"country_code": countryCode,
|
|
|
|
|
|
}
|
|
|
|
|
|
reqBody, _ := json.Marshal(reqData)
|
2025-12-19 22:36:48 +08:00
|
|
|
|
|
2026-01-07 22:55:12 +08:00
|
|
|
|
// 发送HTTP POST请求
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
|
|
Timeout: 120 * time.Second, // 120秒超时(登录可能较慢)
|
2025-12-19 22:36:48 +08:00
|
|
|
|
}
|
2026-01-07 22:55:12 +08:00
|
|
|
|
resp, err := client.Post(apiURL, "application/json", bytes.NewBuffer(reqBody))
|
2025-12-19 22:36:48 +08:00
|
|
|
|
if err != nil {
|
2026-01-07 22:55:12 +08:00
|
|
|
|
return nil, fmt.Errorf("调用Python服务失败: %w", err)
|
2025-12-19 22:36:48 +08:00
|
|
|
|
}
|
2026-01-07 22:55:12 +08:00
|
|
|
|
defer resp.Body.Close()
|
2025-12-19 22:36:48 +08:00
|
|
|
|
|
2026-01-07 22:55:12 +08:00
|
|
|
|
// 读取响应
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, fmt.Errorf("读取响应失败: %w", err)
|
2025-12-19 22:36:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-07 22:55:12 +08:00
|
|
|
|
log.Printf("[Python API-验证登录] 响应: %s", string(body))
|
|
|
|
|
|
|
|
|
|
|
|
// 解析JSON响应
|
|
|
|
|
|
var result LoginResponse
|
|
|
|
|
|
if err := json.Unmarshal(body, &result); err != nil {
|
|
|
|
|
|
return nil, fmt.Errorf("解析响应失败: %w, body: %s", err, string(body))
|
2025-12-19 22:36:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-07 22:55:12 +08:00
|
|
|
|
return &result, nil
|
2025-12-19 22:36:48 +08:00
|
|
|
|
}
|