This commit is contained in:
oneao committed 2026-02-27 17:29:28 +08:00
1 parent 1903861354
commit 1b1a54f7dd
35 files changed
+1185 -535

No files matched your search

+26 -1
View File
@@ -1,6 +1,10 @@
package utils
import "encoding/json"
import (
"crypto/rand"
"encoding/json"
"math/big"
)
// JSONStringToStruct 将 JSON 字符串反序列化到指定结构体类型
func JSONStringToStruct[T any](jsonStr string) (*T, error) {
@@ -18,3 +22,24 @@ func JSONStringToMap(jsonStr string) (map[string]interface{}, error) {
}
return result, nil
}
// GenerateCode 生成指定长度的随机邀请码(大写字母+数字)
// 不会返回错误,如果随机失败,会自动重试
func GenerateCode(length int) string {
const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
code := make([]byte, length)
for i := 0; i < length; i++ {
for {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
if err != nil {
// 随机失败,重试
continue
}
code[i] = charset[num.Int64()]
break
}
}
return string(code)
}