commit
This commit is contained in:
@@ -31,7 +31,7 @@ type XHSCookieVerifyResult struct {
|
||||
}
|
||||
|
||||
// SendXHSCode 发送小红书验证码(调用Python HTTP服务,增加限流控制)
|
||||
func (s *EmployeeService) SendXHSCode(phone string, employeeID int) error {
|
||||
func (s *EmployeeService) SendXHSCode(phone string, employeeID int) (map[string]interface{}, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
// 预检查:验证该手机号是否已被其他用户绑定
|
||||
@@ -45,11 +45,11 @@ func (s *EmployeeService) SendXHSCode(phone string, employeeID int) error {
|
||||
// 找到了其他用户的绑定记录
|
||||
log.Printf("发送验证码 - 用户%d - 失败: 手机号%s已被用户%d绑定",
|
||||
employeeID, phone, conflictAuthor.CreatedUserID)
|
||||
return errors.New("该手机号已被其他用户绑定")
|
||||
return nil, errors.New("该手机号已被其他用户绑定")
|
||||
} else if err != gorm.ErrRecordNotFound {
|
||||
// 数据库查询异常
|
||||
log.Printf("发送验证码 - 用户%d - 检查手机号失败: %v", employeeID, err)
|
||||
return fmt.Errorf("检查手机号失败: %w", err)
|
||||
return nil, fmt.Errorf("检查手机号失败: %w", err)
|
||||
}
|
||||
// err == gorm.ErrRecordNotFound 表示该手机号未被绑定,可以继续
|
||||
|
||||
@@ -57,7 +57,7 @@ func (s *EmployeeService) SendXHSCode(phone string, employeeID int) error {
|
||||
rateLimitKey := fmt.Sprintf("rate:sms:%s", phone)
|
||||
exists, err := utils.ExistsCache(ctx, rateLimitKey)
|
||||
if err == nil && exists {
|
||||
return errors.New("验证码发送过于频繁,请稍后再试")
|
||||
return nil, errors.New("验证码发送过于频繁,请稍后再试")
|
||||
}
|
||||
|
||||
// 从配置获取Python服务地址
|
||||
@@ -76,7 +76,7 @@ func (s *EmployeeService) SendXHSCode(phone string, employeeID int) error {
|
||||
jsonData, err := json.Marshal(requestData)
|
||||
if err != nil {
|
||||
log.Printf("[发送验证码] 序列化请求数据失败: %v", err)
|
||||
return errors.New("网络错误,请稍后重试")
|
||||
return nil, errors.New("网络错误,请稍后重试")
|
||||
}
|
||||
|
||||
log.Printf("[发送验证码] 调用Python HTTP服务: %s, 请求参数: phone=%s", url, phone)
|
||||
@@ -90,7 +90,7 @@ func (s *EmployeeService) SendXHSCode(phone string, employeeID int) error {
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
log.Printf("[发送验证码] 创建请求失败: %v", err)
|
||||
return errors.New("网络错误,请稍后重试")
|
||||
return nil, errors.New("网络错误,请稍后重试")
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
@@ -99,9 +99,9 @@ func (s *EmployeeService) SendXHSCode(phone string, employeeID int) error {
|
||||
log.Printf("[发送验证码] 调用Python服务失败: %v", err)
|
||||
// 判断是否是超时错误
|
||||
if strings.Contains(err.Error(), "timeout") || strings.Contains(err.Error(), "deadline exceeded") {
|
||||
return errors.New("请求超时,请稍后重试")
|
||||
return nil, errors.New("请求超时,请稍后重试")
|
||||
}
|
||||
return errors.New("网络错误,请稍后重试")
|
||||
return nil, errors.New("网络错误,请稍后重试")
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
@@ -109,7 +109,7 @@ func (s *EmployeeService) SendXHSCode(phone string, employeeID int) error {
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Printf("[发送验证码] 读取响应失败: %v", err)
|
||||
return errors.New("网络错误,请稍后重试")
|
||||
return nil, errors.New("网络错误,请稍后重试")
|
||||
}
|
||||
|
||||
log.Printf("[发送验证码] Python服务响应: 状态码=%d, 耗时=%.2fs", resp.StatusCode, time.Since(startTime).Seconds())
|
||||
@@ -123,25 +123,27 @@ func (s *EmployeeService) SendXHSCode(phone string, employeeID int) error {
|
||||
|
||||
if err := json.Unmarshal(body, &apiResponse); err != nil {
|
||||
log.Printf("[发送验证码] 解析Python响应失败: %v, body: %s", err, string(body))
|
||||
return errors.New("网络错误,请稍后重试")
|
||||
return nil, errors.New("网络错误,请稍后重试")
|
||||
}
|
||||
|
||||
log.Printf("[Python响应] code=%d, message=%s", apiResponse.Code, apiResponse.Message)
|
||||
log.Printf("[Python响应] code=%d, message=%s, data=%v", apiResponse.Code, apiResponse.Message, apiResponse.Data)
|
||||
|
||||
// 检查响应code(FastAPI返回code=0为成功)
|
||||
if apiResponse.Code != 0 {
|
||||
log.Printf("[发送验证码] 失败: %s", apiResponse.Message)
|
||||
// 根据错误信息返回用户友好的提示
|
||||
return s.getFriendlyErrorMessage(apiResponse.Message)
|
||||
return nil, s.getFriendlyErrorMessage(apiResponse.Message)
|
||||
}
|
||||
|
||||
// 返回完整的data,包括need_captcha、qrcode_image、session_id
|
||||
log.Printf("[发送验证码] 成功, 返回数据: %v", apiResponse.Data)
|
||||
|
||||
// 2. 发送成功后设置限流标记(1分钟)
|
||||
if err := utils.SetCache(ctx, rateLimitKey, "1", 1*time.Minute); err != nil {
|
||||
log.Printf("设置限流缓存失败: %v", err)
|
||||
}
|
||||
|
||||
log.Printf("[发送验证码] 验证码发送成功")
|
||||
return nil
|
||||
return apiResponse.Data, nil
|
||||
}
|
||||
|
||||
// getFriendlyErrorMessage 将技术错误信息转换为用户友好提示
|
||||
|
||||
Reference in New Issue
Block a user