tianyuan-api-server/pkg/generate/pay.go

37 lines
784 B
Go
Raw Permalink Normal View History

2024-10-15 00:23:07 +08:00
package generate
import (
mathrand "math/rand"
"strconv"
"time"
)
// GenerateTransactionID 生成唯一订单号的函数
func GenerateTransactionID() string {
length := 16
// 获取当前时间戳
timestamp := time.Now().UnixNano()
// 转换为字符串
timeStr := strconv.FormatInt(timestamp, 10)
// 生成随机数
mathrand.Seed(time.Now().UnixNano())
randomPart := strconv.Itoa(mathrand.Intn(1000000))
// 组合时间戳和随机数
combined := timeStr + randomPart
// 如果长度超出指定值,则截断;如果不够,则填充随机字符
if len(combined) >= length {
return combined[:length]
}
// 如果长度不够填充0
for len(combined) < length {
combined += strconv.Itoa(mathrand.Intn(10)) // 填充随机数
}
return combined
}