1
This commit is contained in:
@ -103,6 +103,31 @@ func (in IntToStr) Encode(uid int) string {
|
||||
return string(code)
|
||||
}
|
||||
|
||||
func (i IntToStr) GenerateRandomCode(num int) string {
|
||||
// 用当前时间 + 随机数生成编码基础值
|
||||
rand.Seed(time.Now().UnixNano()) // Ensure randomness
|
||||
num = num + rand.Intn(10000) // 通过加入随机数,避免重复
|
||||
|
||||
// 对输入进行基础的模运算,避免直接按时间戳生成
|
||||
var result []rune
|
||||
for num > 0 {
|
||||
result = append(result, i.AlphanumericSet[num%len(i.AlphanumericSet)])
|
||||
num = num / len(i.AlphanumericSet)
|
||||
}
|
||||
|
||||
// 返回编码后的字符串,并保证字符串长度固定
|
||||
for len(result) < i.Len {
|
||||
result = append(result, i.AlphanumericSet[rand.Intn(len(i.AlphanumericSet))])
|
||||
}
|
||||
|
||||
// 将结果逆序,保证更好的随机性
|
||||
for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
|
||||
result[i], result[j] = result[j], result[i]
|
||||
}
|
||||
|
||||
return string(result)
|
||||
}
|
||||
|
||||
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
const smsLetters = "0123456789"
|
||||
|
||||
|
||||
@ -45,7 +45,7 @@ func CheckIsEmail(email string) bool {
|
||||
}
|
||||
|
||||
// SendFrontedEmail 发送邮件
|
||||
func SendFrontedEmail(toEmail string, code string) error {
|
||||
func SendFrontedEmail(toEmail string, code string, subject, body string) error {
|
||||
// 邮箱配置
|
||||
from := config.ExtConfig.EmailConfig.MailFrom // 发送者邮箱
|
||||
password := config.ExtConfig.EmailConfig.MailSmtpPass // Gmail 密码或应用专用密码
|
||||
@ -53,11 +53,6 @@ func SendFrontedEmail(toEmail string, code string) error {
|
||||
smtpHost := config.ExtConfig.EmailConfig.MailSmtpHost // Gmail SMTP 服务器
|
||||
smtpPort := config.ExtConfig.EmailConfig.MailSmtpPort // SMTP 端口
|
||||
|
||||
link := fmt.Sprintf("%s/verify?email=%s&verify_code=%s&type=register", config.ExtConfig.Domain, toEmail, code)
|
||||
// 创建邮件消息
|
||||
subject := "注册验证"
|
||||
body := fmt.Sprintf("<h1>注册验证</h1><p>您收到此电子邮件,用于进行邮箱验证,请点击下面的链接或打开下面的网址继续。 <p>You have received this email for email verification, please click the link below or open the URL below to continue.</p> %s </p>", link)
|
||||
|
||||
m := gomail.NewMessage()
|
||||
m.SetHeader("From", from) // 发件人
|
||||
m.SetHeader("To", to) // 收件人
|
||||
|
||||
59
pkg/utility/namehelper.go
Normal file
59
pkg/utility/namehelper.go
Normal file
@ -0,0 +1,59 @@
|
||||
package utility
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
var adjectives = []string{
|
||||
"Swift", "Brave", "Clever", "Happy", "Lucky", "Mysterious", "Bold", "Fierce", "Noble", "Graceful",
|
||||
"Vivid", "Loyal", "Fearless", "Cunning", "Wise", "Radiant", "Silent", "Majestic", "Gentle", "Persistent",
|
||||
"Curious", "Agile", "Sharp", "Elegant", "Eager", "Vigorous", "Daring", "Mighty", "Witty", "Strong",
|
||||
"Bright", "Persistent", "Resilient", "Fearless", "Imaginative", "Creative", "Charming", "Playful", "Vigorous",
|
||||
"Passionate", "Dashing", "Resolute", "Adventurous", "Energetic", "Courageous",
|
||||
}
|
||||
|
||||
var animals = []string{
|
||||
"Tiger", "Panda", "Eagle", "Wolf", "Lion", "Fox", "Bear", "Falcon", "Shark", "Rabbit",
|
||||
"Elephant", "Zebra", "Cheetah", "Jaguar", "Leopard", "Giraffe", "Hawk", "Owl", "Dragon", "Whale",
|
||||
"Buffalo", "Panther", "Raven", "Vulture", "Bison", "Wolfhound", "Penguin", "Koala", "Coyote", "Crocodile",
|
||||
"Rhinoceros", "Kangaroo", "Camel", "Alligator", "Otter", "Squid", "Octopus", "Cheetah", "Lynx", "Mole",
|
||||
"Seagull", "Tiger Shark", "Wolverine", "Snow Leopard", "Bald Eagle",
|
||||
}
|
||||
|
||||
// 生成唯一昵称
|
||||
func GenerateUniqueNickname() (string, error) {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
nickname := fmt.Sprintf("%s%s%s%s", // 形容词 + 动物名 + 随机字母 + 随机数字
|
||||
adjectives[rand.Intn(len(adjectives))],
|
||||
animals[rand.Intn(len(animals))],
|
||||
randomString(1), // 随机一个字母
|
||||
randomDigits(4), // 随机 4 个数字
|
||||
)
|
||||
|
||||
return nickname, fmt.Errorf("未能生成唯一昵称")
|
||||
}
|
||||
|
||||
// 随机生成字母
|
||||
func randomString(n int) string {
|
||||
letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
result := make([]rune, n)
|
||||
for i := range result {
|
||||
result[i] = letters[rand.Intn(len(letters))]
|
||||
}
|
||||
return string(result)
|
||||
}
|
||||
|
||||
// 随机生成数字
|
||||
func randomDigits(n int) string {
|
||||
digits := []rune("0123456789")
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
result := make([]rune, n)
|
||||
for i := range result {
|
||||
result[i] = digits[rand.Intn(len(digits))]
|
||||
}
|
||||
return string(result)
|
||||
}
|
||||
21
pkg/utility/urlhelper.go
Normal file
21
pkg/utility/urlhelper.go
Normal file
@ -0,0 +1,21 @@
|
||||
package utility
|
||||
|
||||
import "net/url"
|
||||
|
||||
func ParseURLParams(rawURL string) (map[string]string, error) {
|
||||
parsedURL, err := url.Parse(rawURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
queryParams := parsedURL.Query()
|
||||
result := make(map[string]string)
|
||||
|
||||
for key, values := range queryParams {
|
||||
if len(values) > 0 {
|
||||
result[key] = values[0] // 取第一个值
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user