添加文件: crypto.go
This commit is contained in:
parent
0facb7f445
commit
a3da37d02a
|
|
@ -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
|
||||
Loading…
Reference in New Issue