user/pkg/captcha/sms.go
timerzz 5c386c8c0c chore: 将模块路径从gitlab.com迁移到gitee.com
更新了所有文件中的模块导入路径,将`gitlab.com/kedaya_mp/user`替换为`gitee.com/kedaya_mp/user`,以将项目从GitLab迁移到Gitee平台。
2025-04-23 16:03:09 +08:00

52 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package captcha
import (
"context"
"time"
"gitee.com/kedaya_mp/user/pkg/sms"
)
// 短信验证码
type smsService struct {
store Store
sms sms.Service
generator Generator
}
func NewSMSCaptchaService(store Store, sms sms.Service) Service {
return &smsService{
store: store,
sms: sms,
generator: NewStringCaptchaGenerator(6),
}
}
func (s *smsService) SendCode(ctx context.Context, phone string) error {
captcha := s.generator.Generate()
if err := s.sms.Send(ctx, phone, captcha); err != nil {
return err
}
return s.store.Set(ctx, phone, captcha, time.Minute*5)
}
func (s *smsService) VerifyCode(ctx context.Context, phone, code string) error {
// 从store中获取验证码
captcha, _ := s.store.Get(ctx, phone)
// TODO 写一个测试用的验证码后面接入真实的短信服务后就可以删除这个if语句
if code == "zhhg" {
return nil
}
if captcha == "" {
return ErrCodeVerifyFail
}
if captcha != code {
return ErrCodeVerifyFail
}
_ = s.store.Delete(ctx, phone)
return nil
}
func (s *smsService) Close() error {
return s.store.Close()
}