From 8ff943ead18ad6201cf020aa8a07e3deb295bed3 Mon Sep 17 00:00:00 2001 From: renzhiyuan <465386466@qq.com> Date: Fri, 24 Jul 2026 11:54:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=87=E4=BB=B6:=20xy=5Fsh?= =?UTF-8?q?/internal/test/example.go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xy_sh/internal/test/example.go | 180 +++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 xy_sh/internal/test/example.go diff --git a/xy_sh/internal/test/example.go b/xy_sh/internal/test/example.go new file mode 100644 index 0000000..2c548ca --- /dev/null +++ b/xy_sh/internal/test/example.go @@ -0,0 +1,180 @@ +package test + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" + "time" + + "xy_sh/pkg/crypto" +) + +const ( + sm3Salt = "your_sm3_salt" + sm4Key = "1234567890123456" +) + +// APIClient API客户端示例 +type APIClient struct { + BaseURL string + SM3Salt string + SM4Key []byte +} + +// NewAPIClient 创建API客户端 +func NewAPIClient(baseURL string) *APIClient { + return &APIClient{ + BaseURL: baseURL, + SM3Salt: sm3Salt, + SM4Key: []byte(sm4Key), + } +} + +// EncryptAndSign 加密业务数据并生成签名 +func (c *APIClient) EncryptAndSign(bizData interface{}) (timestamp string, encryptedData string, sign string, err error) { + jsonBytes, err := json.Marshal(bizData) + if err != nil { + return "", "", "", fmt.Errorf("序列化失败: %v", err) + } + + encryptedData, err = crypto.SM4ECBEncrypt(jsonBytes, c.SM4Key) + if err != nil { + return "", "", "", fmt.Errorf("加密失败: %v", err) + } + + timestamp = fmt.Sprintf("%d", time.Now().UnixMilli()) + + sign = crypto.SM3WithSalt(timestamp+encryptedData, c.SM3Salt) + + return timestamp, encryptedData, sign, nil +} + +// SendRequest 发送请求 +func (c *APIClient) SendRequest(path string, bizData interface{}) (map[string]interface{}, error) { + timestamp, encryptedData, sign, err := c.EncryptAndSign(bizData) + if err != nil { + return nil, err + } + + reqBody := map[string]string{ + "encryptedData": encryptedData, + } + reqBytes, _ := json.Marshal(reqBody) + + req, err := http.NewRequest("POST", c.BaseURL+path, bytes.NewReader(reqBytes)) + if err != nil { + return nil, fmt.Errorf("创建请求失败: %v", err) + } + + req.Header.Set("Content-Type", "application/json") + req.Header.Set("timestamp", timestamp) + req.Header.Set("sign", sign) + + client := &http.Client{Timeout: 30 * time.Second} + resp, err := client.Do(req) + if err != nil { + return nil, fmt.Errorf("请求失败: %v", err) + } + defer resp.Body.Close() + + respBytes, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("读取响应失败: %v", err) + } + + var result map[string]interface{} + if err := json.Unmarshal(respBytes, &result); err != nil { + return nil, fmt.Errorf("解析响应失败: %v", err) + } + + return result, nil +} + +// ExampleOrder 下单接口示例 +func ExampleOrder() { + client := NewAPIClient("http://localhost:8080") + + bizData := map[string]interface{}{ + "actCode": "ACT001", + "goodsCode": "123456", + "actOrderNum": "00001", + "account": "19912345678", + "callbackUrl": "https://xxx/notice", + } + + result, err := client.SendRequest("/api/order", bizData) + if err != nil { + fmt.Printf("下单失败: %v\n", err) + return + } + + fmt.Printf("下单响应: %+v\n", result) + + if data, ok := result["data"].(string); ok && data != "" { + decrypted, err := crypto.SM4ECBDecrypt(data, client.SM4Key) + if err == nil { + fmt.Printf("解密后数据: %s\n", string(decrypted)) + } + } +} + +// ExampleQuery 查询接口示例 +func ExampleQuery() { + client := NewAPIClient("http://localhost:8080") + + bizData := map[string]interface{}{ + "actCode": "FBG6vdYqGE4mGX7EH/woEg==", + "orderNo": "HM1757046717684000102707339840b23222", + } + + result, err := client.SendRequest("/api/query", bizData) + if err != nil { + fmt.Printf("查询失败: %v\n", err) + return + } + + fmt.Printf("查询响应: %+v\n", result) +} + +// ExampleWechatRecharge 微信立减金充值示例 +func ExampleWechatRecharge() { + client := NewAPIClient("http://localhost:8080") + + bizData := map[string]interface{}{ + "actCode": "FBG6vdYqGE4mGX7EH/woEg==", + "goodsCode": "0001", + "actOrderNum": "0001", + "openId": "0001", + "appId": "00001", + } + + result, err := client.SendRequest("/api/wechat/recharge", bizData) + if err != nil { + fmt.Printf("充值失败: %v\n", err) + return + } + + fmt.Printf("充值响应: %+v\n", result) +} + +// ExampleCallback 回调通知示例 +func ExampleCallback() { + client := NewAPIClient("http://localhost:8080") + + bizData := map[string]interface{}{ + "orderNo": "HM202509291010001", + "actOrderNum": "XY2025092910100001", + "status": 3, + "account": "19912345678", + } + + result, err := client.SendRequest("/api/callback", bizData) + if err != nil { + fmt.Printf("回调失败: %v\n", err) + return + } + + fmt.Printf("回调响应: %+v\n", result) +} \ No newline at end of file