添加文件: crypto.go

This commit is contained in:
renzhiyuan 2026-08-28 16:16:44 +08:00
parent 0facb7f445
commit a3da37d02a
1 changed files with 39 additions and 0 deletions

39
crypto.go Normal file
View File

@ -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