diff --git a/config/config.yaml b/config/config.yaml index 5c6a5270..94098d65 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -76,7 +76,7 @@ verifyCode: maxCount: 10 # 单位时间内最大获取次数 superCode: "666666" # 超级验证码(只有use为空时使用) len: 6 # 验证码长度 - use: "" # 使用的验证码服务(use: "ali") + use: "ali" # 使用的验证码服务(use: "ali") ali: endpoint: "dysmsapi.aliyuncs.com" accessKeyId: "" @@ -88,7 +88,7 @@ verifyCode: senderMail: "" # 发送者 senderAuthorizationCode: "" # 授权码 smtpAddr: "smtp.qq.com" # smtp 服务器地址 - smtpPort: 25 # smtp 服务器邮件发送端口 + smtpPort: 465 # smtp 服务器邮件发送端口 testDepartMentID: 001 imAPIURL: http://127.0.0.1:10002 diff --git a/internal/api/chat.go b/internal/api/chat.go index 0d334442..3df4bab4 100644 --- a/internal/api/chat.go +++ b/internal/api/chat.go @@ -53,7 +53,7 @@ type ChatApi struct { // ################## ACCOUNT ################## func (o *ChatApi) SendVerifyCode(c *gin.Context) { - var req chat.SendVerifyCodeReq + req := chat.SendVerifyCodeReq{} if err := c.BindJSON(&req); err != nil { apiresp.GinError(c, err) return diff --git a/internal/rpc/chat/login.go b/internal/rpc/chat/login.go index 6cf70509..b5b9a7ac 100644 --- a/internal/rpc/chat/login.go +++ b/internal/rpc/chat/login.go @@ -44,21 +44,21 @@ func (o *chatSvr) verifyCodeJoin(areaCode, phoneNumber string) string { func (o *chatSvr) SendVerifyCode(ctx context.Context, req *chat.SendVerifyCodeReq) (*chat.SendVerifyCodeResp, error) { defer log.ZDebug(ctx, "return") - switch req.UsedFor { + switch int(req.UsedFor) { case constant.VerificationCodeForRegister: if err := o.Admin.CheckRegister(ctx, req.Ip); err != nil { return nil, err } - if req.AreaCode == "" || req.PhoneNumber == "" { - return nil, errs.ErrArgs.Wrap("area code or phone number is empty") - } - if req.AreaCode[0] != '+' { - return nil, errs.ErrArgs.Wrap("area code must start with +") - } - if _, err := strconv.ParseUint(req.AreaCode[1:], 10, 64); err != nil { - return nil, errs.ErrArgs.Wrap("area code must be number") - } - if req.PhoneNumber != "" { + if req.Email == "" { + if req.AreaCode == "" || req.PhoneNumber == "" { + return nil, errs.ErrArgs.Wrap("area code or phone number is empty") + } + if req.AreaCode[0] != '+' { + return nil, errs.ErrArgs.Wrap("area code must start with +") + } + if _, err := strconv.ParseUint(req.AreaCode[1:], 10, 64); err != nil { + return nil, errs.ErrArgs.Wrap("area code must be number") + } if _, err := strconv.ParseUint(req.PhoneNumber, 10, 64); err != nil { return nil, errs.ErrArgs.Wrap("phone number must be number") } @@ -101,6 +101,15 @@ func (o *chatSvr) SendVerifyCode(ctx context.Context, req *chat.SendVerifyCodeRe default: return nil, errs.ErrArgs.Wrap("used unknown") } + var account string + var isEmail bool + if req.Email == "" { + account = o.verifyCodeJoin(req.AreaCode, req.PhoneNumber) + } else { + isEmail = true + account = req.Email + } + verifyCode := config.Config.VerifyCode if verifyCode.UintTime == 0 || verifyCode.MaxCount == 0 { return nil, errs.ErrNoPermission.Wrap("verify code disabled") @@ -112,21 +121,21 @@ func (o *chatSvr) SendVerifyCode(ctx context.Context, req *chat.SendVerifyCodeRe return &chat.SendVerifyCodeResp{}, nil } now := time.Now() - count, err := o.Database.CountVerifyCodeRange(ctx, o.verifyCodeJoin(req.AreaCode, req.PhoneNumber), now.Add(-time.Duration(verifyCode.UintTime)*time.Second), now) - if err != nil { - return nil, err + + var count uint32 + var err error + if !isEmail { + count, err = o.Database.CountVerifyCodeRange(ctx, o.verifyCodeJoin(req.AreaCode, req.PhoneNumber), now.Add(-time.Duration(verifyCode.UintTime)*time.Second), now) + if err != nil { + return nil, err + } + } else { + count, err = o.Database.CountVerifyCodeRange(ctx, req.Email, now.Add(-time.Duration(verifyCode.UintTime)*time.Second), now) } if verifyCode.MaxCount < int(count) { return nil, eerrs.ErrVerifyCodeSendFrequently.Wrap() } - var account string - var isEmail bool - if req.PhoneNumber != "" { - account = o.verifyCodeJoin(req.AreaCode, req.PhoneNumber) - } else { - isEmail = true - account = req.Email - } + t := &chat2.VerifyCode{ Account: account, Code: o.genVerifyCode(), @@ -146,6 +155,7 @@ func (o *chatSvr) SendVerifyCode(ctx context.Context, req *chat.SendVerifyCodeRe if err != nil { return nil, err } + defer log.ZDebug(ctx, "last", "email", req.Email, "account", account, "isEmail", isEmail) return &chat.SendVerifyCodeResp{}, nil } diff --git a/pkg/email/mail.go b/pkg/email/mail.go index 73178a41..9db4bd61 100644 --- a/pkg/email/mail.go +++ b/pkg/email/mail.go @@ -17,9 +17,9 @@ package email import ( "context" "fmt" - "github.com/OpenIMSDK/chat/pkg/common/config" "github.com/OpenIMSDK/tools/errs" + "github.com/OpenIMSDK/tools/log" "gopkg.in/gomail.v2" ) @@ -30,7 +30,7 @@ func NewMail() (Mail, error) { config.Config.VerifyCode.Mail.SmtpPort, config.Config.VerifyCode.Mail.SenderMail, config.Config.VerifyCode.Mail.SenderAuthorizationCode) - + return &mail{dail: dail}, nil } @@ -56,5 +56,6 @@ func (a *mail) SendMail(ctx context.Context, mail string, verifyCode string) err // Send err := a.dail.DialAndSend(m) + defer log.ZDebug(ctx, "return", mail, verifyCode, err) return errs.Wrap(err) }