feat: 优化API文档格式,调整代码缩进和注释,确保一致性和可读性

This commit is contained in:
danial
2025-05-18 22:17:35 +08:00
parent 7201a26137
commit f3f3955829

View File

@@ -60,85 +60,85 @@ sign 加密算法为 AES/CBC 加密,数据填充模式为 PKCS7 填充,加
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"encoding/json"
"fmt"
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"encoding/json"
"fmt"
)
type OrderParams struct {
OrderNo string `json:"orderNo"`
PayKey string `json:"payKey"` // 支付明文秘钥
NotifyUrl string `json:"notifyUrl"` // 回调地址
GeneratedTime int64 `json:"generatedTime"` // 过期时间
Duration int `json:"duration"` // 存续时间(小时)
ProductCode string `json:"productCode"`
ShowMMValue float64 `json:"showMMValue"`
OrderNo string `json:"orderNo"`
PayKey string `json:"payKey"` // 支付明文秘钥
NotifyUrl string `json:"notifyUrl"` // 回调地址
GeneratedTime int64 `json:"generatedTime"` // 过期时间
Duration int `json:"duration"` // 存续时间(小时)
ProductCode string `json:"productCode"`
ShowMMValue float64 `json:"showMMValue"`
}
func String(p *OrderParams) string {
r, err := json.Marshal(p)
if err != nil {
return ""
}
logs.Info(string(r))
return string(r)
r, err := json.Marshal(p)
if err != nil {
return ""
}
logs.Info(string(r))
return string(r)
}
func Encrypt(p *OrderParams) string {
prepareData := String(p)
print(prepareData, "\n")
if prepareData == "" {
return prepareData
}
prepareData := String(p)
print(prepareData, "\n")
if prepareData == "" {
return prepareData
}
// 加密密码数据
key := "thisis32bitlongpassphraseimusing"
iv := "1234567890123456"
result, _ := AesCBCEncrypt([]byte(prepareData), []byte(key), []byte(iv))
return hex.EncodeToString(result)
// 加密密码数据
key := "thisis32bitlongpassphraseimusing"
iv := "1234567890123456"
result, _ := AesCBCEncrypt([]byte(prepareData), []byte(key), []byte(iv))
return hex.EncodeToString(result)
}
// AesCBCEncrypt AES/CBC/PKCS7Padding 加密
func AesCBCEncrypt(plaintext []byte, key []byte, iv []byte) ([]byte, error) {
// AES
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
// AES
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
// PKCS7 填充
plaintext = paddingPKCS7(plaintext, aes.BlockSize)
// PKCS7 填充
plaintext = paddingPKCS7(plaintext, aes.BlockSize)
// CBC 加密
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(plaintext, plaintext)
// CBC 加密
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(plaintext, plaintext)
return plaintext, nil
return plaintext, nil
}
// PKCS7 填充
func paddingPKCS7(plaintext []byte, blockSize int) []byte {
paddingSize := blockSize - len(plaintext)%blockSize
paddingText := bytes.Repeat([]byte{byte(paddingSize)}, paddingSize)
return append(plaintext, paddingText...)
paddingSize := blockSize - len(plaintext)%blockSize
paddingText := bytes.Repeat([]byte{byte(paddingSize)}, paddingSize)
return append(plaintext, paddingText...)
}
func main() {
orderParams := OrderParams{
OrderNo: "20190520123456789",
PayKey: "20190520123456789",
NotifyUrl: "https://baidu.com/notify",
GeneratedTime: 1558312000,
Duration: 24,
ProductCode: "8645",
ShowMMValue: 100,
}
sign := Encrypt(&orderParams)
// 原始本文
fmt.Println(sign)
orderParams := OrderParams{
OrderNo: "20190520123456789",
PayKey: "20190520123456789",
NotifyUrl: "https://baidu.com/notify",
GeneratedTime: 1558312000,
Duration: 24,
ProductCode: "8645",
ShowMMValue: 100,
}
sign := Encrypt(&orderParams)
// 原始本文
fmt.Println(sign)
}
// 原始文本:{"orderNo":"20190520123456789","payKey":"20190520123456789","notifyUrl":"https://baidu.com/notify","generatedTime":1558312000,"duration":24,"productCode":"8645","showMMValue":100}
@@ -182,7 +182,7 @@ func main() {
> 提交订单接口,此接口用于第三方直接提交卡号/卡密,使用自有拉单页面
接口:{{.remoteHost}}/gateway/scan
请求方式POST
请求方式POST/FormData
### 请求参数
@@ -272,6 +272,7 @@ func main() {
> 创建订单,并返回下单单号,地址等信息
地址:{{.remoteHost}}/gateway/createOrder
请求方式Post/JSON
### 请求参数
@@ -319,51 +320,51 @@ func main() {
package main
import (
"crypto/md5"
"encoding/hex"
"sort"
"strconv"
"time"
"crypto/md5"
"encoding/hex"
"sort"
"strconv"
"time"
)
func main() {
//实际获取的appKey值
appKey := "1f2811dcd32b2c5c"
//实际获取的appSecret值
appSecret := "3C569A210D1260B2"
//实际获取的appKey值
appKey := "1f2811dcd32b2c5c"
//实际获取的appSecret值
appSecret := "3C569A210D1260B2"
// 如果有其它参数,依次添加 key=value
params := map[string]string{}
// 如果有其它参数,依次添加 key=value
params := map[string]string{}
params["appKey"] = appKey
params["phone"] = "13800138000"
params["timestamp"] = strconv.FormatInt(time.Now().Unix(), 10)
params["amount"] = "100"
params["appKey"] = appKey
params["phone"] = "13800138000"
params["timestamp"] = strconv.FormatInt(time.Now().Unix(), 10)
params["amount"] = "100"
strArr := SortMap(params)
signStr := ""
for i := 0; i < len(strArr); i++ {
k := strArr[i]
if len(params[k]) == 0 {
signStr += k
} else {
signStr += k + params[k]
}
}
signStr += appSecret
h := md5.New()
h.Write([]byte(signStr))
hex.EncodeToString(h.Sum(nil))
strArr := SortMap(params)
signStr := ""
for i := 0; i < len(strArr); i++ {
k := strArr[i]
if len(params[k]) == 0 {
signStr += k
} else {
signStr += k + params[k]
}
}
signStr += appSecret
h := md5.New()
h.Write([]byte(signStr))
hex.EncodeToString(h.Sum(nil))
}
// SortMap 对map的key值进行排序
func SortMap(m map[string]string) []string {
var arr []string
for k := range m {
arr = append(arr, k)
}
sort.Strings(arr)
return arr
var arr []string
for k := range m {
arr = append(arr, k)
}
sort.Strings(arr)
return arr
}