1、response修改 2、负数扣款
This commit is contained in:
@@ -3,7 +3,6 @@ package service
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"io"
|
||||
@@ -11,6 +10,7 @@ import (
|
||||
"strconv"
|
||||
"tianyuan-api/apps/api/internal/config"
|
||||
"tianyuan-api/pkg/crypto"
|
||||
"tianyuan-api/pkg/errs"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -37,7 +37,7 @@ func NewWestDexService(config config.WestConfig) *WestDexService {
|
||||
}
|
||||
|
||||
// CallAPI 调用西部数据的 API
|
||||
func (w *WestDexService) CallAPI(code string, reqData map[string]interface{}) (resp []byte, err error) {
|
||||
func (w *WestDexService) CallAPI(code string, reqData map[string]interface{}) (resp []byte, err *errs.AppError) {
|
||||
logx.Infof("西部请求传入%v", reqData)
|
||||
// 生成当前的13位时间戳
|
||||
timestamp := strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10)
|
||||
@@ -45,17 +45,17 @@ func (w *WestDexService) CallAPI(code string, reqData map[string]interface{}) (r
|
||||
// 构造请求URL
|
||||
reqUrl := fmt.Sprintf("%s/%s/%s?timestamp=%s", w.config.Url, w.config.SecretId, code, timestamp)
|
||||
|
||||
jsonData, err := json.Marshal(reqData)
|
||||
if err != nil {
|
||||
logx.Errorf("【西部数据请求】JSON编码错误: %v", err)
|
||||
return
|
||||
jsonData, marshalErr := json.Marshal(reqData)
|
||||
if marshalErr != nil {
|
||||
logx.Errorf("【西部数据请求】JSON编码错误: %v", marshalErr)
|
||||
return nil, errs.ErrSystem
|
||||
}
|
||||
|
||||
// 创建HTTP POST请求
|
||||
req, err := http.NewRequest("POST", reqUrl, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
logx.Errorf("【西部数据请求】创建请求错误: %v", err)
|
||||
return
|
||||
req, newRequestErr := http.NewRequest("POST", reqUrl, bytes.NewBuffer(jsonData))
|
||||
if newRequestErr != nil {
|
||||
logx.Errorf("【西部数据请求】创建请求错误: %v", newRequestErr)
|
||||
return nil, errs.ErrSystem
|
||||
}
|
||||
|
||||
// 设置请求头
|
||||
@@ -63,14 +63,14 @@ func (w *WestDexService) CallAPI(code string, reqData map[string]interface{}) (r
|
||||
|
||||
// 发送请求
|
||||
client := &http.Client{}
|
||||
httpResp, err := client.Do(req)
|
||||
if err != nil {
|
||||
logx.Errorf("【西部数据请求】发送请求错误: %v", err)
|
||||
return nil, errors.New("业务异常")
|
||||
httpResp, clientDoErr := client.Do(req)
|
||||
if clientDoErr != nil {
|
||||
logx.Errorf("【西部数据请求】发送请求错误: %v", clientDoErr)
|
||||
return nil, errs.ErrSystem
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
err := Body.Close()
|
||||
if err != nil {
|
||||
closeErr := Body.Close()
|
||||
if closeErr != nil {
|
||||
|
||||
}
|
||||
}(httpResp.Body)
|
||||
@@ -81,7 +81,7 @@ func (w *WestDexService) CallAPI(code string, reqData map[string]interface{}) (r
|
||||
bodyBytes, ReadErr := io.ReadAll(httpResp.Body)
|
||||
if ReadErr != nil {
|
||||
logx.Errorf("【西部数据请求】读取响应体错误: %v", ReadErr)
|
||||
return nil, ReadErr
|
||||
return nil, errs.ErrSystem
|
||||
}
|
||||
|
||||
// 手动调用 json.Unmarshal 触发自定义的 UnmarshalJSON 方法
|
||||
@@ -89,7 +89,7 @@ func (w *WestDexService) CallAPI(code string, reqData map[string]interface{}) (r
|
||||
UnmarshalErr := json.Unmarshal(bodyBytes, &westDexResp)
|
||||
if UnmarshalErr != nil {
|
||||
logx.Errorf("【西部数据请求】JSON反序列化错误: %v", UnmarshalErr)
|
||||
return nil, UnmarshalErr
|
||||
return nil, errs.ErrSystem
|
||||
}
|
||||
logx.Infof("西部请求响应%v", westDexResp)
|
||||
|
||||
@@ -97,14 +97,14 @@ func (w *WestDexService) CallAPI(code string, reqData map[string]interface{}) (r
|
||||
// 到这层是西部系统
|
||||
if westDexResp.Code != "00000" {
|
||||
logx.Errorf("【西部数据请求】响应数据业务异常: %s %s", westDexResp.Message, westDexResp.Reason)
|
||||
return nil, errors.New("业务异常")
|
||||
return []byte(westDexResp.Data), errs.ErrDataSource
|
||||
}
|
||||
|
||||
// 解密响应数据
|
||||
decryptedData, DecryptErr := crypto.WestDexDecrypt(westDexResp.Data, w.config.Key)
|
||||
if DecryptErr != nil {
|
||||
logx.Errorf("【西部数据请求】响应数据解密错误: %v", DecryptErr)
|
||||
return nil, DecryptErr
|
||||
return nil, errs.ErrSystem
|
||||
}
|
||||
|
||||
// 输出解密后的数据
|
||||
@@ -112,5 +112,5 @@ func (w *WestDexService) CallAPI(code string, reqData map[string]interface{}) (r
|
||||
}
|
||||
|
||||
logx.Errorf("【西部数据请求】请求失败,状态码: %d", httpResp.StatusCode)
|
||||
return nil, errors.New("west response error status code: " + strconv.Itoa(httpResp.StatusCode))
|
||||
return nil, errs.ErrSystem
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user