添加再次发送短信接口

This commit is contained in:
sh
2026-04-10 11:19:02 +08:00
parent cb529480ee
commit 5eed92dd35
3 changed files with 77 additions and 10 deletions

View File

@@ -2,7 +2,6 @@ package com.ruoyi.framework.web.service;
import javax.annotation.Resource;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.cms.domain.vo.WechatAuthVO;
@@ -374,6 +373,25 @@ public class SysLoginService
}
}
/**
* 再次发送验证码
* @param dto
* @return
*/
public AjaxResult sendSmsAgain(LoginBody dto){
if (StringUtils.isEmpty(dto.getPhone())) {
return AjaxResult.error("手机号不能为空");
}
String phone = dto.getPhone();
// 60秒内不能重发Redis 防刷)
if (redisCache.hasKey(RedisUtils.SMS_SEND_LOCK + phone)) {
return AjaxResult.error("发送频繁请60秒后重试");
}
return doSendSms(phone);
}
/**
* 发送短信并缓存
* @param phone
@@ -383,27 +401,37 @@ public class SysLoginService
if (Boolean.TRUE.equals(redisCache.hasKey(RedisUtils.SMS_SEND_LOCK + phone))) {
return AjaxResult.error("验证码发送频繁请60秒后重试");
}
// 生成6位验证码
String code = RandomUtil.randomNumbers(6);
return doSendSms(phone);
}
/**
* 发送短信
*/
private AjaxResult doSendSms(String phone) {
// 生成验证码
String code = RandomCodeUtils.generateSixCode();
// 缓存验证码
redisCache.setCacheObject(RedisUtils.SMS_CODE_KEY + phone, code, RedisUtils.SMS_EXPIRE, TimeUnit.MINUTES);
// 缓存发送锁(防重)
redisCache.setCacheObject(RedisUtils.SMS_SEND_LOCK + phone, 1, RedisUtils.LOCK_EXPIRE, TimeUnit.SECONDS);
try {
SmsRequestDTO requestDTO=new SmsRequestDTO();
// 组装短信参数
SmsRequestDTO requestDTO = new SmsRequestDTO();
requestDTO.setMobile(phone);
String[] tmpValues={code,String.valueOf(RedisUtils.SMS_EXPIRE)};
String[] tmpValues = {code, String.valueOf(RedisUtils.SMS_EXPIRE)};
requestDTO.setTemplateVars(tmpValues);
AjaxResult result=smsRequestClient.sendSms(requestDTO);
//判断是否成功
if(!result.isSuccess()){
//调用发送接口
AjaxResult result = smsRequestClient.sendSms(requestDTO);
if (!result.isSuccess()) {
return result;
}
System.out.println("短信发送成功 → phone=" + phone + "code=" + code);
return AjaxResult.success("验证码发送成功");
} catch (Exception e) {
// 发送失败,删除缓存
// 发送失败,清理缓存
redisCache.deleteObject(RedisUtils.SMS_CODE_KEY + phone);
redisCache.deleteObject(RedisUtils.SMS_SEND_LOCK + phone);
System.err.println("短信发送失败:" + e.getMessage());