f
This commit is contained in:
67
internal/infrastructure/external/yushan/yushan_factory.go
vendored
Normal file
67
internal/infrastructure/external/yushan/yushan_factory.go
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
package yushan
|
||||
|
||||
import (
|
||||
"hyapi-server/internal/config"
|
||||
"hyapi-server/internal/shared/external_logger"
|
||||
)
|
||||
|
||||
// NewYushanServiceWithConfig 使用配置创建羽山服务
|
||||
func NewYushanServiceWithConfig(cfg *config.Config) (*YushanService, error) {
|
||||
// 将配置类型转换为通用外部服务日志配置
|
||||
loggingConfig := external_logger.ExternalServiceLoggingConfig{
|
||||
Enabled: cfg.Yushan.Logging.Enabled,
|
||||
LogDir: cfg.Yushan.Logging.LogDir,
|
||||
ServiceName: "yushan",
|
||||
UseDaily: cfg.Yushan.Logging.UseDaily,
|
||||
EnableLevelSeparation: cfg.Yushan.Logging.EnableLevelSeparation,
|
||||
LevelConfigs: make(map[string]external_logger.ExternalServiceLevelFileConfig),
|
||||
}
|
||||
|
||||
// 转换级别配置
|
||||
for key, value := range cfg.Yushan.Logging.LevelConfigs {
|
||||
loggingConfig.LevelConfigs[key] = external_logger.ExternalServiceLevelFileConfig{
|
||||
MaxSize: value.MaxSize,
|
||||
MaxBackups: value.MaxBackups,
|
||||
MaxAge: value.MaxAge,
|
||||
Compress: value.Compress,
|
||||
}
|
||||
}
|
||||
|
||||
// 创建通用外部服务日志器
|
||||
logger, err := external_logger.NewExternalServiceLogger(loggingConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 创建羽山服务
|
||||
service := NewYushanService(
|
||||
cfg.Yushan.URL,
|
||||
cfg.Yushan.APIKey,
|
||||
cfg.Yushan.AcctID,
|
||||
logger,
|
||||
)
|
||||
|
||||
return service, nil
|
||||
}
|
||||
|
||||
// NewYushanServiceWithLogging 使用自定义日志配置创建羽山服务
|
||||
func NewYushanServiceWithLogging(url, apiKey, acctID string, loggingConfig external_logger.ExternalServiceLoggingConfig) (*YushanService, error) {
|
||||
// 设置服务名称
|
||||
loggingConfig.ServiceName = "yushan"
|
||||
|
||||
// 创建通用外部服务日志器
|
||||
logger, err := external_logger.NewExternalServiceLogger(loggingConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 创建羽山服务
|
||||
service := NewYushanService(url, apiKey, acctID, logger)
|
||||
|
||||
return service, nil
|
||||
}
|
||||
|
||||
// NewYushanServiceSimple 创建简单的羽山服务(无日志)
|
||||
func NewYushanServiceSimple(url, apiKey, acctID string) *YushanService {
|
||||
return NewYushanService(url, apiKey, acctID, nil)
|
||||
}
|
||||
287
internal/infrastructure/external/yushan/yushan_service.go
vendored
Normal file
287
internal/infrastructure/external/yushan/yushan_service.go
vendored
Normal file
@@ -0,0 +1,287 @@
|
||||
package yushan
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/md5"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"hyapi-server/internal/shared/external_logger"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrDatasource = errors.New("数据源异常")
|
||||
ErrNotFound = errors.New("查询为空")
|
||||
ErrSystem = errors.New("系统异常")
|
||||
)
|
||||
|
||||
type YushanConfig struct {
|
||||
URL string
|
||||
ApiKey string
|
||||
AcctID string
|
||||
}
|
||||
|
||||
type YushanService struct {
|
||||
config YushanConfig
|
||||
logger *external_logger.ExternalServiceLogger
|
||||
}
|
||||
|
||||
// NewYushanService 是一个构造函数,用于初始化 YushanService
|
||||
func NewYushanService(url, apiKey, acctID string, logger *external_logger.ExternalServiceLogger) *YushanService {
|
||||
return &YushanService{
|
||||
config: YushanConfig{
|
||||
URL: url,
|
||||
ApiKey: apiKey,
|
||||
AcctID: acctID,
|
||||
},
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// CallAPI 调用羽山数据的 API
|
||||
func (y *YushanService) CallAPI(ctx context.Context, code string, params map[string]interface{}) (respBytes []byte, err error) {
|
||||
startTime := time.Now()
|
||||
requestID := y.generateRequestID()
|
||||
|
||||
// 从ctx中获取transactionId
|
||||
var transactionID string
|
||||
if ctxTransactionID, ok := ctx.Value("transaction_id").(string); ok {
|
||||
transactionID = ctxTransactionID
|
||||
}
|
||||
|
||||
// 记录请求日志
|
||||
if y.logger != nil {
|
||||
y.logger.LogRequest(requestID, transactionID, code, y.config.URL)
|
||||
}
|
||||
|
||||
// 获取当前时间戳
|
||||
unixMilliseconds := time.Now().UnixNano() / int64(time.Millisecond)
|
||||
|
||||
// 生成请求序列号
|
||||
requestSN, _ := y.GenerateRandomString()
|
||||
|
||||
// 构建请求数据
|
||||
reqData := map[string]interface{}{
|
||||
"prod_id": code,
|
||||
"req_time": unixMilliseconds,
|
||||
"request_sn": requestSN,
|
||||
"req_data": params,
|
||||
}
|
||||
|
||||
// 将请求数据转换为 JSON 字节数组
|
||||
messageBytes, err := json.Marshal(reqData)
|
||||
if err != nil {
|
||||
err = errors.Join(ErrSystem, err)
|
||||
if y.logger != nil {
|
||||
y.logger.LogError(requestID, transactionID, code, err, params)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 获取 API 密钥
|
||||
key, err := hex.DecodeString(y.config.ApiKey)
|
||||
if err != nil {
|
||||
err = errors.Join(ErrSystem, err)
|
||||
if y.logger != nil {
|
||||
y.logger.LogError(requestID, transactionID, code, err, params)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 使用 AES CBC 加密请求数据
|
||||
cipherText := y.AES_CBC_Encrypt(messageBytes, key)
|
||||
|
||||
// 将加密后的数据编码为 Base64 字符串
|
||||
content := base64.StdEncoding.EncodeToString(cipherText)
|
||||
|
||||
// 发起 HTTP 请求,超时时间设置为60秒
|
||||
client := &http.Client{
|
||||
Timeout: 60 * time.Second,
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", y.config.URL, strings.NewReader(content))
|
||||
if err != nil {
|
||||
err = errors.Join(ErrSystem, err)
|
||||
if y.logger != nil {
|
||||
y.logger.LogError(requestID, transactionID, code, err, params)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("ACCT_ID", y.config.AcctID)
|
||||
|
||||
// 执行请求
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
// 检查是否是超时错误
|
||||
isTimeout := false
|
||||
if ctx.Err() == context.DeadlineExceeded {
|
||||
isTimeout = true
|
||||
} else if netErr, ok := err.(interface{ Timeout() bool }); ok && netErr.Timeout() {
|
||||
isTimeout = true
|
||||
} else if errStr := err.Error(); errStr == "context deadline exceeded" ||
|
||||
errStr == "timeout" ||
|
||||
errStr == "Client.Timeout exceeded" ||
|
||||
errStr == "net/http: request canceled" {
|
||||
isTimeout = true
|
||||
}
|
||||
|
||||
if isTimeout {
|
||||
err = errors.Join(ErrDatasource, fmt.Errorf("API请求超时: %v", err))
|
||||
} else {
|
||||
err = errors.Join(ErrSystem, err)
|
||||
}
|
||||
if y.logger != nil {
|
||||
y.logger.LogError(requestID, transactionID, code, err, params)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 读取响应体
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
if y.logger != nil {
|
||||
y.logger.LogError(requestID, transactionID, code, err, params)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var respData []byte
|
||||
|
||||
if IsJSON(string(body)) {
|
||||
respData = body
|
||||
} else {
|
||||
sDec, err := base64.StdEncoding.DecodeString(string(body))
|
||||
if err != nil {
|
||||
err = errors.Join(ErrSystem, err)
|
||||
if y.logger != nil {
|
||||
y.logger.LogError(requestID, transactionID, code, err, params)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
respData = y.AES_CBC_Decrypt(sDec, key)
|
||||
}
|
||||
retCode := gjson.GetBytes(respData, "retcode").String()
|
||||
|
||||
// 记录响应日志(不记录具体响应数据)
|
||||
if y.logger != nil {
|
||||
duration := time.Since(startTime)
|
||||
y.logger.LogResponse(requestID, transactionID, code, resp.StatusCode, duration)
|
||||
}
|
||||
|
||||
if retCode == "100000" {
|
||||
// retcode 为 100000,表示查询为空
|
||||
return nil, ErrNotFound
|
||||
} else if retCode == "000000" {
|
||||
// retcode 为 000000,表示有数据,返回 retdata
|
||||
retData := gjson.GetBytes(respData, "retdata")
|
||||
if !retData.Exists() {
|
||||
err = errors.Join(ErrDatasource, fmt.Errorf("羽山请求retdata为空"))
|
||||
if y.logger != nil {
|
||||
y.logger.LogError(requestID, transactionID, code, err, params)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return []byte(retData.Raw), nil
|
||||
} else {
|
||||
err = errors.Join(ErrDatasource, fmt.Errorf("羽山请求未知的状态码"))
|
||||
if y.logger != nil {
|
||||
y.logger.LogError(requestID, transactionID, code, err, params)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// generateRequestID 生成请求ID
|
||||
func (y *YushanService) generateRequestID() string {
|
||||
timestamp := time.Now().UnixNano()
|
||||
hash := md5.Sum([]byte(fmt.Sprintf("%d_%s", timestamp, y.config.ApiKey)))
|
||||
return fmt.Sprintf("yushan_%x", hash[:8])
|
||||
}
|
||||
|
||||
// GenerateRandomString 生成一个32位的随机字符串订单号
|
||||
func (y *YushanService) GenerateRandomString() (string, error) {
|
||||
// 创建一个16字节的数组
|
||||
bytes := make([]byte, 16)
|
||||
// 读取随机字节到数组中
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
return "", err
|
||||
}
|
||||
// 将字节数组编码为16进制字符串
|
||||
return hex.EncodeToString(bytes), nil
|
||||
}
|
||||
|
||||
// AEC加密(CBC模式)
|
||||
func (y *YushanService) AES_CBC_Encrypt(plainText []byte, key []byte) []byte {
|
||||
//指定加密算法,返回一个AES算法的Block接口对象
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
//进行填充
|
||||
plainText = Padding(plainText, block.BlockSize())
|
||||
//指定初始向量vi,长度和block的块尺寸一致
|
||||
iv := []byte("0000000000000000")
|
||||
//指定分组模式,返回一个BlockMode接口对象
|
||||
blockMode := cipher.NewCBCEncrypter(block, iv)
|
||||
//加密连续数据库
|
||||
cipherText := make([]byte, len(plainText))
|
||||
blockMode.CryptBlocks(cipherText, plainText)
|
||||
//返回base64密文
|
||||
return cipherText
|
||||
}
|
||||
|
||||
// AEC解密(CBC模式)
|
||||
func (y *YushanService) AES_CBC_Decrypt(cipherText []byte, key []byte) []byte {
|
||||
//指定解密算法,返回一个AES算法的Block接口对象
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
//指定初始化向量IV,和加密的一致
|
||||
iv := []byte("0000000000000000")
|
||||
//指定分组模式,返回一个BlockMode接口对象
|
||||
blockMode := cipher.NewCBCDecrypter(block, iv)
|
||||
//解密
|
||||
plainText := make([]byte, len(cipherText))
|
||||
blockMode.CryptBlocks(plainText, cipherText)
|
||||
//删除填充
|
||||
plainText = UnPadding(plainText)
|
||||
return plainText
|
||||
} // 对明文进行填充
|
||||
func Padding(plainText []byte, blockSize int) []byte {
|
||||
//计算要填充的长度
|
||||
n := blockSize - len(plainText)%blockSize
|
||||
//对原来的明文填充n个n
|
||||
temp := bytes.Repeat([]byte{byte(n)}, n)
|
||||
plainText = append(plainText, temp...)
|
||||
return plainText
|
||||
}
|
||||
|
||||
// 对密文删除填充
|
||||
func UnPadding(cipherText []byte) []byte {
|
||||
//取出密文最后一个字节end
|
||||
end := cipherText[len(cipherText)-1]
|
||||
//删除填充
|
||||
cipherText = cipherText[:len(cipherText)-int(end)]
|
||||
return cipherText
|
||||
}
|
||||
|
||||
// 判断字符串是否为 JSON 格式
|
||||
func IsJSON(s string) bool {
|
||||
var js interface{}
|
||||
return json.Unmarshal([]byte(s), &js) == nil
|
||||
}
|
||||
83
internal/infrastructure/external/yushan/yushan_test.go
vendored
Normal file
83
internal/infrastructure/external/yushan/yushan_test.go
vendored
Normal file
@@ -0,0 +1,83 @@
|
||||
package yushan
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestGenerateRequestID(t *testing.T) {
|
||||
service := &YushanService{
|
||||
config: YushanConfig{
|
||||
ApiKey: "test_api_key_123",
|
||||
},
|
||||
}
|
||||
|
||||
id1 := service.generateRequestID()
|
||||
|
||||
// 等待一小段时间确保时间戳不同
|
||||
time.Sleep(time.Millisecond)
|
||||
|
||||
id2 := service.generateRequestID()
|
||||
|
||||
if id1 == "" || id2 == "" {
|
||||
t.Error("请求ID生成失败")
|
||||
}
|
||||
|
||||
if id1 == id2 {
|
||||
t.Error("不同时间生成的请求ID应该不同")
|
||||
}
|
||||
|
||||
// 验证ID格式
|
||||
if len(id1) < 20 { // yushan_ + 8位十六进制 + 其他
|
||||
t.Errorf("请求ID长度不足,实际: %s", id1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateRandomString(t *testing.T) {
|
||||
service := &YushanService{}
|
||||
|
||||
str1, err := service.GenerateRandomString()
|
||||
if err != nil {
|
||||
t.Fatalf("生成随机字符串失败: %v", err)
|
||||
}
|
||||
|
||||
str2, err := service.GenerateRandomString()
|
||||
if err != nil {
|
||||
t.Fatalf("生成随机字符串失败: %v", err)
|
||||
}
|
||||
|
||||
if str1 == "" || str2 == "" {
|
||||
t.Error("随机字符串为空")
|
||||
}
|
||||
|
||||
if str1 == str2 {
|
||||
t.Error("两次生成的随机字符串应该不同")
|
||||
}
|
||||
|
||||
// 验证长度(16字节 = 32位十六进制字符)
|
||||
if len(str1) != 32 || len(str2) != 32 {
|
||||
t.Error("随机字符串长度应该是32位")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsJSON(t *testing.T) {
|
||||
testCases := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"{}", true},
|
||||
{"[]", true},
|
||||
{"{\"key\": \"value\"}", true},
|
||||
{"[1, 2, 3]", true},
|
||||
{"invalid json", false},
|
||||
{"", false},
|
||||
{"{invalid}", false},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
result := IsJSON(tc.input)
|
||||
if result != tc.expected {
|
||||
t.Errorf("输入: %s, 期望: %v, 实际: %v", tc.input, tc.expected, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user