Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix the #268

Closed
wants to merge 11 commits into from
4 changes: 2 additions & 2 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ""
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion internal/api/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 32 additions & 22 deletions internal/rpc/chat/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
Expand All @@ -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(),
Expand All @@ -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
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/email/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}

Expand All @@ -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)
}
Loading