增加1分钟不能重复获取验证码
This commit is contained in:
parent
beb76ea959
commit
3383f61bf5
@ -2,7 +2,6 @@ package com.xhpc.general.service;
|
|||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.xhpc.common.core.utils.HttpUtils;
|
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.core.web.domain.AjaxResult;
|
||||||
import com.xhpc.common.redis.service.RedisService;
|
import com.xhpc.common.redis.service.RedisService;
|
||||||
import com.xhpc.general.domain.XhpcSms;
|
import com.xhpc.general.domain.XhpcSms;
|
||||||
@ -13,6 +12,7 @@ import org.springframework.stereotype.Service;
|
|||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
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.Matcher;
|
||||||
@ -57,18 +57,28 @@ public class XhpcSmsServiceImpl implements IXhpcSmsService{
|
|||||||
//添加短信记录
|
//添加短信记录
|
||||||
XhpcSms xhpcSms =new XhpcSms();
|
XhpcSms xhpcSms =new XhpcSms();
|
||||||
try {
|
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 conten ="【小华充电】您的验证码是:"+random+",有效期为5分钟。如非本人操作,可不用理会。";
|
||||||
String req = HttpUtils.postFormData(URL, null, assembleSmsReq(phone,conten));
|
String req = HttpUtils.postFormData(URL, null, assembleSmsReq(phone,conten));
|
||||||
JSONObject json = JSONObject.parseObject(req);
|
JSONObject json = JSONObject.parseObject(req);
|
||||||
xhpcSms.setCode(random);
|
xhpcSms.setCode(random);
|
||||||
xhpcSms.setPhone(phone);
|
xhpcSms.setPhone(phone);
|
||||||
xhpcSms.setContent(conten);
|
xhpcSms.setContent(conten);
|
||||||
xhpcSms.setCreateTime(new Date());
|
xhpcSms.setCreateTime(new Date());
|
||||||
xhpcSms.setRemark(req);
|
xhpcSms.setRemark(req);
|
||||||
String ok = json.getString("ok");
|
String ok = json.getString("ok");
|
||||||
if("true".equals(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);
|
xhpcSms.setStatus(0);
|
||||||
xhpcSmsMapper.addXhpcSms(xhpcSms);
|
xhpcSmsMapper.addXhpcSms(xhpcSms);
|
||||||
return AjaxResult.success();
|
return AjaxResult.success();
|
||||||
@ -96,13 +106,13 @@ public class XhpcSmsServiceImpl implements IXhpcSmsService{
|
|||||||
return params;
|
return params;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Long getRandom(){
|
private String getRandom(){
|
||||||
Random rnd = new Random();
|
Random rnd = new Random();
|
||||||
int i = rnd.nextInt(999999);
|
int i = rnd.nextInt(999999);
|
||||||
if(i<100000){
|
if(i<100000){
|
||||||
i=i+100000;
|
i=i+100000;
|
||||||
}
|
}
|
||||||
return Long.valueOf(i);
|
return i+"";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user