From f86b56a79d3df926469dfa98c7eb201b23ee6ee0 Mon Sep 17 00:00:00 2001 From: Echo <1711788888@qq.com> Date: Sun, 20 Jul 2025 02:10:55 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=20=E6=96=B0=E5=A2=9E=E9=9A=8F=E6=9C=BA?= =?UTF-8?q?=E9=82=80=E8=AF=B7=E7=A0=81=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/rand_code.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 utils/rand_code.go diff --git a/utils/rand_code.go b/utils/rand_code.go new file mode 100644 index 0000000..4ca7184 --- /dev/null +++ b/utils/rand_code.go @@ -0,0 +1,23 @@ +package utils + +import ( + "crypto/md5" + "fmt" + "math/rand" + "time" +) + +const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + +func GenerateInviteCode(userID uint) string { + rand.Seed(time.Now().UnixNano()) + // 拼接用户ID和随机数 + data := fmt.Sprintf("%d%d", userID, rand.Intn(1000000)) + hash := md5.Sum([]byte(data)) + code := "" + for i := 0; i < 6; i++ { + // 取哈希的前6位,每位映射到字符集 + code += string(charset[int(hash[i])%len(charset)]) + } + return code +}