发送短信按钮倒计时服务器端支持
发送成功之后才计时:
- /***
- * 服务器端启动倒计时,即记录时间戳
- *
- * @param mobile
- */
- private void setSMSLimit(String mobile) {
- HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
- HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
- saveExpxKeyCache(request, response, mobile, String.valueOf(DateTimeUtil.getCurrentTimeSecond()), 60);
- }
上述代码中的saveExpxKeyCache 是关键方法,用于记录时间戳
- public /*synchronized*/ void saveExpxKeyCache(HttpServletRequest request, HttpServletResponse response, String k, String v,long time) {
- String cid = getCid(request, response);
- // logger.debug("cid is " + cid);
- // logger.debug("set " + k + " to value : " + v);
- RedisHelper helper = RedisHelper.getInstance();
- helper.saveExpxKeyCache(cid+k, v,time);
- }
对上述方法的说明:
参数说明:
(1)k:redis的key,比如手机号;
(2)v:redis的value,比如发送短信时的时间戳;
(3)time:超时时间
time就是发送短信间隔的时间,所以就是60秒,这个方法的功能:
60秒之后,这条记录被自动删除.
发送短信之前得检查是否在60秒之内发送短信:
- public WapResponseDto sendMobileSMS(HttpServletRequest request, HttpServletResponse response,
- String mobile,
- String type/*,
- String authImage*/) {
- WapResponseDto wapResponseDto = null;
- //检查格式
- if (!StringUtil.isMobileNumber(mobile)) {
- wapResponseDto = new WapResponseDto();
- wapResponseDto.setResult(false);
- wapResponseDto.setErrorFieldName("mobile");
- wapResponseDto.setErrorMessage(Constant.ERROR_MESSAGE_MOBILE_FORMAT_INVALID);
- return wapResponseDto;
- }
- WapResponseDto SMSLimitResponseDto = verifySMSLimit(mobile);
- if (!StringUtil.isNullOrEmpty(SMSLimitResponseDto)) {//检查一分钟之内不能重复发送手机短信
- return SMSLimitResponseDto;
- }
- /***
- * 检查一分钟之内不能重复发送手机短信
- *
- * @param mobile
- * @return : 返回null则表示校验通过
- */
- public WapResponseDto verifySMSLimit(String mobile) {
- int remainingTime = sMSWaitingTime(mobile, false);//一分钟之内不能重复发送手机短信
- if (remainingTime > 0) {
- SMSRemainingTimeDto sMSRemainingTimeDto = new SMSRemainingTimeDto();
- sMSRemainingTimeDto.setResult(false);
- sMSRemainingTimeDto.setErrorMessage(Constant.getErrorMsg("a00001"));
- sMSRemainingTimeDto.setRemainingSecond(remainingTime);
- return sMSRemainingTimeDto;
- }
- return null;
- }
sMSWaitingTime 用于检查还要等待多长时间:
- /***
- * 倒计时还剩余多长时间
- * @param mobile : 手机号
- * @param reallySendSMS : 是否真正发送短信
- * @return : second
- */
- public int sMSWaitingTime(String mobile,boolean reallySendSMS) {
- HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
- HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
- RedisHelper rdsHelper = RedisHelper.getInstance();
- String cid = getCid(request, response);
- String lastSendSMSTime = rdsHelper.getCache(cid+mobile);
- if(StringUtil.isNullOrEmpty(lastSendSMSTime)) {
- if(reallySendSMS){
- saveExpxKeyCache(request, response, mobile, String.valueOf(DateTimeUtil.getCurrentTimeSecond()),60);
- }
- return 0;//不需要倒计时
- } else {
- long lastSendSMSTimeSecond=Long.parseLong(lastSendSMSTime);
- long currentTimeSecond=DateTimeUtil.getCurrentTimeSecond();
- int delter=(int) (currentTimeSecond-lastSendSMSTimeSecond);
- if(delter>=60){
- return 0;//不需要倒计时
- }else{
- return 60-delter;
- }
- }
- }
注意:如果超过60秒,lastSendSMSTime 应该是为null的.因为我通过saveExpxKeyCache 设置了超时时间
时间: 2024-10-03 02:08:03