feat: 互动处理
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
package uniqueid
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strings"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
@@ -17,25 +16,30 @@ func GenerateId() string {
|
||||
return uuidV1.String()
|
||||
}
|
||||
|
||||
func GenerateName() string {
|
||||
str := uuid.New().String()
|
||||
//生成一个用户名 比如花友u278bb 中文后的字符随机生成 不可重复 取str的前六位
|
||||
return "花友" + str[6:12]
|
||||
// GenerateRadioUsername 生成具有电台氛围的用户名称
|
||||
func GenerateRadioUsername() string {
|
||||
// 1. 文艺词库
|
||||
adjectives := []string{"虚构", "私奔", "落日", "低空", "巡航", "无声", "迷失", "告白", "极光", "霓虹"}
|
||||
nouns := []string{"调频", "电波", "磁带", "频率", "回声", "岛屿", "信箱", "航站", "独白", "碎片"}
|
||||
|
||||
}
|
||||
// 2. 初始化随机种子 (使用纳秒级时间戳)
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
|
||||
// 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()])
|
||||
// 3. 随机抽取词库
|
||||
adj := adjectives[r.Intn(len(adjectives))]
|
||||
noun := nouns[r.Intn(len(nouns))]
|
||||
|
||||
// 4. 获取当前时间的微秒/纳秒部分作为“身份码”
|
||||
// 取纳秒的最后5位,既能体现随机性,又不会像日期那样冗长
|
||||
timeSuffix := time.Now().UnixNano() % 100000
|
||||
|
||||
// 5. 混合生成:采用不同的模板增加随机感
|
||||
templates := []string{
|
||||
"%s%s_%05d", // 如:落日电波_12345
|
||||
"Hz.%d-%s%s", // 如:Hz.67890-虚构独白
|
||||
"%s%s-%d-FM", // 如:迷失频率-54321-FM
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// GenCodeKey 生成邀请码的key
|
||||
func GenCodeKey(userId string) string {
|
||||
return fmt.Sprintf("code:%s", userId)
|
||||
selectedTemplate := templates[r.Intn(len(templates))]
|
||||
return fmt.Sprintf(selectedTemplate, adj, noun, timeSuffix)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user