f
This commit is contained in:
@@ -32,11 +32,11 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
headerAuthorization = "Authorization"
|
headerAuthorization = "Authorization"
|
||||||
headerYMDate = "YmDate"
|
headerWorkOrderCode = "workOrderCode"
|
||||||
headerOrderCode = "X-ORDER-CODE"
|
headerOrderCode = "X-ORDER-CODE"
|
||||||
headerResponseType = "X-RESPONSE-TYPE"
|
headerSecretIDHdr = "secretId"
|
||||||
headerResponseTypeDataVal = "data"
|
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) {
|
func (s *HuiboService) callAPI(ctx context.Context, reqOuterJSON []byte, pdfBytes []byte) ([]byte, error) {
|
||||||
var body bytes.Buffer
|
var body bytes.Buffer
|
||||||
writer := multipart.NewWriter(&body)
|
writer := multipart.NewWriter(&body)
|
||||||
if err := writer.WriteField("req", string(reqOuterJSON)); err != nil {
|
// 与对接示例一致:先 file 再 req
|
||||||
return nil, errors.Join(ErrSystem, err)
|
|
||||||
}
|
|
||||||
part, err := writer.CreateFormFile("file", "authorization.pdf")
|
part, err := writer.CreateFormFile("file", "authorization.pdf")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Join(ErrSystem, err)
|
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 {
|
if _, err = part.Write(pdfBytes); err != nil {
|
||||||
return nil, errors.Join(ErrSystem, err)
|
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 {
|
if err = writer.Close(); err != nil {
|
||||||
return nil, errors.Join(ErrSystem, err)
|
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)
|
return nil, errors.Join(ErrSystem, err)
|
||||||
}
|
}
|
||||||
req.Header.Set(headerAuthorization, s.config.AppID+"::"+s.config.AppKey)
|
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(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())
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||||
|
|
||||||
client := &http.Client{Timeout: 60 * time.Second}
|
client := &http.Client{Timeout: 60 * time.Second}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
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)
|
return nil, errors.Join(ErrDatasource, err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
respBody, err := io.ReadAll(resp.Body)
|
respBody, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
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)
|
return nil, errors.Join(ErrSystem, err)
|
||||||
}
|
}
|
||||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
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 nil, errors.Join(ErrDatasource, fmt.Errorf("HTTP状态码异常: %d, body: %s", resp.StatusCode, string(respBody)))
|
||||||
}
|
}
|
||||||
return respBody, nil
|
return respBody, nil
|
||||||
@@ -272,6 +299,7 @@ func (s *HuiboService) validateConfig() error {
|
|||||||
strings.TrimSpace(s.config.AppKey) == "" ||
|
strings.TrimSpace(s.config.AppKey) == "" ||
|
||||||
strings.TrimSpace(s.config.SecretID) == "" ||
|
strings.TrimSpace(s.config.SecretID) == "" ||
|
||||||
strings.TrimSpace(s.config.AESKey) == "" ||
|
strings.TrimSpace(s.config.AESKey) == "" ||
|
||||||
|
strings.TrimSpace(s.config.WorkOrderCode) == "" ||
|
||||||
strings.TrimSpace(s.config.XOrderCode) == "" {
|
strings.TrimSpace(s.config.XOrderCode) == "" {
|
||||||
return errors.New("汇博配置不完整")
|
return errors.New("汇博配置不完整")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user