From a3da37d02aff1bad28852c34ac7d7b2803e8ebfb Mon Sep 17 00:00:00 2001 From: renzhiyuan <465386466@qq.com> Date: Fri, 28 Aug 2026 16:16:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=87=E4=BB=B6:=20crypto.?= =?UTF-8?q?go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crypto.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 crypto.go diff --git a/crypto.go b/crypto.go new file mode 100644 index 0000000..2ceb3a1 --- /dev/null +++ b/crypto.go @@ -0,0 +1,39 @@ +package sdk_LinkedMall + +import ( + "bytes" + "encoding/json" +) + +// 关于加解密与签名: +// +// 本 SDK 基于阿里云通用 OpenAPI 客户端(darabonba-openapi/v2)发起 ROA 调用, +// ROA 签名(AccessKeyId / AccessKeySecret 签名)由客户端内部自动完成, +// 业务侧无需自行实现签名算法,因此本文件不包含额外的签名/加密实现。 +// +// 本文件保留与数据安全相关的辅助函数,供后续扩展加解密、签名能力时使用: +// - MarshalJSON / UnmarshalJSON:确保金额等 int64 字段在 JSON 序列化/反序列化过程中 +// 不会丢失精度(接口金额单位统一为分,使用 int64,禁止使用 float64)。 + +// MarshalJSON 将对象安全序列化为 JSON 字节。 +// 与标准库 encoding/json.Marshal 的区别:关闭了 HTML 转义,保证输出紧凑且不转义特殊字符。 +func MarshalJSON(v interface{}) ([]byte, error) { + var buf bytes.Buffer + enc := json.NewEncoder(&buf) + enc.SetEscapeHTML(false) + if err := enc.Encode(v); err != nil { + return nil, err + } + // json.Encoder.Encode 会在末尾追加一个换行符,这里去掉。 + return bytes.TrimRight(buf.Bytes(), "\n"), nil +} + +// UnmarshalJSON 使用 UseNumber 解析 JSON,避免 int64 金额被转换为 float64 丢失精度。 +func UnmarshalJSON(data []byte, v interface{}) error { + dec := json.NewDecoder(bytes.NewReader(data)) + dec.UseNumber() + return dec.Decode(v) +} +``` + +```go \ No newline at end of file