33 lines
866 B
Go
33 lines
866 B
Go
package utility
|
||
|
||
import (
|
||
"fmt"
|
||
"math/rand"
|
||
"strings"
|
||
"time"
|
||
)
|
||
|
||
// GenerateUserID 根据传入的用户类型生成唯一的用户ID。
|
||
// 用户ID格式为:<PREFIX>_<16位数字字符串>,例如:QQ_2025052712345678。
|
||
// 前缀由 userType 参数指定(例如 "qq"、"wx"),自动转换为大写。
|
||
// 数字部分由当前时间戳与随机数拼接而成,确保唯一性。
|
||
//
|
||
// 参数:
|
||
// - userType: 用户类型,如 "qq" 或 "wx"
|
||
//
|
||
// 返回值:
|
||
// - string: 格式为 <大写前缀>_<16位唯一数字> 的用户ID
|
||
func GenerateUserID(userType string) string {
|
||
prefix := strings.ToUpper(userType)
|
||
|
||
timestamp := fmt.Sprintf("%d", time.Now().UnixNano())
|
||
core := timestamp[:12]
|
||
|
||
rand.Seed(time.Now().UnixNano())
|
||
suffix := fmt.Sprintf("%04d", rand.Intn(10000))
|
||
|
||
idNum := core + suffix
|
||
|
||
return fmt.Sprintf("%s_%s", prefix, idNum)
|
||
}
|