Files
arenax-server/utility/openid.go

35 lines
1.0 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package utility
import (
"crypto/rand"
"fmt"
"math/big"
"strings"
"time"
)
// GenerateUserID 根据传入的用户类型生成唯一的用户ID。
// 用户ID格式为<PREFIX>_<17位数字字符串>例如QQ_20250527123456789。
// 前缀由 userType 参数指定(例如 "qq"、"wx"),自动转换为大写。
// 数字部分由当前时间戳毫秒级与4位加密随机数字拼接而成确保唯一性。
//
// 参数:
// - userType: 用户类型,如 "qq" 或 "wx"
//
// 返回值:
// - string: 格式为 <大写前缀>_<17位唯一数字> 的用户ID
func GenerateUserID(userType string) string {
prefix := strings.ToUpper(userType)
// 当前时间戳毫秒级长度为13位
timestamp := fmt.Sprintf("%d", time.Now().UnixNano()/1e6)
// 生成一个0~9999之间的随机数补齐4位
suffixNum, _ := rand.Int(rand.Reader, big.NewInt(10000))
suffix := fmt.Sprintf("%04d", suffixNum.Int64())
// 拼接最终ID
idNum := timestamp + suffix
return fmt.Sprintf("%s_%s", prefix, idNum)
}