tianyuan-api-server/apps/api/internal/service/west_dex_service.go

221 lines
7.0 KiB
Go
Raw Normal View History

2024-10-02 00:57:17 +08:00
package service
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
2024-10-02 12:52:20 +08:00
"tianyuan-api/apps/api/internal/config"
"tianyuan-api/pkg/crypto"
2024-10-15 20:52:51 +08:00
"tianyuan-api/pkg/errs"
2024-10-02 00:57:17 +08:00
"time"
2025-05-21 20:06:48 +08:00
"github.com/zeromicro/go-zero/core/logx"
2024-10-02 00:57:17 +08:00
)
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"`
}
2024-10-25 14:43:49 +08:00
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"`
}
2024-10-02 00:57:17 +08:00
type WestDexService struct {
2024-10-02 12:52:20 +08:00
config config.WestConfig
2024-10-02 00:57:17 +08:00
}
// NewWestDexService 是一个构造函数,用于初始化 WestDexService
2024-10-02 12:52:20 +08:00
func NewWestDexService(config config.WestConfig) *WestDexService {
2024-10-02 00:57:17 +08:00
return &WestDexService{
2024-10-02 12:52:20 +08:00
config: config,
2024-10-02 00:57:17 +08:00
}
}
// CallAPI 调用西部数据的 API
func (w *WestDexService) CallAPI(code string, reqData map[string]interface{}, secretId string) (resp []byte, err *errs.AppError) {
2024-10-12 21:35:35 +08:00
logx.Infof("西部请求传入%v", reqData)
2024-10-02 00:57:17 +08:00
// 生成当前的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)
2024-10-02 00:57:17 +08:00
2024-10-15 20:52:51 +08:00
jsonData, marshalErr := json.Marshal(reqData)
if marshalErr != nil {
logx.Errorf("【西部数据请求】JSON编码错误: %v", marshalErr)
return nil, errs.ErrSystem
2024-10-02 00:57:17 +08:00
}
// 创建HTTP POST请求
2024-10-15 20:52:51 +08:00
req, newRequestErr := http.NewRequest("POST", reqUrl, bytes.NewBuffer(jsonData))
if newRequestErr != nil {
logx.Errorf("【西部数据请求】创建请求错误: %v", newRequestErr)
return nil, errs.ErrSystem
2024-10-02 00:57:17 +08:00
}
// 设置请求头
req.Header.Set("Content-Type", "application/json")
// 发送请求
client := &http.Client{}
2024-10-15 20:52:51 +08:00
httpResp, clientDoErr := client.Do(req)
if clientDoErr != nil {
logx.Errorf("【西部数据请求】发送请求错误: %v", clientDoErr)
return nil, errs.ErrSystem
2024-10-02 00:57:17 +08:00
}
defer func(Body io.ReadCloser) {
2024-10-15 20:52:51 +08:00
closeErr := Body.Close()
if closeErr != nil {
}
}(httpResp.Body)
2024-10-02 00:57:17 +08:00
// 检查请求是否成功
if httpResp.StatusCode == 200 {
// 读取响应体
bodyBytes, ReadErr := io.ReadAll(httpResp.Body)
if ReadErr != nil {
logx.Errorf("【西部数据请求】读取响应体错误: %v", ReadErr)
2024-10-15 20:52:51 +08:00
return nil, errs.ErrSystem
2024-10-02 00:57:17 +08:00
}
// 手动调用 json.Unmarshal 触发自定义的 UnmarshalJSON 方法
var westDexResp WestResp
UnmarshalErr := json.Unmarshal(bodyBytes, &westDexResp)
if UnmarshalErr != nil {
logx.Errorf("【西部数据请求】JSON反序列化错误: %v", UnmarshalErr)
2024-10-15 20:52:51 +08:00
return nil, errs.ErrSystem
2024-10-02 00:57:17 +08:00
}
logx.Infof("西部请求响应%+v", westDexResp)
2024-10-12 21:35:35 +08:00
2024-10-12 20:41:55 +08:00
logx.Infof("西部流水号: %s", westDexResp.ID)
2025-05-21 20:16:49 +08:00
if westDexResp.Code != "00000" && westDexResp.Code != "200" {
2024-10-31 11:42:13 +08:00
if westDexResp.Data == "" {
logx.Errorf("【西部数据请求】业务失败时响应数据为空: %s %s", westDexResp.Message, westDexResp.Reason)
return nil, errs.ErrSystem
}
2024-10-31 11:42:13 +08:00
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
}
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
}
2024-10-02 00:57:17 +08:00
// 输出解密后的数据
return decryptedData, nil
}
2025-05-24 13:13:10 +08:00
// 读取错误响应体
bodyBytes, _ := io.ReadAll(httpResp.Body)
logx.Errorf("【西部数据请求】请求失败,状态码: %d, 响应: %s", httpResp.StatusCode, string(bodyBytes))
2024-10-15 20:52:51 +08:00
return nil, errs.ErrSystem
2024-10-02 00:57:17 +08:00
}
2024-10-25 14:43:49 +08:00
// 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" {
2024-10-31 11:42:13 +08:00
if westDexResp.Data == nil {
logx.Errorf("【西部数据请求】业务失败时响应数据为空: %s %s", westDexResp.Message, westDexResp.Reason)
2024-10-25 14:43:49 +08:00
return nil, errs.ErrSystem
}
2024-10-31 11:42:13 +08:00
logx.Errorf("【西部数据请求】响应数据业务异常: %s %s %s", westDexResp.Message, westDexResp.Reason, string(westDexResp.Data))
return westDexResp.Data, errs.ErrDataSource
2024-10-25 14:43:49 +08:00
}
if westDexResp.Data == nil {
logx.Errorf("【西部数据请求】响应Data字段数据为空")
return nil, errs.ErrSystem
}
// 输出解密后的数据
return westDexResp.Data, nil
}
2025-05-24 13:13:10 +08:00
// 读取错误响应体
bodyBytes, _ := io.ReadAll(httpResp.Body)
logx.Errorf("【西部数据请求】请求失败,状态码: %d, 响应: %s", httpResp.StatusCode, string(bodyBytes))
2024-10-25 14:43:49 +08:00
return nil, errs.ErrSystem
}