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

118 lines
3.1 KiB
Go
Raw Normal View History

2024-10-02 00:57:17 +08:00
package service
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/zeromicro/go-zero/core/logx"
"io"
"net/http"
"strconv"
2024-10-02 12:52:20 +08:00
"tianyuan-api/apps/api/internal/config"
"tianyuan-api/pkg/crypto"
2024-10-02 00:57:17 +08:00
"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 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{}) (resp []byte, err error) {
// 生成当前的13位时间戳
timestamp := strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10)
// 构造请求URL
2024-10-02 12:52:20 +08:00
reqUrl := fmt.Sprintf("%s/%s/%s?timestamp=%s", w.config.Url, w.config.SecretId, code, timestamp)
2024-10-02 00:57:17 +08:00
// 将请求参数编码为JSON格式
data := map[string]interface{}{
"data": reqData,
}
jsonData, err := json.Marshal(data)
if err != nil {
logx.Errorf("【西部数据请求】JSON编码错误: %v", err)
return
}
// 创建HTTP POST请求
req, err := http.NewRequest("POST", reqUrl, bytes.NewBuffer(jsonData))
if err != nil {
logx.Errorf("【西部数据请求】创建请求错误: %v", err)
return
}
// 设置请求头
req.Header.Set("Content-Type", "application/json")
// 发送请求
client := &http.Client{}
httpResp, err := client.Do(req)
if err != nil {
logx.Errorf("【西部数据请求】发送请求错误: %v", err)
return nil, errors.New("业务异常")
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != 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)
return nil, ReadErr
}
// 手动调用 json.Unmarshal 触发自定义的 UnmarshalJSON 方法
var westDexResp WestResp
UnmarshalErr := json.Unmarshal(bodyBytes, &westDexResp)
if UnmarshalErr != nil {
logx.Errorf("【西部数据请求】JSON反序列化错误: %v", UnmarshalErr)
return nil, UnmarshalErr
}
// 到这层是西部系统
if westDexResp.Code != "00000" {
logx.Errorf("【西部数据请求】响应数据业务异常: %s %s", westDexResp.Message, westDexResp.Reason)
return nil, errors.New("业务异常")
}
// 解密响应数据
2024-10-02 12:52:20 +08:00
decryptedData, DecryptErr := crypto.WestDexDecrypt(westDexResp.Data, w.config.Key)
2024-10-02 00:57:17 +08:00
if DecryptErr != nil {
logx.Errorf("【西部数据请求】响应数据解密错误: %v", DecryptErr)
return nil, DecryptErr
}
// 输出解密后的数据
return decryptedData, nil
}
logx.Errorf("【西部数据请求】请求失败,状态码: %d", httpResp.StatusCode)
return nil, errors.New("west response error status code: " + strconv.Itoa(httpResp.StatusCode))
}