2026-01-06 12:25:25 +08:00
|
|
|
|
package auth
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"bdqr-server/common/xerr"
|
|
|
|
|
|
"bdqr-server/pkg/lzkit/crypto"
|
2026-01-14 16:37:52 +08:00
|
|
|
|
"context"
|
2026-01-06 12:25:25 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
|
"math/rand"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
|
|
|
|
"bdqr-server/app/main/api/internal/svc"
|
|
|
|
|
|
"bdqr-server/app/main/api/internal/types"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type SendSmsLogic struct {
|
|
|
|
|
|
logx.Logger
|
|
|
|
|
|
ctx context.Context
|
|
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
|
|
|
|
|
|
return &SendSmsLogic{
|
|
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *SendSmsLogic) SendSms(req *types.SendSmsReq) error {
|
|
|
|
|
|
secretKey := l.svcCtx.Config.Encrypt.SecretKey
|
|
|
|
|
|
encryptedMobile, err := crypto.EncryptMobile(req.Mobile, secretKey)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "短信发送, 加密手机号失败: %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
// 检查手机号是否在一分钟内已发送过验证码
|
|
|
|
|
|
limitCodeKey := fmt.Sprintf("limit:%s:%s", req.ActionType, encryptedMobile)
|
|
|
|
|
|
exists, err := l.svcCtx.Redis.Exists(limitCodeKey)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "短信发送, 读取redis缓存失败: %s", encryptedMobile)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
|
|
// 如果 Redis 中已经存在标记,说明在 1 分钟内请求过,返回错误
|
|
|
|
|
|
return errors.Wrapf(xerr.NewErrMsg("一分钟内不能重复发送验证码"), "短信发送, 手机号1分钟内重复请求发送验证码: %s", encryptedMobile)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
code := fmt.Sprintf("%06d", rand.New(rand.NewSource(time.Now().UnixNano())).Intn(1000000))
|
|
|
|
|
|
|
|
|
|
|
|
// 发送短信
|
2026-01-19 21:10:35 +08:00
|
|
|
|
err = l.svcCtx.TencentCloudService.SendVerificationCode(req.Mobile, code)
|
2026-01-06 12:25:25 +08:00
|
|
|
|
if err != nil {
|
2026-01-19 21:10:35 +08:00
|
|
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "短信发送, 调用腾讯云短信服务失败: %v", err)
|
2026-01-06 12:25:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
codeKey := fmt.Sprintf("%s:%s", req.ActionType, encryptedMobile)
|
|
|
|
|
|
// 将验证码保存到 Redis,设置过期时间
|
|
|
|
|
|
err = l.svcCtx.Redis.Setex(codeKey, code, l.svcCtx.Config.VerifyCode.ValidTime) // 验证码有效期5分钟
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "短信发送, 验证码设置过期时间失败: %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
// 在 Redis 中设置 1 分钟的标记,限制重复请求
|
|
|
|
|
|
err = l.svcCtx.Redis.Setex(limitCodeKey, code, 60) // 标记 1 分钟内不能重复请求
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "短信发送, 验证码设置限制重复请求失败: %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|