|
@@ -0,0 +1,224 @@
|
|
|
+package com.diagbot.service.impl;
|
|
|
+
|
|
|
+import com.diagbot.entity.SwsVerInfo;
|
|
|
+import com.diagbot.exception.CommonException;
|
|
|
+import com.diagbot.exception.ErrorCode;
|
|
|
+import com.diagbot.service.SmsVerService;
|
|
|
+import com.diagbot.util.DateUtil;
|
|
|
+import com.diagbot.util.GsonUtil;
|
|
|
+import com.diagbot.util.SmsCodeUtil;
|
|
|
+import com.diagbot.util.StringUtil;
|
|
|
+import com.taobao.api.TaobaoClient;
|
|
|
+import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
|
|
|
+import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
+import org.springframework.dao.DataAccessException;
|
|
|
+import org.springframework.data.redis.connection.RedisConnection;
|
|
|
+import org.springframework.data.redis.core.RedisCallback;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description:
|
|
|
+ * 短信验证接口实现
|
|
|
+ * 阿里大于验证码发送
|
|
|
+ * @author: gaodm
|
|
|
+ * @time: 2018/9/4 16:25
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class SmsVerServiceImpl implements SmsVerService {
|
|
|
+ @Autowired
|
|
|
+ @Qualifier("redisTemplateForSms")
|
|
|
+ private RedisTemplate redisForSms;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TaobaoClient taobaoClient;
|
|
|
+
|
|
|
+ private byte[] serializeKey(Object o) {
|
|
|
+ return redisForSms.getKeySerializer().serialize(o);
|
|
|
+ }
|
|
|
+
|
|
|
+ private byte[] serializeValue(Object o) {
|
|
|
+ return redisForSms.getValueSerializer().serialize(o);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Object deserializeValue(byte[] b) {
|
|
|
+ return redisForSms.getValueSerializer().deserialize(b);
|
|
|
+ }
|
|
|
+
|
|
|
+ private byte[] getUserSmsKey(int userId) {
|
|
|
+ String userSmsFormat = "user_sms_%d";
|
|
|
+ return serializeKey(String.format(userSmsFormat, userId));
|
|
|
+ }
|
|
|
+
|
|
|
+ public String smsSend(String mobile,String smsTemplateCode){
|
|
|
+ if (StringUtil.isBlank(mobile)){
|
|
|
+ throw new CommonException(ErrorCode.PARAM_IS_NULL,
|
|
|
+ "电话号码不能为空!");
|
|
|
+ }
|
|
|
+ AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
|
|
|
+ req.setSmsType( "normal" );
|
|
|
+ req.setSmsFreeSignName( "朗通云平台" );
|
|
|
+ String code = SmsCodeUtil.getVerCode();
|
|
|
+ String json="{\"code\":\""
|
|
|
+ + code
|
|
|
+ + "\",\"product\":\"【朗通云平台】\"}";
|
|
|
+ req.setSmsParamString(json);
|
|
|
+ req.setRecNum(mobile);
|
|
|
+ req.setSmsTemplateCode(smsTemplateCode);
|
|
|
+ try {
|
|
|
+ AlibabaAliqinFcSmsNumSendResponse rsp = taobaoClient.execute(req);
|
|
|
+ System.out.println(GsonUtil.toJson(rsp));
|
|
|
+ if (null == rsp || !rsp.isSuccess()){
|
|
|
+ throw new CommonException(ErrorCode.SMS_SEND_ERROR);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new CommonException(ErrorCode.SMS_SEND_ERROR);
|
|
|
+ }
|
|
|
+ return code;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建用户短信验证信息
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Boolean createSmsVerification(SwsVerInfo swsVerInfo){
|
|
|
+ if (null == swsVerInfo){
|
|
|
+ throw new CommonException(ErrorCode.PARAM_IS_NULL,
|
|
|
+ "用户短信验证信息不能为空!");
|
|
|
+ }
|
|
|
+ if (null == swsVerInfo.getUserId()){
|
|
|
+ throw new CommonException(ErrorCode.PARAM_IS_NULL,
|
|
|
+ "用户ID不能为空!");
|
|
|
+ }
|
|
|
+ if (StringUtil.isBlank(swsVerInfo.getMobile())){
|
|
|
+ throw new CommonException(ErrorCode.PARAM_IS_NULL,
|
|
|
+ "用户电话不能为空!");
|
|
|
+ }
|
|
|
+ if (StringUtil.isBlank(swsVerInfo.getCode())){
|
|
|
+ throw new CommonException(ErrorCode.PARAM_IS_NULL,
|
|
|
+ "验证码不能为空!");
|
|
|
+ }
|
|
|
+ swsVerInfo.setCreateTime(DateUtil.now());
|
|
|
+ final Date expireDate = DateUtil.addMinutes(DateUtil.now(), 20);
|
|
|
+ swsVerInfo.setExpireTime(expireDate);
|
|
|
+ final byte[] redis_key = getUserSmsKey(swsVerInfo.getUserId());
|
|
|
+ redisForSms.execute(new RedisCallback<Object>() {
|
|
|
+ @Override
|
|
|
+ public Object doInRedis(RedisConnection connection) throws DataAccessException {
|
|
|
+ //获取旧的
|
|
|
+ byte[] bytes = connection.get(redis_key);
|
|
|
+ //删除旧的
|
|
|
+ if (bytes != null) {
|
|
|
+ connection.del(bytes);
|
|
|
+ }
|
|
|
+ //设置新的
|
|
|
+ connection.setEx(
|
|
|
+ redis_key,
|
|
|
+ (expireDate.getTime() - DateUtil.now().getTime()) / 1000,
|
|
|
+ serializeValue(swsVerInfo)
|
|
|
+ );
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证短信验证码是否有效
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Boolean verifySmsVerification(SwsVerInfo swsVerInfo){
|
|
|
+ if (null == swsVerInfo){
|
|
|
+ throw new CommonException(ErrorCode.PARAM_IS_NULL,
|
|
|
+ "用户短信验证信息不能为空!");
|
|
|
+ }
|
|
|
+ if (null == swsVerInfo.getUserId()){
|
|
|
+ throw new CommonException(ErrorCode.PARAM_IS_NULL,
|
|
|
+ "用户ID不能为空!");
|
|
|
+ }
|
|
|
+ if (StringUtil.isBlank(swsVerInfo.getMobile())){
|
|
|
+ throw new CommonException(ErrorCode.PARAM_IS_NULL,
|
|
|
+ "用户电话不能为空!");
|
|
|
+ }
|
|
|
+ if (StringUtil.isBlank(swsVerInfo.getCode())){
|
|
|
+ throw new CommonException(ErrorCode.PARAM_IS_NULL,
|
|
|
+ "短信验证码不能为空!");
|
|
|
+ }
|
|
|
+ //从redis中取出
|
|
|
+ final byte[] redis_key = getUserSmsKey(swsVerInfo.getUserId());
|
|
|
+ SwsVerInfo swsVerInfoRes = (SwsVerInfo) redisForSms.execute(new RedisCallback<SwsVerInfo>() {
|
|
|
+ @Override
|
|
|
+ public SwsVerInfo doInRedis(RedisConnection connection) throws DataAccessException {
|
|
|
+ byte[] bytes = connection.get(redis_key);
|
|
|
+ if (bytes == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return (SwsVerInfo) deserializeValue(bytes);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ if (swsVerInfoRes == null) {
|
|
|
+ log.info("用户短信验证信息不存在!用户ID:{}", swsVerInfo.getUserId());
|
|
|
+ throw new CommonException(ErrorCode.NOT_EXISTS,
|
|
|
+ "用户短信验证信息不存在!");
|
|
|
+ }
|
|
|
+ //判断是否是想要的对象
|
|
|
+ if (!swsVerInfoRes.getMobile().equals(swsVerInfo.getMobile())) {
|
|
|
+ log.info("用户电话不一致!用户ID:{},用户电话:{}", swsVerInfo.getUserId(), swsVerInfo.getMobile());
|
|
|
+ throw new CommonException(ErrorCode.PARAM_IS_ERROR,
|
|
|
+ "用户电话不一致!");
|
|
|
+ }
|
|
|
+ if (!swsVerInfoRes.getCode().equals(swsVerInfo.getCode())) {
|
|
|
+ log.info("用户短信验证码不一致!用户ID:{},验证码:{}", swsVerInfo.getUserId(),swsVerInfo.getCode());
|
|
|
+ throw new CommonException(ErrorCode.PARAM_IS_ERROR,
|
|
|
+ "用户短息验证码不一致!");
|
|
|
+ }
|
|
|
+ //判断是否过期
|
|
|
+ if (!DateUtil.after(swsVerInfoRes.getExpireTime(), DateUtil.now())) {
|
|
|
+ log.info("短息验证码已过期,请重新获取!用户ID:{},过期时间:{}", swsVerInfo.getUserId(), swsVerInfoRes.getExpireTime());
|
|
|
+ throw new CommonException(ErrorCode.PARAM_IS_ERROR,
|
|
|
+ "短息验证码已过期,请重新获取!");
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户短信验证码信息
|
|
|
+ * @param userId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public SwsVerInfo getSmsVerification(Integer userId){
|
|
|
+ return (SwsVerInfo) redisForSms.execute(new RedisCallback<Object>() {
|
|
|
+ @Override
|
|
|
+ public Object doInRedis(RedisConnection connection) throws DataAccessException {
|
|
|
+ byte[] redis_key = getUserSmsKey(userId);
|
|
|
+ byte[] bytes = connection.get(redis_key);
|
|
|
+ if (bytes == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return deserializeValue(bytes);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除用户短信验证码信息
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Boolean deleteSmsVerification(Integer userId){
|
|
|
+ final byte[] redis_key = getUserSmsKey(userId);
|
|
|
+ Long l = (Long) redisForSms.execute(new RedisCallback<Long>() {
|
|
|
+ @Override
|
|
|
+ public Long doInRedis(RedisConnection connection) throws DataAccessException {
|
|
|
+ return connection.del(redis_key);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return l > 0;
|
|
|
+ }
|
|
|
+}
|