LicenseManger/internal/utils/captcha.go

32 lines
824 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package utils
import (
"crypto/rand"
"fmt"
)
// GenerateCaptcha 生成6位数字验证码
func GenerateCaptcha() (string, error) {
// 生成6位随机数字
b := make([]byte, 3)
if _, err := rand.Read(b); err != nil {
return "", err
}
// 将随机字节转换为6位数字
num := int(b[0])<<16 | int(b[1])<<8 | int(b[2])
return fmt.Sprintf("%06d", num%1000000), nil
}
// GenerateEmailCaptchaContent 生成验证码邮件内容
func GenerateEmailCaptchaContent(code, username, action string) string {
return fmt.Sprintf(`
<h3>验证码</h3>
<p>您好,%s</p>
<p>您正在进行%s操作验证码为</p>
<h2 style="color: #1890ff;">%s</h2>
<p>验证码有效期为5分钟请勿泄露给他人。</p>
<p>如果这不是您的操作,请忽略此邮件。</p>
`, username, action, code)
}