增加语音短信

parent 147cdbbb
...@@ -25,6 +25,7 @@ public interface Constants { ...@@ -25,6 +25,7 @@ public interface Constants {
String REDIS_PREFIX_VERIFICATION_CODE = "verificationCode_"; String REDIS_PREFIX_VERIFICATION_CODE = "verificationCode_";
String REDIS_VOICE_CODE_COUNT = "voice_verification_code_count:";
/** /**
* redis中token的key值前缀 * redis中token的key值前缀
...@@ -61,5 +62,9 @@ public interface Constants { ...@@ -61,5 +62,9 @@ public interface Constants {
interface UserAvatar { interface UserAvatar {
String AVATAR_DEFAULT = "https://avatar.xyqb.com/default_avatar.png"; String AVATAR_DEFAULT = "https://avatar.xyqb.com/default_avatar.png";
} }
interface Sms {
String VERIFICATION_CODE = "尊敬的用户,您本次的验证码为:%s,有效期10分钟。"; // 随机验证码
String BINDCARD_SMS = "用户您好,您已绑卡成功,将会在1-5个工作日内收到借款,请耐心等待。如有疑问,请致电400-002-0061,感谢您对我们的支持";//绑卡成功后的短信文案
String REPAY_SMS = "用户您好,您在信用钱包的本期账单已还款成功,保持良好的信用可升级为VIP用户,享更多特权,感谢您对信用钱包的支持";
}
} }
...@@ -5,7 +5,9 @@ import cn.quantgroup.xyqb.Constants; ...@@ -5,7 +5,9 @@ import cn.quantgroup.xyqb.Constants;
import cn.quantgroup.xyqb.aspect.captcha.CaptchaValidator; import cn.quantgroup.xyqb.aspect.captcha.CaptchaValidator;
import cn.quantgroup.xyqb.model.JsonResult; import cn.quantgroup.xyqb.model.JsonResult;
import cn.quantgroup.xyqb.service.sms.ISmsService; import cn.quantgroup.xyqb.service.sms.ISmsService;
import cn.quantgroup.xyqb.util.DateUtils;
import cn.quantgroup.xyqb.util.ValidationUtil; import cn.quantgroup.xyqb.util.ValidationUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -55,12 +57,51 @@ public class SmsController { ...@@ -55,12 +57,51 @@ public class SmsController {
return sendVerificationCode2(phoneNo); return sendVerificationCode2(phoneNo);
} }
/**
* 短信验证码: for H5
* 使用 @FPLock 注解并加入自定义限制参数, 做针对手机号的发送次数限制
*/
@CaptchaValidator
@RequestMapping("/send_sms_verification_code_voice")
public JsonResult verifyPhoneNoH5New(@RequestParam String phoneNo, @RequestParam(required = false) String registerFrom,
String usage) {
if (StringUtils.isEmpty(usage) || !"4".equals(usage)) {
LOGGER.error("参数校验失败,用户注册语音验证码usage参数为{}", usage);
return JsonResult.buildErrorStateResult("参数校验失败.", null);
}
LOGGER.info("注册-发送验证码, phoneNo:{}, registerFrom:{}", phoneNo, registerFrom);
return sendVerificationCode2Voice(phoneNo, usage);
}
@CaptchaValidator
@RequestMapping("/send_reset_code_voice")
public JsonResult resetPasswordH5New(@RequestParam String phoneNo, @RequestParam(required = false) String registerFrom,
String usage) {
if (StringUtils.isEmpty(usage) || !"5".equals(usage)) {
LOGGER.error("参数校验失败,重置密码语音验证码usage参数为{}", usage);
return JsonResult.buildErrorStateResult("参数校验失败.", null);
}
LOGGER.info("重置密码-发送验证码, phoneNo:{}, registerFrom:{}", phoneNo, registerFrom);
return sendVerificationCode2Voice(phoneNo, usage);
}
/**
* 快速登陆发送验证码
*/
@CaptchaValidator
@RequestMapping("/send_login_code_voice")
public JsonResult sendLoginCodeNew(@RequestParam String phoneNo, @RequestParam(required = false) String registerFrom,
String usage) {
if (StringUtils.isEmpty(usage) || !"6".equals(usage)) {
LOGGER.error("参数校验失败,用户登录语音验证码usage参数为{}", usage);
return JsonResult.buildErrorStateResult("参数校验失败.", null);
}
LOGGER.info("快速登陆-发送验证码, phoneNo:{}, registerFrom:{}", phoneNo, registerFrom);
return sendVerificationCode2Voice(phoneNo, usage);
}
/** /**
* 快速登陆发送验证码 * 快速登陆发送验证码
*
* @param phoneNo
* @param registerFrom
* @return
*/ */
@CaptchaValidator @CaptchaValidator
@RequestMapping("/send_login_code") @RequestMapping("/send_login_code")
...@@ -71,10 +112,6 @@ public class SmsController { ...@@ -71,10 +112,6 @@ public class SmsController {
/** /**
* 快速注册发送验证码 * 快速注册发送验证码
*
* @param phoneNo
* @param registerFrom
* @return
*/ */
@CaptchaValidator @CaptchaValidator
@RequestMapping("/send_regist_code") @RequestMapping("/send_regist_code")
...@@ -83,7 +120,6 @@ public class SmsController { ...@@ -83,7 +120,6 @@ public class SmsController {
return sendVerificationCode2(phoneNo); return sendVerificationCode2(phoneNo);
} }
private JsonResult sendVerificationCode2(String phoneNo) { private JsonResult sendVerificationCode2(String phoneNo) {
if (!ValidationUtil.validatePhoneNo(phoneNo)) { if (!ValidationUtil.validatePhoneNo(phoneNo)) {
return JsonResult.buildErrorStateResult("手机号格式有误", null); return JsonResult.buildErrorStateResult("手机号格式有误", null);
...@@ -113,4 +149,31 @@ public class SmsController { ...@@ -113,4 +149,31 @@ public class SmsController {
} }
} }
private JsonResult sendVerificationCode2Voice(String phoneNo, String usage) {
String verificationCountKey = Constants.REDIS_VOICE_CODE_COUNT + phoneNo;
Long getVerificationCount = redisTemplate.opsForHash().increment(verificationCountKey, usage.toString(), 1);
if (getVerificationCount > 5) {
return JsonResult.buildErrorStateResult("今天已获取5次语音验证码,请使用短信验证码或明天再试", null);
}
redisTemplate.expire(verificationCountKey, DateUtils.getSeconds(), TimeUnit.SECONDS);
if (!ValidationUtil.validatePhoneNo(phoneNo)) {
return JsonResult.buildErrorStateResult("手机号格式有误", null);
}
String key = Constants.REDIS_PREFIX_VERIFICATION_CODE + phoneNo;
long expire = redisTemplate.getExpire(key, TimeUnit.MINUTES);
if (expire >= EXPIRE_MINUTES - 1) {
return JsonResult.buildSuccessResult("发送成功", null);
}
String randomCode = smsIsDebug ? "000000" : String.valueOf(random.nextInt(899999) + 100000);
String uniqueId = phoneNo + UUID.randomUUID().toString().replaceAll("-", "");
MsgParams message = new MsgParams(Collections.singletonList(4), phoneNo, "1", "4", Collections.singletonList(randomCode), uniqueId);
try {
smsService.getSmsSender().sendMsg(message);
redisTemplate.opsForValue().set(key, uniqueId + ":" + randomCode, EXPIRE_MINUTES, TimeUnit.MINUTES);
return JsonResult.buildSuccessResult("发送成功", uniqueId);
} catch (Exception e) {
LOGGER.error("发送语音短信验证码失败");
return JsonResult.buildErrorStateResult("发送失败", null);
}
}
} }
package cn.quantgroup.xyqb.model.sms;
/**
* Created by xuran on 2017/7/4.
*/
public class SmsResult {
private boolean ret;
private String msg;
private SmsResult(boolean ret, String msg) {
this.ret = ret;
this.msg = msg;
}
public static SmsResult success() {
return new SmsResult(true, null);
}
public static SmsResult success(String msg) {
return new SmsResult(true, msg);
}
public static SmsResult error(String msg) {
return new SmsResult(false, msg);
}
public boolean isRet() {
return ret;
}
public void setRet(boolean ret) {
this.ret = ret;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
@Override
public String toString() {
return "SmsResult{" +
"ret=" + ret +
", msg='" + msg + '\'' +
'}';
}
}
package cn.quantgroup.xyqb.service.sms; package cn.quantgroup.xyqb.service.sms;
import cn.quantgroup.sms.SmsSender; import cn.quantgroup.sms.SmsSender;
import cn.quantgroup.xyqb.model.sms.SmsResult;
/** /**
* 短信发送服务 * 短信发送服务
...@@ -19,4 +20,5 @@ public interface ISmsService { ...@@ -19,4 +20,5 @@ public interface ISmsService {
boolean validRegisterOrResetPasswdVerificationCode(String phoneNo, String smsVerificationCode); boolean validRegisterOrResetPasswdVerificationCode(String phoneNo, String smsVerificationCode);
boolean validateFastLoginVerificationCode(String phoneNo, String verificationCode); boolean validateFastLoginVerificationCode(String phoneNo, String verificationCode);
} }
...@@ -3,6 +3,7 @@ package cn.quantgroup.xyqb.service.sms.impl; ...@@ -3,6 +3,7 @@ package cn.quantgroup.xyqb.service.sms.impl;
import cn.quantgroup.sms.MsgParams; import cn.quantgroup.sms.MsgParams;
import cn.quantgroup.sms.SmsSender; import cn.quantgroup.sms.SmsSender;
import cn.quantgroup.xyqb.Constants; import cn.quantgroup.xyqb.Constants;
import cn.quantgroup.xyqb.model.sms.SmsResult;
import cn.quantgroup.xyqb.service.sms.ISmsService; import cn.quantgroup.xyqb.service.sms.ISmsService;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -107,6 +108,7 @@ public class SmsServiceImpl implements ISmsService { ...@@ -107,6 +108,7 @@ public class SmsServiceImpl implements ISmsService {
} }
private boolean confirmSms(String smsVerificationCode, String unqiueId, String code) { private boolean confirmSms(String smsVerificationCode, String unqiueId, String code) {
try { try {
MsgParams message = new MsgParams(Collections.singletonList(2), unqiueId); MsgParams message = new MsgParams(Collections.singletonList(2), unqiueId);
......
package cn.quantgroup.xyqb.util;
import java.util.Calendar;
/**
* Created by xuran on 2017/7/4.
*/
public class DateUtils {
/**
* 计算当前时间到当天23:59:59时间差,
* 返回时间差(单位秒)
* @return
*/
public static long getSeconds() {
Calendar endOfDay = Calendar.getInstance();
endOfDay.set(Calendar.HOUR_OF_DAY, 23);
endOfDay.set(Calendar.MINUTE, 59);
endOfDay.set(Calendar.SECOND, 59);
long timeStamp = endOfDay.getTimeInMillis();
Calendar current = Calendar.getInstance();
long now = current.getTimeInMillis();
long during = (timeStamp - now) / 1000;
return during;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment