增加1分钟不能重复获取验证码

This commit is contained in:
yuyang 2021-08-03 11:54:30 +08:00
parent beb76ea959
commit 3383f61bf5

View File

@ -2,7 +2,6 @@ package com.xhpc.general.service;
import com.alibaba.fastjson.JSONObject;
import com.xhpc.common.core.utils.HttpUtils;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.redis.service.RedisService;
import com.xhpc.general.domain.XhpcSms;
@ -13,6 +12,7 @@ import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
@ -57,18 +57,28 @@ public class XhpcSmsServiceImpl implements IXhpcSmsService{
//添加短信记录
XhpcSms xhpcSms =new XhpcSms();
try {
String random = StringUtils.valueOf(getRandom());
//用户使用的Key
String pvToken = "pvToken:"+phone;
//用户频繁调用的判断的Key
String token = "token:"+phone;
String cacheObject = REDIS.getCacheObject(token);
if(cacheObject !=null){
return AjaxResult.error("1012","操作过于频繁请于1分钟后重试");
}
String random = getRandom();
String conten ="【小华充电】您的验证码是:"+random+"有效期为5分钟。如非本人操作可不用理会。";
String req = HttpUtils.postFormData(URL, null, assembleSmsReq(phone,conten));
JSONObject json = JSONObject.parseObject(req);
xhpcSms.setCode(random);
xhpcSms.setCode(random);
xhpcSms.setPhone(phone);
xhpcSms.setContent(conten);
xhpcSms.setCreateTime(new Date());
xhpcSms.setRemark(req);
String ok = json.getString("ok");
if("true".equals(ok)){
REDIS.setCacheObject(phone,random,300L, TimeUnit.SECONDS);
REDIS.setCacheObject(pvToken,random,300L, TimeUnit.SECONDS);
//1分钟有效时间设置防止用户频繁调用
REDIS.setCacheObject(token,random,60L, TimeUnit.SECONDS);
xhpcSms.setStatus(0);
xhpcSmsMapper.addXhpcSms(xhpcSms);
return AjaxResult.success();
@ -96,13 +106,13 @@ public class XhpcSmsServiceImpl implements IXhpcSmsService{
return params;
}
private Long getRandom(){
private String getRandom(){
Random rnd = new Random();
int i = rnd.nextInt(999999);
if(i<100000){
i=i+100000;
}
return Long.valueOf(i);
return i+"";
}