This commit is contained in:
@@ -359,6 +359,64 @@ func (m *users) gen4Digits() (string, error) {
|
||||
return fmt.Sprintf("%04d", n.Int64()), nil
|
||||
}
|
||||
|
||||
// SetPhoneCode 手动设置短信验证码(后台操作);默认有效期 5 分钟,不受发送频率限制。
|
||||
func (m *users) SetPhoneCode(ctx context.Context, phone, code string, ttl time.Duration) (*models.SmsCodeSend, error) {
|
||||
phone = m.normalizePhone(phone)
|
||||
code = strings.TrimSpace(code)
|
||||
if phone == "" {
|
||||
return nil, errors.New("手机号不能为空")
|
||||
}
|
||||
if code == "" {
|
||||
return nil, errors.New("验证码不能为空")
|
||||
}
|
||||
if len(code) != 4 {
|
||||
return nil, errors.New("验证码必须为 4 位数字")
|
||||
}
|
||||
for _, r := range code {
|
||||
if r < '0' || r > '9' {
|
||||
return nil, errors.New("验证码必须为 4 位数字")
|
||||
}
|
||||
}
|
||||
|
||||
// 前置校验:手机号必须已注册
|
||||
_, err := m.FindByPhone(ctx, phone)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errors.New("手机号未注册,请联系管理员开通")
|
||||
}
|
||||
return nil, errors.Wrap(err, "failed to find user by phone")
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
if ttl <= 0 {
|
||||
ttl = 5 * time.Minute
|
||||
}
|
||||
expiresAt := now.Add(ttl)
|
||||
|
||||
m.mu.Lock()
|
||||
m.ensurePhoneAuthMaps()
|
||||
m.codeByPhone[phone] = phoneCodeEntry{code: code, expiresAt: expiresAt}
|
||||
m.lastSentAtByPhone[phone] = now
|
||||
m.mu.Unlock()
|
||||
|
||||
if _db == nil {
|
||||
return nil, errors.New("db not initialized")
|
||||
}
|
||||
|
||||
record := &models.SmsCodeSend{
|
||||
Phone: phone,
|
||||
Code: code,
|
||||
SentAt: now,
|
||||
ExpiresAt: expiresAt,
|
||||
}
|
||||
if err := _db.WithContext(ctx).Create(record).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Infof("SetPhoneCode to %s: code=%s", phone, code)
|
||||
return record, nil
|
||||
}
|
||||
|
||||
// SendPhoneCode 发送短信验证码(内存限流:同一手机号 58s 内仅允许发送一次;验证码 5 分钟过期)。
|
||||
func (m *users) SendPhoneCode(ctx context.Context, phone string) error {
|
||||
phone = m.normalizePhone(phone)
|
||||
|
||||
Reference in New Issue
Block a user