|
@@ -25,7 +25,7 @@ import java.util.Date;
|
|
/**
|
|
/**
|
|
* @Description:
|
|
* @Description:
|
|
* 短信验证接口实现
|
|
* 短信验证接口实现
|
|
- * 阿里大于验证码发送
|
|
|
|
|
|
+ * 阿里短信验证码发送
|
|
* @author: gaodm
|
|
* @author: gaodm
|
|
* @time: 2018/9/4 16:25
|
|
* @time: 2018/9/4 16:25
|
|
*/
|
|
*/
|
|
@@ -51,9 +51,9 @@ public class SmsVerServiceImpl implements SmsVerService {
|
|
return redisForSms.getValueSerializer().deserialize(b);
|
|
return redisForSms.getValueSerializer().deserialize(b);
|
|
}
|
|
}
|
|
|
|
|
|
- private byte[] getUserSmsKey(Long userId) {
|
|
|
|
- String userSmsFormat = "user_sms_%d";
|
|
|
|
- return serializeKey(String.format(userSmsFormat, userId));
|
|
|
|
|
|
+ private byte[] getUserSmsKey(String mobile) {
|
|
|
|
+ String userSmsFormat = "user_sms_%s";
|
|
|
|
+ return serializeKey(String.format(userSmsFormat, mobile));
|
|
}
|
|
}
|
|
|
|
|
|
public String smsSend(String mobile,String smsTemplateCode){
|
|
public String smsSend(String mobile,String smsTemplateCode){
|
|
@@ -104,10 +104,6 @@ public class SmsVerServiceImpl implements SmsVerService {
|
|
throw new CommonException(ErrorCode.PARAM_IS_NULL,
|
|
throw new CommonException(ErrorCode.PARAM_IS_NULL,
|
|
"用户短信验证信息不能为空!");
|
|
"用户短信验证信息不能为空!");
|
|
}
|
|
}
|
|
- if (null == swsVerInfo.getUserId()){
|
|
|
|
- throw new CommonException(ErrorCode.PARAM_IS_NULL,
|
|
|
|
- "用户ID不能为空!");
|
|
|
|
- }
|
|
|
|
if (StringUtil.isBlank(swsVerInfo.getMobile())){
|
|
if (StringUtil.isBlank(swsVerInfo.getMobile())){
|
|
throw new CommonException(ErrorCode.PARAM_IS_NULL,
|
|
throw new CommonException(ErrorCode.PARAM_IS_NULL,
|
|
"用户电话不能为空!");
|
|
"用户电话不能为空!");
|
|
@@ -119,7 +115,7 @@ public class SmsVerServiceImpl implements SmsVerService {
|
|
swsVerInfo.setCreateTime(DateUtil.now());
|
|
swsVerInfo.setCreateTime(DateUtil.now());
|
|
final Date expireDate = DateUtil.addMinutes(DateUtil.now(), 3);
|
|
final Date expireDate = DateUtil.addMinutes(DateUtil.now(), 3);
|
|
swsVerInfo.setExpireTime(expireDate);
|
|
swsVerInfo.setExpireTime(expireDate);
|
|
- final byte[] redis_key = getUserSmsKey(swsVerInfo.getUserId());
|
|
|
|
|
|
+ final byte[] redis_key = getUserSmsKey(swsVerInfo.getMobile());
|
|
redisForSms.execute(new RedisCallback<Object>() {
|
|
redisForSms.execute(new RedisCallback<Object>() {
|
|
@Override
|
|
@Override
|
|
public Object doInRedis(RedisConnection connection) throws DataAccessException {
|
|
public Object doInRedis(RedisConnection connection) throws DataAccessException {
|
|
@@ -150,10 +146,6 @@ public class SmsVerServiceImpl implements SmsVerService {
|
|
throw new CommonException(ErrorCode.PARAM_IS_NULL,
|
|
throw new CommonException(ErrorCode.PARAM_IS_NULL,
|
|
"用户短信验证信息不能为空!");
|
|
"用户短信验证信息不能为空!");
|
|
}
|
|
}
|
|
- if (null == swsVerInfo.getUserId()){
|
|
|
|
- throw new CommonException(ErrorCode.PARAM_IS_NULL,
|
|
|
|
- "用户ID不能为空!");
|
|
|
|
- }
|
|
|
|
if (StringUtil.isBlank(swsVerInfo.getMobile())){
|
|
if (StringUtil.isBlank(swsVerInfo.getMobile())){
|
|
throw new CommonException(ErrorCode.PARAM_IS_NULL,
|
|
throw new CommonException(ErrorCode.PARAM_IS_NULL,
|
|
"用户电话不能为空!");
|
|
"用户电话不能为空!");
|
|
@@ -163,7 +155,7 @@ public class SmsVerServiceImpl implements SmsVerService {
|
|
"短信验证码不能为空!");
|
|
"短信验证码不能为空!");
|
|
}
|
|
}
|
|
//从redis中取出
|
|
//从redis中取出
|
|
- final byte[] redis_key = getUserSmsKey(swsVerInfo.getUserId());
|
|
|
|
|
|
+ final byte[] redis_key = getUserSmsKey(swsVerInfo.getMobile());
|
|
SwsVerInfo swsVerInfoRes = (SwsVerInfo) redisForSms.execute(new RedisCallback<SwsVerInfo>() {
|
|
SwsVerInfo swsVerInfoRes = (SwsVerInfo) redisForSms.execute(new RedisCallback<SwsVerInfo>() {
|
|
@Override
|
|
@Override
|
|
public SwsVerInfo doInRedis(RedisConnection connection) throws DataAccessException {
|
|
public SwsVerInfo doInRedis(RedisConnection connection) throws DataAccessException {
|
|
@@ -175,24 +167,24 @@ public class SmsVerServiceImpl implements SmsVerService {
|
|
}
|
|
}
|
|
});
|
|
});
|
|
if (swsVerInfoRes == null) {
|
|
if (swsVerInfoRes == null) {
|
|
- log.info("用户短信验证信息不存在!用户ID:{}", swsVerInfo.getUserId());
|
|
|
|
|
|
+ log.info("用户短信验证信息不存在!用户电话:{}", swsVerInfo.getMobile());
|
|
throw new CommonException(ErrorCode.NOT_EXISTS,
|
|
throw new CommonException(ErrorCode.NOT_EXISTS,
|
|
"用户短信验证信息不存在!");
|
|
"用户短信验证信息不存在!");
|
|
}
|
|
}
|
|
//判断是否是想要的对象
|
|
//判断是否是想要的对象
|
|
if (!swsVerInfoRes.getMobile().equals(swsVerInfo.getMobile())) {
|
|
if (!swsVerInfoRes.getMobile().equals(swsVerInfo.getMobile())) {
|
|
- log.info("用户电话不一致!用户ID:{},用户电话:{}", swsVerInfo.getUserId(), swsVerInfo.getMobile());
|
|
|
|
|
|
+ log.info("用户电话不一致! 用户电话:{}", swsVerInfo.getMobile());
|
|
throw new CommonException(ErrorCode.PARAM_IS_ERROR,
|
|
throw new CommonException(ErrorCode.PARAM_IS_ERROR,
|
|
"用户电话不一致!");
|
|
"用户电话不一致!");
|
|
}
|
|
}
|
|
if (!swsVerInfoRes.getCode().equals(swsVerInfo.getCode())) {
|
|
if (!swsVerInfoRes.getCode().equals(swsVerInfo.getCode())) {
|
|
- log.info("用户短信验证码不一致!用户ID:{},验证码:{}", swsVerInfo.getUserId(),swsVerInfo.getCode());
|
|
|
|
|
|
+ log.info("用户短信验证码不一致!用户电话:{},验证码:{}", swsVerInfo.getMobile(),swsVerInfo.getCode());
|
|
throw new CommonException(ErrorCode.PARAM_IS_ERROR,
|
|
throw new CommonException(ErrorCode.PARAM_IS_ERROR,
|
|
"用户短息验证码不一致!");
|
|
"用户短息验证码不一致!");
|
|
}
|
|
}
|
|
//判断是否过期
|
|
//判断是否过期
|
|
if (!DateUtil.after(swsVerInfoRes.getExpireTime(), DateUtil.now())) {
|
|
if (!DateUtil.after(swsVerInfoRes.getExpireTime(), DateUtil.now())) {
|
|
- log.info("短息验证码已过期,请重新获取!用户ID:{},过期时间:{}", swsVerInfo.getUserId(), swsVerInfoRes.getExpireTime());
|
|
|
|
|
|
+ log.info("短息验证码已过期,请重新获取!用户电话:{},过期时间:{}", swsVerInfo.getMobile(), swsVerInfoRes.getExpireTime());
|
|
throw new CommonException(ErrorCode.PARAM_IS_ERROR,
|
|
throw new CommonException(ErrorCode.PARAM_IS_ERROR,
|
|
"短息验证码已过期,请重新获取!");
|
|
"短息验证码已过期,请重新获取!");
|
|
}
|
|
}
|
|
@@ -201,15 +193,15 @@ public class SmsVerServiceImpl implements SmsVerService {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 获取用户短信验证码信息
|
|
* 获取用户短信验证码信息
|
|
- * @param userId
|
|
|
|
- * @return
|
|
|
|
|
|
+ * @param mobile 用户电话
|
|
|
|
+ * @return 用户短信验证码信息
|
|
*/
|
|
*/
|
|
@Override
|
|
@Override
|
|
- public SwsVerInfo getSmsVerification(Long userId){
|
|
|
|
|
|
+ public SwsVerInfo getSmsVerification(String mobile){
|
|
return (SwsVerInfo) redisForSms.execute(new RedisCallback<Object>() {
|
|
return (SwsVerInfo) redisForSms.execute(new RedisCallback<Object>() {
|
|
@Override
|
|
@Override
|
|
public Object doInRedis(RedisConnection connection) throws DataAccessException {
|
|
public Object doInRedis(RedisConnection connection) throws DataAccessException {
|
|
- byte[] redis_key = getUserSmsKey(userId);
|
|
|
|
|
|
+ byte[] redis_key = getUserSmsKey(mobile);
|
|
byte[] bytes = connection.get(redis_key);
|
|
byte[] bytes = connection.get(redis_key);
|
|
if (bytes == null) {
|
|
if (bytes == null) {
|
|
return null;
|
|
return null;
|
|
@@ -223,8 +215,8 @@ public class SmsVerServiceImpl implements SmsVerService {
|
|
* 删除用户短信验证码信息
|
|
* 删除用户短信验证码信息
|
|
*/
|
|
*/
|
|
@Override
|
|
@Override
|
|
- public Boolean deleteSmsVerification(Long userId){
|
|
|
|
- final byte[] redis_key = getUserSmsKey(userId);
|
|
|
|
|
|
+ public Boolean deleteSmsVerification(String mobile){
|
|
|
|
+ final byte[] redis_key = getUserSmsKey(mobile);
|
|
Long l = (Long) redisForSms.execute(new RedisCallback<Long>() {
|
|
Long l = (Long) redisForSms.execute(new RedisCallback<Long>() {
|
|
@Override
|
|
@Override
|
|
public Long doInRedis(RedisConnection connection) throws DataAccessException {
|
|
public Long doInRedis(RedisConnection connection) throws DataAccessException {
|