Files
rootandClaude Opus 5 8679200f41 Initial commit
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-15 13:57:10 +08:00

55 lines
1.6 KiB
Go

package emailser
import (
"context"
"strings"
"91porn-server/app/appg"
"91porn-server/common/constant/redisconst"
"91porn-server/common/log"
"91porn-server/common/stderr"
"github.com/badoux/checkmail"
)
// CheckEmail 校验邮件地址是否合法
func CheckEmail(ctx context.Context, emailStr string) stderr.Code {
if emailStr == "" {
return stderr.ErrInvalidEmail
}
if err := checkmail.ValidateFormat(emailStr); err != nil {
log.ErrorX(ctx, "invalid email", log.Any("email", emailStr), log.E(err))
return stderr.ErrInvalidEmail
}
return stderr.Success
}
// VerifyCaptchaCode 核对验证码
func VerifyCaptchaCode(ctx context.Context, emailAddr, code string) stderr.Code {
emailAddr = strings.TrimSpace(emailAddr)
if strings.HasSuffix(emailAddr, "@dsp.xyz") { // pass the test email
log.InfoX(ctx, "VerifyCaptchaCode dismiss test email", log.Any("email", emailAddr))
return stderr.Success
}
if code := CheckEmail(ctx, emailAddr); code != stderr.Success {
return code
}
redisKey := redisconst.GetEmailCaptchaKey(emailAddr)
redisCode, err := appg.Redis.Get(redisKey)
if err != nil {
log.ErrorX(ctx, "VerifyCaptchaCode redis get err", log.Any("emailAddr", emailAddr),
log.Any("redisKey", redisKey), log.E(err))
return stderr.ErrNetWorkBusy
}
if redisCode == nil {
log.InfoX(ctx, "VerifyCaptchaCode nil code in redis", log.Any("emailAddr", emailAddr), log.Any("code", code))
return stderr.ErrCaptcha
}
if *redisCode != code {
log.InfoX(ctx, "VerifyCaptchaCode invalid", log.Any("emailAddr", emailAddr), log.Any("code", code),
log.Any("redisCode", redisCode))
return stderr.ErrCaptchaInvalid
}
return stderr.Success
}