添加再次发送短信接口
This commit is contained in:
@@ -97,6 +97,11 @@ public class SysLoginController
|
||||
return ajax;
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信+手机验证码登录
|
||||
* @param loginBody
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/app/appLoginPhone")
|
||||
public AjaxResult appLoginPhone(@RequestBody LoginBody loginBody)
|
||||
{
|
||||
@@ -105,6 +110,17 @@ public class SysLoginController
|
||||
return ajax;
|
||||
}
|
||||
|
||||
/**
|
||||
* 再次发送验证码
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/app/sendSmsAgain")
|
||||
private AjaxResult sendSmsAgain(@RequestBody LoginBody loginBody){
|
||||
AjaxResult ajax = AjaxResult.success();
|
||||
ajax=loginService.sendSmsAgain(loginBody);
|
||||
return ajax;
|
||||
}
|
||||
|
||||
/**
|
||||
* 一体机身份证登录
|
||||
* @param loginBody
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
|
||||
/**
|
||||
* 验证码通用工具类(全项目复用)
|
||||
*/
|
||||
public class RandomCodeUtils {
|
||||
|
||||
/**
|
||||
* 生成 6 位数字验证码
|
||||
*/
|
||||
public static String generateSixCode() {
|
||||
return RandomUtil.randomNumbers(6);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成指定位数数字验证码
|
||||
*/
|
||||
public static String generateNumberCode(int length) {
|
||||
return RandomUtil.randomNumbers(length);
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user