feat: 互动处理

This commit is contained in:
Blizzard
2026-03-05 16:54:25 +08:00
parent 74b252550b
commit 2583b5f302
30 changed files with 412 additions and 119 deletions
+5
View File
@@ -109,3 +109,8 @@ func TestGetZeroTime(t *testing.T) {
fmt.Printf("当天零点: %v\n", zeroTime)
fmt.Printf("时间戳(秒): %v\n", zeroTime.Unix())
}
func TestGenName(t *testing.T) {
username := uniqueid.GenerateRadioUsername()
fmt.Println(username)
}
+24 -20
View File
@@ -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)
}