🎨 新增随机邀请码工具
This commit is contained in:
23
utils/rand_code.go
Normal file
23
utils/rand_code.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/md5"
|
||||||
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||||
|
|
||||||
|
func GenerateInviteCode(userID uint) string {
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
// 拼接用户ID和随机数
|
||||||
|
data := fmt.Sprintf("%d%d", userID, rand.Intn(1000000))
|
||||||
|
hash := md5.Sum([]byte(data))
|
||||||
|
code := ""
|
||||||
|
for i := 0; i < 6; i++ {
|
||||||
|
// 取哈希的前6位,每位映射到字符集
|
||||||
|
code += string(charset[int(hash[i])%len(charset)])
|
||||||
|
}
|
||||||
|
return code
|
||||||
|
}
|
Reference in New Issue
Block a user