From b22fdd297bb248c1024c13b218ee872acbb6a5f0 Mon Sep 17 00:00:00 2001 From: Mrx <18278715334@163.com> Date: Fri, 8 May 2026 12:07:05 +0800 Subject: [PATCH] f --- .../external/huibo/huibo_service.go | 48 +++++++++++++++---- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/internal/infrastructure/external/huibo/huibo_service.go b/internal/infrastructure/external/huibo/huibo_service.go index 29dfb0f..a9773e5 100644 --- a/internal/infrastructure/external/huibo/huibo_service.go +++ b/internal/infrastructure/external/huibo/huibo_service.go @@ -32,11 +32,11 @@ var ( ) const ( - headerAuthorization = "Authorization" - headerYMDate = "YmDate" - headerOrderCode = "X-ORDER-CODE" - headerResponseType = "X-RESPONSE-TYPE" - headerResponseTypeDataVal = "data" + headerAuthorization = "Authorization" + headerWorkOrderCode = "workOrderCode" + headerOrderCode = "X-ORDER-CODE" + headerSecretIDHdr = "secretId" + headerAESKeyHdr = "aesKey" ) // 汇博常见状态码 @@ -225,9 +225,7 @@ func (s *HuiboService) CallEducationBackgroundDetailed(ctx context.Context, name func (s *HuiboService) callAPI(ctx context.Context, reqOuterJSON []byte, pdfBytes []byte) ([]byte, error) { var body bytes.Buffer writer := multipart.NewWriter(&body) - if err := writer.WriteField("req", string(reqOuterJSON)); err != nil { - return nil, errors.Join(ErrSystem, err) - } + // 与对接示例一致:先 file 再 req part, err := writer.CreateFormFile("file", "authorization.pdf") if err != nil { return nil, errors.Join(ErrSystem, err) @@ -235,6 +233,9 @@ func (s *HuiboService) callAPI(ctx context.Context, reqOuterJSON []byte, pdfByte if _, err = part.Write(pdfBytes); err != nil { return nil, errors.Join(ErrSystem, err) } + if err := writer.WriteField("req", string(reqOuterJSON)); err != nil { + return nil, errors.Join(ErrSystem, err) + } if err = writer.Close(); err != nil { return nil, errors.Join(ErrSystem, err) } @@ -244,23 +245,49 @@ func (s *HuiboService) callAPI(ctx context.Context, reqOuterJSON []byte, pdfByte return nil, errors.Join(ErrSystem, err) } req.Header.Set(headerAuthorization, s.config.AppID+"::"+s.config.AppKey) - req.Header.Set(headerYMDate, strconv.FormatInt(time.Now().UnixMilli(), 10)) + req.Header.Set(headerWorkOrderCode, s.config.WorkOrderCode) req.Header.Set(headerOrderCode, s.config.XOrderCode) - req.Header.Set(headerResponseType, headerResponseTypeDataVal) + req.Header.Set(headerSecretIDHdr, s.config.SecretID) + req.Header.Set(headerAESKeyHdr, s.config.AESKey) req.Header.Set("Content-Type", writer.FormDataContentType()) client := &http.Client{Timeout: 60 * time.Second} resp, err := client.Do(req) if err != nil { + if s.logger != nil { + s.logger.LogErrorWithFields("汇博 HTTP 请求失败", + zap.String("url", s.config.URL), + zap.Error(err), + ) + } return nil, errors.Join(ErrDatasource, err) } defer resp.Body.Close() respBody, err := io.ReadAll(resp.Body) if err != nil { + if s.logger != nil { + s.logger.LogErrorWithFields("汇博 读取响应体失败", + zap.String("url", s.config.URL), + zap.Int("http_status", resp.StatusCode), + zap.Error(err), + ) + } return nil, errors.Join(ErrSystem, err) } if resp.StatusCode < 200 || resp.StatusCode >= 300 { + if s.logger != nil { + bodySnippet := string(respBody) + const maxLog = 1024 + if len(bodySnippet) > maxLog { + bodySnippet = bodySnippet[:maxLog] + "...(truncated)" + } + s.logger.LogErrorWithFields("汇博 HTTP 状态异常", + zap.String("url", s.config.URL), + zap.Int("http_status", resp.StatusCode), + zap.String("response_body", bodySnippet), + ) + } return nil, errors.Join(ErrDatasource, fmt.Errorf("HTTP状态码异常: %d, body: %s", resp.StatusCode, string(respBody))) } return respBody, nil @@ -272,6 +299,7 @@ func (s *HuiboService) validateConfig() error { strings.TrimSpace(s.config.AppKey) == "" || strings.TrimSpace(s.config.SecretID) == "" || strings.TrimSpace(s.config.AESKey) == "" || + strings.TrimSpace(s.config.WorkOrderCode) == "" || strings.TrimSpace(s.config.XOrderCode) == "" { return errors.New("汇博配置不完整") }