42 lines
854 B
Go
42 lines
854 B
Go
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)
|
|
}
|