init: initial commit
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package uniqueid
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func GenerateId() string {
|
||||
uuidV1, err := uuid.NewUUID()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return uuidV1.String()
|
||||
}
|
||||
|
||||
func GenerateName() string {
|
||||
str := uuid.New().String()
|
||||
//生成一个用户名 比如花友u278bb 中文后的字符随机生成 不可重复 取str的前六位
|
||||
return "花友" + str[6:12]
|
||||
|
||||
}
|
||||
|
||||
// GenerateRandomCode 生成邀请码
|
||||
func GenerateRandomCode(length int) string {
|
||||
const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
var sb strings.Builder
|
||||
for i := 0; i < length; i++ {
|
||||
n, _ := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
|
||||
sb.WriteByte(charset[n.Int64()])
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// GenCodeKey 生成邀请码的key
|
||||
func GenCodeKey(userId string) string {
|
||||
return fmt.Sprintf("code:%s", userId)
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package uniqueid
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func GenOrderNo() string {
|
||||
/*
|
||||
支付宝订单号示例:1217752501201407033233368028
|
||||
分析可能格式:
|
||||
- 前14位:时间戳(年月日时分秒)20250112 151420
|
||||
- 中间6位:商户/业务标识
|
||||
- 最后8位:随机数或序列号
|
||||
*/
|
||||
|
||||
now := time.Now()
|
||||
|
||||
// 时间部分:年月日时分秒
|
||||
timePart := now.Format("20060102150405") // 14位
|
||||
|
||||
// 业务标识:机器ID + 进程ID + 随机数
|
||||
machineID := 1
|
||||
pid := now.Nanosecond() % 1000
|
||||
businessPart := fmt.Sprintf("%02d%03d%01d", machineID, pid, now.Second()%10) // 6位
|
||||
|
||||
// 随机部分:纳秒取模
|
||||
random1 := fmt.Sprintf("%04d", now.Nanosecond()%10000)
|
||||
random2 := fmt.Sprintf("%04d", (now.Nanosecond()/10000)%10000)
|
||||
|
||||
return timePart + businessPart + random1 + random2
|
||||
}
|
||||
Reference in New Issue
Block a user