|
@@ -44,59 +44,37 @@ public class SmsServiceImpl implements ISmsService {
|
|
|
|
|
|
/**
|
|
|
* 发送短信验证码
|
|
|
- * @param type
|
|
|
- * @param phone
|
|
|
+ * @param map
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
- public Response sendVerificationCode(String type, String phone) {
|
|
|
+ public Response sendVerificationCode(Map map) {
|
|
|
//字符编码
|
|
|
String charset = "UTF-8";
|
|
|
|
|
|
//type验证
|
|
|
List<String> collect = Arrays.stream(SmsTemplateConstants.smsTemplateType)
|
|
|
.parallel()
|
|
|
- .filter(str -> str.equals(type))
|
|
|
+ .filter(str -> str.equals(map.get("type")))
|
|
|
.collect(Collectors.toList());
|
|
|
if (collect.size()==0){
|
|
|
return Response.error(500,"参数{type}类型错误");
|
|
|
}
|
|
|
|
|
|
-
|
|
|
//手机号格式验证
|
|
|
- if(phone.length() != 11){
|
|
|
+ if(map.get("phone").toString().length() != 11){
|
|
|
return Response.error(500,"手机格式错误");
|
|
|
} else {
|
|
|
Pattern pattern = Pattern.compile(MOBILE_REGEX);
|
|
|
- Matcher matcher = pattern.matcher(phone);
|
|
|
+ Matcher matcher = pattern.matcher(map.get("phone").toString());
|
|
|
if(!matcher.matches()) {
|
|
|
return Response.error(500,"手机号格式错误");
|
|
|
}
|
|
|
}
|
|
|
try{
|
|
|
|
|
|
- //短信模板
|
|
|
- String smsTemplateStr = "";
|
|
|
-
|
|
|
- /**
|
|
|
- * login:登录验证 modify;修改验证
|
|
|
- * keyPrefix : 缓存key前缀
|
|
|
- * verificationCount : 验证次数
|
|
|
- */
|
|
|
- Map<String, String> map = new HashMap<>();
|
|
|
- if (type.equals("login")){
|
|
|
- map.put("keyPrefix", Constants.SMS_LOGIN_CODE);
|
|
|
- map.put("verificationCount",Constants.SMS_LOGIN_CODE_COUNT);
|
|
|
- smsTemplateStr = SmsTemplateConstants.login;
|
|
|
- }
|
|
|
- if (type.equals("modify")){
|
|
|
- map.put("keyPrefix",Constants.SMS_MODIFY_CODE);
|
|
|
- map.put("verificationCount",Constants.SMS_MODIFY_CODE_COUNT);
|
|
|
- smsTemplateStr = SmsTemplateConstants.modify;
|
|
|
- }
|
|
|
-
|
|
|
//验证手机号
|
|
|
- Response resultJson = validateMobile(phone,map);
|
|
|
+ Response resultJson = validateMobile(map);
|
|
|
if("500".equals(resultJson.getCode().toString())) {
|
|
|
return resultJson;
|
|
|
}
|
|
@@ -106,13 +84,13 @@ public class SmsServiceImpl implements ISmsService {
|
|
|
for (int i=0;i<6;i++){
|
|
|
code += new Random().nextInt(10);
|
|
|
}
|
|
|
- String content = smsTemplateStr.replace("code",code);
|
|
|
+ String content = map.get("smsTemplateConstants").toString().replace("code",code);
|
|
|
|
|
|
// 调用发送接口
|
|
|
Client client = new Client(smsUserName, smsPassword);
|
|
|
String content1 = URLEncoder.encode(content, charset);
|
|
|
String ecodeContent = URLEncoder.encode(content1, charset);
|
|
|
- String result = client.mtData(ecodeContent, phone, "", "", "", charset);
|
|
|
+ String result = client.mtData(ecodeContent, map.get("phone").toString(), "", "", "", charset);
|
|
|
String strCode = result.split("\n")[0];
|
|
|
long codenum = 0;
|
|
|
codenum = Long.valueOf(strCode);
|
|
@@ -120,7 +98,7 @@ public class SmsServiceImpl implements ISmsService {
|
|
|
if (codenum > 0) {//成功提交
|
|
|
Info = "发送成功";
|
|
|
// 写入缓存中
|
|
|
- writeCache(phone, code,map);
|
|
|
+ writeCache(code,map);
|
|
|
return Response.success(Info);
|
|
|
|
|
|
} else if (codenum == 0) {
|
|
@@ -159,19 +137,21 @@ public class SmsServiceImpl implements ISmsService {
|
|
|
|
|
|
/**
|
|
|
* 手机号验证
|
|
|
- * @param mobile
|
|
|
+ * keyPrefix : 缓存key前缀
|
|
|
+ * verificationCount : 验证次数
|
|
|
+ * @param map
|
|
|
* @return
|
|
|
*/
|
|
|
- private Response validateMobile(final String mobile,Map map){
|
|
|
+ private Response validateMobile(Map map){
|
|
|
|
|
|
- if(redisTemplate.hasKey(map.get("verificationCount") + mobile)){
|
|
|
+ if(redisTemplate.hasKey(map.get("verificationCount") + map.get("phone").toString())){
|
|
|
// 验证发送时间
|
|
|
- if (redisTemplate.hasKey(map.get("keyPrefix") + mobile)){
|
|
|
+ if (redisTemplate.hasKey(map.get("keyPrefix") + map.get("phone").toString())){
|
|
|
return Response.error(500,"验证码还未过期,不能重新发送!");
|
|
|
}
|
|
|
|
|
|
// 验证发送次数
|
|
|
- Integer count = Integer.valueOf(redisTemplate.opsForValue().get(map.get("verificationCount") + mobile).toString());
|
|
|
+ Integer count = Integer.valueOf(redisTemplate.opsForValue().get(map.get("verificationCount") + map.get("phone").toString()).toString());
|
|
|
if(count >= MAX_COUNT) {
|
|
|
return Response.error(500,"当前手机号发送验证码次数过多!");
|
|
|
}
|
|
@@ -182,31 +162,31 @@ public class SmsServiceImpl implements ISmsService {
|
|
|
|
|
|
/**
|
|
|
* 写入缓存
|
|
|
- * @param mobile
|
|
|
+ * @param map
|
|
|
* @param code
|
|
|
*/
|
|
|
- private void writeCache(String mobile, String code,Map map){
|
|
|
+ private void writeCache(String code,Map map){
|
|
|
|
|
|
//该手机号首次获取验证码
|
|
|
- if(!redisTemplate.hasKey(map.get("keyPrefix") + mobile) && !redisTemplate.hasKey(map.get("verificationCount") + mobile)){
|
|
|
+ if(!redisTemplate.hasKey(map.get("keyPrefix") + map.get("phone").toString()) && !redisTemplate.hasKey(map.get("verificationCount") + map.get("phone").toString())){
|
|
|
synchronized(this){
|
|
|
//设置该手机号验证码 两分钟过期
|
|
|
- redisTemplate.opsForValue().set(map.get("keyPrefix") + mobile , code, SEND_INTERVAL, TimeUnit.MINUTES);
|
|
|
+ redisTemplate.opsForValue().set(map.get("keyPrefix") + map.get("phone").toString() , code, SEND_INTERVAL, TimeUnit.MINUTES);
|
|
|
//设置该手机号验证次数 一天过期
|
|
|
- redisTemplate.opsForValue().set(map.get("verificationCount") + mobile , "1",1,TimeUnit.DAYS);
|
|
|
+ redisTemplate.opsForValue().set(map.get("verificationCount") + map.get("phone").toString() , "1",1,TimeUnit.DAYS);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//多次获取验证码
|
|
|
- if (redisTemplate.hasKey(map.get("verificationCount") + mobile) && !redisTemplate.hasKey(map.get("keyPrefix") + mobile)){
|
|
|
+ if (redisTemplate.hasKey(map.get("verificationCount") + map.get("phone").toString()) && !redisTemplate.hasKey(map.get("keyPrefix") + map.get("phone").toString())){
|
|
|
//判断该手机验证次数
|
|
|
- Integer count = Integer.valueOf(redisTemplate.opsForValue().get(map.get("verificationCount") + mobile).toString());
|
|
|
+ Integer count = Integer.valueOf(redisTemplate.opsForValue().get(map.get("verificationCount") + map.get("phone").toString()).toString());
|
|
|
if (count < MAX_COUNT){
|
|
|
synchronized(this){
|
|
|
//该手机号验证次数+1 一天过期
|
|
|
- redisTemplate.opsForValue().set(map.get("verificationCount") + mobile,String.valueOf(count+1),1,TimeUnit.DAYS);
|
|
|
+ redisTemplate.opsForValue().set(map.get("verificationCount") + map.get("phone").toString(),String.valueOf(count+1),1,TimeUnit.DAYS);
|
|
|
//设置该手机号验证码 两分钟过期
|
|
|
- redisTemplate.opsForValue().set(map.get("keyPrefix") + mobile , code, SEND_INTERVAL, TimeUnit.MINUTES);
|
|
|
+ redisTemplate.opsForValue().set(map.get("keyPrefix") + map.get("phone").toString() , code, SEND_INTERVAL, TimeUnit.MINUTES);
|
|
|
}
|
|
|
}
|
|
|
}
|