手机号次数限制
This commit is contained in:
parent
397b31a548
commit
32b80e0d6c
|
|
@ -89,6 +89,14 @@ public class TokenController {
|
||||||
|
|
||||||
@PostMapping("sendCode")
|
@PostMapping("sendCode")
|
||||||
public R<?> sendCode(@RequestBody LoginBody form) {
|
public R<?> sendCode(@RequestBody LoginBody form) {
|
||||||
|
String uuid = form.getUuid();
|
||||||
|
String captcha = redisService.getCacheObject(CacheConstants.CAPTCHA_CODE_KEY + uuid).toString();
|
||||||
|
if (StringUtils.isBlank(captcha)) {
|
||||||
|
return R.fail("验证码超时,请重新刷新");
|
||||||
|
}
|
||||||
|
if (form.getCode() != null && !form.getCode().equals(captcha)) {
|
||||||
|
return R.fail("验证码错误");
|
||||||
|
}
|
||||||
R<Boolean> sendState = remoteUserService.sendCode(form.getPhone());
|
R<Boolean> sendState = remoteUserService.sendCode(form.getPhone());
|
||||||
return sendState;
|
return sendState;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -87,4 +87,9 @@ public class UserConstants
|
||||||
* 手机号长度限制
|
* 手机号长度限制
|
||||||
*/
|
*/
|
||||||
public static final int PHONE_DEFAULT_LENGTH_LOGIN = 11;
|
public static final int PHONE_DEFAULT_LENGTH_LOGIN = 11;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 手机1天限制
|
||||||
|
*/
|
||||||
|
public static final String MOBILE_PHONE_1D_LIMIT_DIR = "mobilePhoneDay:";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -84,18 +84,43 @@ public class SysSmsServiceImpl implements ISysSmsService {
|
||||||
public AjaxResult codeLogin(String phone) {
|
public AjaxResult codeLogin(String phone) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// 检查发送次数是否超限
|
||||||
|
if (isOverLimit(phone)) {
|
||||||
|
throw new ServiceException("当天此手机号发送验证码次数超过限制", 1001);
|
||||||
|
}
|
||||||
//获取六位验证码
|
//获取六位验证码
|
||||||
String code = getSixBitCode();
|
String code = getSixBitCode();
|
||||||
//调用发送短信的方法
|
//调用发送短信的方法
|
||||||
String body = sendMessageNew(phone,tencentSmsConfig.getTemplateId().get(1),code);
|
String body = sendMessageNew(phone,tencentSmsConfig.getTemplateId().get(1),code);
|
||||||
// 存储验证码至Redis中,键值为:code_15588886157 , 有效期5,时间颗粒度为MINUTES:分钟
|
// 存储验证码至Redis中,键值为:code_15588886157 , 有效期5,时间颗粒度为MINUTES:分钟
|
||||||
redisService.setCacheObject("code_" + phone, code, 5L, TimeUnit.MINUTES);
|
redisService.setCacheObject("code_" + phone, code, 5L, TimeUnit.MINUTES);
|
||||||
return success("手机号:" + phone + ",用户登录验证码:" + code + ",返回结果:" + body);
|
String key = UserConstants.MOBILE_PHONE_1D_LIMIT_DIR + phone;
|
||||||
|
Integer sendCount = redisService.getCacheObject(key);
|
||||||
|
if (sendCount == null) {
|
||||||
|
sendCount = 1;
|
||||||
|
} else {
|
||||||
|
sendCount++;
|
||||||
|
}
|
||||||
|
//存储一天手机号发送验证码数据
|
||||||
|
redisService.setCacheObject(key, sendCount, 1L, TimeUnit.DAYS);
|
||||||
|
return success("手机号:" + phone + ",短信验证码发送成功 !");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return AjaxResult.error("发送失败:" + e.getMessage());
|
return AjaxResult.error("发送失败:" + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查发送次数是否超过限制
|
||||||
|
* @param phone 手机号
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private boolean isOverLimit(String phone) {
|
||||||
|
String key = UserConstants.MOBILE_PHONE_1D_LIMIT_DIR + phone;
|
||||||
|
Integer sendCount = redisService.getCacheObject(key);
|
||||||
|
// 限制每个手机号每天最多发送10次
|
||||||
|
return sendCount != null && sendCount >= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送消息msg到手机
|
* 发送消息msg到手机
|
||||||
|
|
@ -177,10 +202,6 @@ public class SysSmsServiceImpl implements ISysSmsService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
System.out.println(getSixBitCode());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 随机生成6位验证码
|
* 随机生成6位验证码
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue