226 lines
7.1 KiB
Go
226 lines
7.1 KiB
Go
package service
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
"tianyuan-api/apps/api/internal/config"
|
|
"tianyuan-api/pkg/crypto"
|
|
"tianyuan-api/pkg/errs"
|
|
"time"
|
|
)
|
|
|
|
type Wrapper[T any] struct {
|
|
Data T `json:"data"`
|
|
}
|
|
type WestResp struct {
|
|
Message string `json:"message"`
|
|
Code string `json:"code"`
|
|
Data string `json:"data"`
|
|
ID string `json:"id"`
|
|
ErrorCode *int `json:"error_code"`
|
|
Reason string `json:"reason"`
|
|
}
|
|
type WestSecondResp struct {
|
|
Message string `json:"message"`
|
|
Code string `json:"code"`
|
|
Data json.RawMessage `json:"data"`
|
|
ID string `json:"id"`
|
|
ErrorCode *int `json:"error_code"`
|
|
Reason string `json:"reason"`
|
|
}
|
|
type WestDexService struct {
|
|
config config.WestConfig
|
|
}
|
|
|
|
// NewWestDexService 是一个构造函数,用于初始化 WestDexService
|
|
func NewWestDexService(config config.WestConfig) *WestDexService {
|
|
return &WestDexService{
|
|
config: config,
|
|
}
|
|
}
|
|
|
|
// CallAPI 调用西部数据的 API
|
|
func (w *WestDexService) CallAPI(code string, reqData map[string]interface{}, secretId string) (resp []byte, err *errs.AppError) {
|
|
logx.Infof("西部请求传入%v", reqData)
|
|
// 生成当前的13位时间戳
|
|
timestamp := strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10)
|
|
|
|
// 构造请求URL
|
|
reqUrl := fmt.Sprintf("%s/%s/%s?timestamp=%s", w.config.Url, secretId, code, timestamp)
|
|
|
|
jsonData, marshalErr := json.Marshal(reqData)
|
|
if marshalErr != nil {
|
|
logx.Errorf("【西部数据请求】JSON编码错误: %v", marshalErr)
|
|
return nil, errs.ErrSystem
|
|
}
|
|
|
|
// 创建HTTP POST请求
|
|
req, newRequestErr := http.NewRequest("POST", reqUrl, bytes.NewBuffer(jsonData))
|
|
if newRequestErr != nil {
|
|
logx.Errorf("【西部数据请求】创建请求错误: %v", newRequestErr)
|
|
return nil, errs.ErrSystem
|
|
}
|
|
|
|
// 设置请求头
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
// 发送请求
|
|
client := &http.Client{}
|
|
httpResp, clientDoErr := client.Do(req)
|
|
if clientDoErr != nil {
|
|
logx.Errorf("【西部数据请求】发送请求错误: %v", clientDoErr)
|
|
return nil, errs.ErrSystem
|
|
}
|
|
defer func(Body io.ReadCloser) {
|
|
closeErr := Body.Close()
|
|
if closeErr != nil {
|
|
|
|
}
|
|
}(httpResp.Body)
|
|
|
|
// 检查请求是否成功
|
|
if httpResp.StatusCode == 200 {
|
|
// 读取响应体
|
|
bodyBytes, ReadErr := io.ReadAll(httpResp.Body)
|
|
if ReadErr != nil {
|
|
logx.Errorf("【西部数据请求】读取响应体错误: %v", ReadErr)
|
|
return nil, errs.ErrSystem
|
|
}
|
|
|
|
// 手动调用 json.Unmarshal 触发自定义的 UnmarshalJSON 方法
|
|
var westDexResp WestResp
|
|
UnmarshalErr := json.Unmarshal(bodyBytes, &westDexResp)
|
|
if UnmarshalErr != nil {
|
|
logx.Errorf("【西部数据请求】JSON反序列化错误: %v", UnmarshalErr)
|
|
return nil, errs.ErrSystem
|
|
}
|
|
logx.Infof("西部请求响应%+v", westDexResp)
|
|
|
|
logx.Infof("西部流水号: %s", westDexResp.ID)
|
|
|
|
if westDexResp.Code != "00000" {
|
|
if westDexResp.Code == "-1" {
|
|
if westDexResp.Data == "" {
|
|
logx.Errorf("【西部数据请求】业务失败时响应数据为空: %s %s", westDexResp.Message, westDexResp.Reason)
|
|
return nil, errs.ErrSystem
|
|
}
|
|
decryptedData, DecryptErr := crypto.WestDexDecrypt(westDexResp.Data, w.config.Key)
|
|
if DecryptErr != nil {
|
|
logx.Errorf("【西部数据请求】响应数据解密错误: %v", DecryptErr)
|
|
return nil, errs.ErrSystem
|
|
}
|
|
logx.Errorf("【西部数据请求】响应数据业务异常: %s %s %s", westDexResp.Message, westDexResp.Reason, string(decryptedData))
|
|
return decryptedData, errs.ErrDataSource
|
|
} else {
|
|
logx.Errorf("【西部数据请求】响应数据异常: %s %s", westDexResp.Message, westDexResp.Reason)
|
|
return nil, errs.ErrSystem
|
|
}
|
|
}
|
|
if westDexResp.Data == "" {
|
|
logx.Errorf("【西部数据请求】响应Data字段数据为空")
|
|
return nil, errs.ErrSystem
|
|
}
|
|
// 解密响应数据
|
|
decryptedData, DecryptErr := crypto.WestDexDecrypt(westDexResp.Data, w.config.Key)
|
|
if DecryptErr != nil {
|
|
logx.Errorf("【西部数据请求】响应数据解密错误: %v", DecryptErr)
|
|
return nil, errs.ErrSystem
|
|
}
|
|
// 输出解密后的数据
|
|
return decryptedData, nil
|
|
}
|
|
|
|
logx.Errorf("【西部数据请求】请求失败,状态码: %d", httpResp.StatusCode)
|
|
return nil, errs.ErrSystem
|
|
}
|
|
|
|
// CallAPI 调用西部数据的 API
|
|
func (w *WestDexService) CallAPISecond(code string, reqData map[string]interface{}, secretId string) (resp []byte, err *errs.AppError) {
|
|
logx.Infof("西部请求传入%v", reqData)
|
|
// 生成当前的13位时间戳
|
|
timestamp := strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10)
|
|
|
|
// 构造请求URL
|
|
reqUrl := fmt.Sprintf("%s/%s/%s?timestamp=%s", w.config.Url, secretId, code, timestamp)
|
|
|
|
jsonData, marshalErr := json.Marshal(reqData)
|
|
if marshalErr != nil {
|
|
logx.Errorf("【西部数据请求】JSON编码错误: %v", marshalErr)
|
|
return nil, errs.ErrSystem
|
|
}
|
|
|
|
// 创建HTTP POST请求
|
|
req, newRequestErr := http.NewRequest("POST", reqUrl, bytes.NewBuffer(jsonData))
|
|
if newRequestErr != nil {
|
|
logx.Errorf("【西部数据请求】创建请求错误: %v", newRequestErr)
|
|
return nil, errs.ErrSystem
|
|
}
|
|
|
|
// 设置请求头
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
// 发送请求
|
|
client := &http.Client{}
|
|
httpResp, clientDoErr := client.Do(req)
|
|
if clientDoErr != nil {
|
|
logx.Errorf("【西部数据请求】发送请求错误: %v", clientDoErr)
|
|
return nil, errs.ErrSystem
|
|
}
|
|
defer func(Body io.ReadCloser) {
|
|
closeErr := Body.Close()
|
|
if closeErr != nil {
|
|
|
|
}
|
|
}(httpResp.Body)
|
|
|
|
// 检查请求是否成功
|
|
if httpResp.StatusCode == 200 {
|
|
// 读取响应体
|
|
bodyBytes, ReadErr := io.ReadAll(httpResp.Body)
|
|
if ReadErr != nil {
|
|
logx.Errorf("【西部数据请求】读取响应体错误: %v", ReadErr)
|
|
return nil, errs.ErrSystem
|
|
}
|
|
|
|
// 手动调用 json.Unmarshal 触发自定义的 UnmarshalJSON 方法
|
|
var westDexResp WestSecondResp
|
|
UnmarshalErr := json.Unmarshal(bodyBytes, &westDexResp)
|
|
if UnmarshalErr != nil {
|
|
logx.Errorf("【西部数据请求】JSON反序列化错误: %v", UnmarshalErr)
|
|
return nil, errs.ErrSystem
|
|
}
|
|
logx.Infof("西部请求响应%+v", westDexResp)
|
|
|
|
logx.Infof("西部流水号: %s", westDexResp.ID)
|
|
|
|
if westDexResp.Code != "0000" {
|
|
if westDexResp.Code == "-1" {
|
|
if westDexResp.Data == nil {
|
|
logx.Errorf("【西部数据请求】业务失败时响应数据为空: %s %s", westDexResp.Message, westDexResp.Reason)
|
|
return nil, errs.ErrSystem
|
|
}
|
|
logx.Errorf("【西部数据请求】响应数据业务异常: %s %s %s", westDexResp.Message, westDexResp.Reason, string(westDexResp.Data))
|
|
return westDexResp.Data, errs.ErrDataSource
|
|
} else {
|
|
logx.Errorf("【西部数据请求】响应数据异常: %s %s", westDexResp.Message, westDexResp.Reason)
|
|
return nil, errs.ErrSystem
|
|
}
|
|
}
|
|
if westDexResp.Data == nil {
|
|
logx.Errorf("【西部数据请求】响应Data字段数据为空")
|
|
return nil, errs.ErrSystem
|
|
}
|
|
|
|
// 输出解密后的数据
|
|
return westDexResp.Data, nil
|
|
}
|
|
|
|
logx.Errorf("【西部数据请求】请求失败,状态码: %d", httpResp.StatusCode)
|
|
return nil, errs.ErrSystem
|
|
}
|