【更新】注册逻辑优化

This commit is contained in:
nmy 2023-12-09 10:42:16 +08:00
parent d3704ad147
commit bda31c5b1e
5 changed files with 23 additions and 9 deletions

View File

@ -126,13 +126,15 @@ public class SysLoginService
{ {
checkCode(phonenumber,code);
if (StringUtils.isAnyBlank(phonenumber,code,username, password)) if (StringUtils.isAnyBlank(phonenumber,code,username, password))
{ {
throw new ServiceException("用户/密码必须填写"); throw new ServiceException("用户/密码必须填写");
} }
checkCode(phonenumber,code);
String mobileRegEx = "^1[3,4,5,6,7,8,9][0-9]{9}$"; String mobileRegEx = "^1[3,4,5,6,7,8,9][0-9]{9}$";
Pattern pattern = Pattern.compile(mobileRegEx); Pattern pattern = Pattern.compile(mobileRegEx);
Matcher matcher = pattern.matcher(phonenumber); Matcher matcher = pattern.matcher(phonenumber);

View File

@ -13,6 +13,8 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Map;
/** /**
* Description: 腾讯云短信配置 * Description: 腾讯云短信配置
* *
@ -39,7 +41,7 @@ public class SmsComponent {
private String signName; private String signName;
@Value("${tencent.sms.templateId}") @Value("${tencent.sms.templateId}")
private String templateCodeId; private Map<Integer,String> templateCodeIds;
@Value("${tencent.sms.timeout}") @Value("${tencent.sms.timeout}")
private Integer timeout; private Integer timeout;
@ -92,11 +94,11 @@ public class SmsComponent {
* @param code 验证码 * @param code 验证码
* @param param2 分钟参数可为空 * @param param2 分钟参数可为空
*/ */
public void sendCode(String phone, String code, String param2) { public void sendCode(String phone, String code, String param2,Integer type) {
// 返回的resp是一个SendSmsResponse的实例与请求对象对应 // 返回的resp是一个SendSmsResponse的实例与请求对象对应
SendSmsResponse resp; SendSmsResponse resp;
try { try {
resp = getClient().SendSms(getReqTwo(phone, code,param2 ,templateCodeId)); // 模板id是自己设置好的 resp = getClient().SendSms(getReqTwo(phone, code,param2 ,templateCodeIds.get(type))); // 模板id是自己设置好的
log.info(SendSmsResponse.toJsonString(resp)); // 把返回信息输入到日志中 log.info(SendSmsResponse.toJsonString(resp)); // 把返回信息输入到日志中
} catch (TencentCloudSDKException e) { } catch (TencentCloudSDKException e) {
log.error("腾讯云短信发送失败"); log.error("腾讯云短信发送失败");

View File

@ -24,9 +24,9 @@ public class SmsSendController extends BaseController {
private ISmsService smsService; private ISmsService smsService;
@PostMapping("send") @PostMapping("send")
public AjaxResult send(@RequestParam("phone") String phone){ public AjaxResult send(@RequestParam("phone") String phone,@RequestParam("type") Integer type){
try { try {
String msg = smsService.sendCode(phone, 5 * 60 * 1000); String msg = smsService.sendCode(phone, 5 * 60 * 1000,type);
return success(msg); return success(msg);
} catch (Exception e) { } catch (Exception e) {
return error(e.getMessage()); return error(e.getMessage());

View File

@ -14,7 +14,7 @@ public interface ISmsService {
* @param leastTime 短信有效时间 * @param leastTime 短信有效时间
* @return 结果 * @return 结果
*/ */
public String sendCode( String phone,int leastTime); public String sendCode(String phone,int leastTime,Integer type);
/** /**
* 校验验证码 * 校验验证码

View File

@ -11,6 +11,8 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Random; import java.util.Random;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* Description: 短信发送接口实现 * Description: 短信发送接口实现
@ -35,11 +37,19 @@ public class ISmsServiceImpl implements ISmsService {
* @throws ServiceException 异常信息 * @throws ServiceException 异常信息
*/ */
@Override @Override
public String sendCode(String phone, int leastTime) { public String sendCode(String phone, int leastTime,Integer type) {
if (phone == null || phone.isEmpty()) { if (phone == null || phone.isEmpty()) {
throw new ServiceException("手机号为空"); throw new ServiceException("手机号为空");
} }
String mobileRegEx = "^1[3,4,5,6,7,8,9][0-9]{9}$";
Pattern pattern = Pattern.compile(mobileRegEx);
Matcher matcher = pattern.matcher(phone);
if (!matcher.matches()) {
throw new ServiceException("手机号格式错误");
}
//从redis获取有效时间 如果相差小于1分钟不可以重新发送 //从redis获取有效时间 如果相差小于1分钟不可以重新发送
long expire = redisCache.getExpire(RedisConstants.REDIS_PHONE_CODE_KEY + phone); long expire = redisCache.getExpire(RedisConstants.REDIS_PHONE_CODE_KEY + phone);
@ -51,7 +61,7 @@ public class ISmsServiceImpl implements ISmsService {
String code = getSixBitCode(); // 生成新的验证码 String code = getSixBitCode(); // 生成新的验证码
//存储 phone -> code到Redis中 //存储 phone -> code到Redis中
redisCache.setCacheObject(RedisConstants.REDIS_PHONE_CODE_KEY + phone, code, Long.parseLong(leastTime+""), TimeUnit.MILLISECONDS); redisCache.setCacheObject(RedisConstants.REDIS_PHONE_CODE_KEY + phone, code, Long.parseLong(leastTime+""), TimeUnit.MILLISECONDS);
smsComponent.sendCode(phone, code,""); smsComponent.sendCode(phone, code,"",type);
return "已发送验证码 " + phone; return "已发送验证码 " + phone;
} }