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