Commit 32b9aa4e by 段启岩

验证码,redis异常层级重构

parent 77ae64b1
package cn.meteor.beyondclouds.common.enums;
import cn.meteor.beyondclouds.core.IErrorCode;
/**
* Redis操作相关错误
* @author meteor
*/
public enum RedisErrorCode implements IErrorCode {
SET_FAILURE(10001, "存储失败");
RedisErrorCode(long code, String msg) {
this.code = code;
this.msg = msg;
}
private long code;
private String msg;
@Override
public long code() {
return code;
}
@Override
public String msg() {
return msg;
}
}
......@@ -6,12 +6,15 @@ import cn.meteor.beyondclouds.core.exception.ServiceException;
/**
* @author meteor
*/
public class RedisOperationException extends ServiceException {
public RedisOperationException(long errorCode, String errorMsg) {
super(errorCode, errorMsg);
public class RedisOperationException extends Exception {
public RedisOperationException() {
}
public RedisOperationException(IErrorCode errorCode) {
super(errorCode);
public RedisOperationException(String message) {
super(message);
}
public RedisOperationException(String message, Throwable cause) {
super(message, cause);
}
}
......@@ -7,12 +7,16 @@ import cn.meteor.beyondclouds.core.exception.ServiceException;
* 短信相关异常
* @author meteor
*/
public class SmsException extends ServiceException {
public SmsException(long errorCode, String errorMsg) {
super(errorCode, errorMsg);
public class SmsException extends Exception {
public SmsException() {
}
public SmsException(IErrorCode errorCode) {
super(errorCode);
public SmsException(String message) {
super(message);
}
public SmsException(String message, Throwable cause) {
super(message, cause);
}
}
package cn.meteor.beyondclouds.common.helper;
package cn.meteor.beyondclouds.common.helper.impl;
import cn.meteor.beyondclouds.common.enums.RedisErrorCode;
import cn.meteor.beyondclouds.common.exception.RedisOperationException;
import cn.meteor.beyondclouds.common.helper.IRedisHelper;
import cn.meteor.beyondclouds.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
......@@ -75,7 +74,7 @@ public class RedisHelperImpl implements IRedisHelper {
}
} catch (IOException e) {
e.printStackTrace();
throw new RedisOperationException(RedisErrorCode.SET_FAILURE);
throw new RedisOperationException("数据存储失败");
}
}
......@@ -89,7 +88,7 @@ public class RedisHelperImpl implements IRedisHelper {
}
} catch (IOException e) {
e.printStackTrace();
throw new RedisOperationException(RedisErrorCode.SET_FAILURE);
throw new RedisOperationException();
}
}
......
package cn.meteor.beyondclouds.common.helper;
package cn.meteor.beyondclouds.common.helper.impl;
import cn.meteor.beyondclouds.common.enums.SmsErrorCode;
import cn.meteor.beyondclouds.common.exception.SmsException;
import cn.meteor.beyondclouds.common.helper.ISmsHelper;
import cn.meteor.beyondclouds.util.JsonUtils;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Map;
/**
......@@ -47,16 +44,10 @@ public class SmsHelperImpl implements ISmsHelper {
CommonResponse response = client.getCommonResponse(request);
Map data = JsonUtils.toBean(response.getData(), Map.class);
if (!data.get(SMS_RESPONSE_DATA_KEY).equals(SMS_RESPONSE_DATA_VALUE_OK)) {
throw new SmsException(SmsErrorCode.SEND_FAILURE);
throw new SmsException("短信发送失败");
}
} catch (ServerException e) {
e.printStackTrace();
throw new SmsException(SmsErrorCode.SERVER_ERROR.code(), e.getErrMsg());
} catch (ClientException e) {
e.printStackTrace();
throw new SmsException(SmsErrorCode.CLIENT_ERROR.code(), e.getErrMsg());
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
throw new SmsException(e.getMessage(), e);
}
}
......
......@@ -3,6 +3,7 @@ package cn.meteor.beyondclouds.modules.sms;
import cn.meteor.beyondclouds.common.exception.SmsException;
import cn.meteor.beyondclouds.core.api.Response;
import cn.meteor.beyondclouds.core.exception.ServiceException;
import cn.meteor.beyondclouds.modules.sms.exception.SmsServiceException;
import cn.meteor.beyondclouds.modules.sms.service.ISmsService;
import cn.meteor.beyondclouds.util.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -32,7 +33,7 @@ public class SmsApi {
try {
smsService.sendVerifyCode(mobile, VerifyCodeUtils.randomVerifyCode());
return Response.success();
} catch (ServiceException e) {
} catch (SmsServiceException e) {
e.printStackTrace();
return Response.error(e);
}
......
package cn.meteor.beyondclouds.common.enums;
package cn.meteor.beyondclouds.modules.sms.enums;
import cn.meteor.beyondclouds.core.IErrorCode;
......
package cn.meteor.beyondclouds.modules.sms.exception;
import cn.meteor.beyondclouds.core.IErrorCode;
import cn.meteor.beyondclouds.core.exception.ServiceException;
/**
* 短信业务异常
* @author meteor
*/
public class SmsServiceException extends ServiceException {
public SmsServiceException(long errorCode, String errorMsg) {
super(errorCode, errorMsg);
}
public SmsServiceException(IErrorCode errorCode) {
super(errorCode);
}
}
......@@ -2,6 +2,7 @@ package cn.meteor.beyondclouds.modules.sms.service;
import cn.meteor.beyondclouds.common.exception.RedisOperationException;
import cn.meteor.beyondclouds.common.exception.SmsException;
import cn.meteor.beyondclouds.modules.sms.exception.SmsServiceException;
/**
* 短信业务类
......@@ -14,5 +15,5 @@ public interface ISmsService {
* @param mobile
* @param randomVerifyCode
*/
void sendVerifyCode(String mobile, String randomVerifyCode) throws SmsException, RedisOperationException;
void sendVerifyCode(String mobile, String randomVerifyCode) throws SmsServiceException;
}
package cn.meteor.beyondclouds.modules.sms.service.impl;
import cn.meteor.beyondclouds.common.exception.RedisOperationException;
import cn.meteor.beyondclouds.common.exception.SmsException;
import cn.meteor.beyondclouds.modules.sms.enums.SmsErrorCode;
import cn.meteor.beyondclouds.common.helper.IRedisHelper;
import cn.meteor.beyondclouds.common.helper.ISmsHelper;
import cn.meteor.beyondclouds.core.redis.RedisKey;
import cn.meteor.beyondclouds.modules.sms.exception.SmsServiceException;
import cn.meteor.beyondclouds.modules.sms.service.ISmsService;
import cn.meteor.beyondclouds.modules.user.enums.UserErrorCode;
import cn.meteor.beyondclouds.modules.user.exception.UserServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -28,11 +26,15 @@ public class SmsServiceImpl implements ISmsService {
}
@Override
public void sendVerifyCode(String mobile, String randomVerifyCode) throws SmsException, RedisOperationException {
//1. 发送短信
smsHelper.sendVerifyCode(mobile, randomVerifyCode);
public void sendVerifyCode(String mobile, String randomVerifyCode) throws SmsServiceException {
try {
//1. 发送短信
smsHelper.sendVerifyCode(mobile, randomVerifyCode);
//2.存储短信到redis
redisHelper.set(RedisKey.VERIFY_CODE_KEY(mobile), randomVerifyCode, 5 * 60);
//2.存储短信到redis
redisHelper.set(RedisKey.VERIFY_CODE_KEY(mobile), randomVerifyCode, 5 * 60);
} catch (Exception e) {
throw new SmsServiceException(SmsErrorCode.SEND_FAILURE);
}
}
}
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