短信发送日志调整
This commit is contained in:
@@ -27,6 +27,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
import springfox.documentation.annotations.ApiIgnore;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 类的介绍:
|
||||
@@ -60,8 +61,12 @@ public class AppUserController extends BaseController {
|
||||
})
|
||||
public HttpResult<String> authCode(String phone, String devCode, String type) {
|
||||
String methodDescribe = getMethodDescribe("authCode");
|
||||
appUserService.setMessage(phone,devCode,type);
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, "success", methodDescribe);
|
||||
String code = appUserService.setMessage(phone,devCode,type);
|
||||
if (Objects.equals(code,"OK")){
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, "success", methodDescribe);
|
||||
} else {
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, "fail", methodDescribe);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -11,7 +11,7 @@ public interface IAppUserService {
|
||||
* @param devCode 设备码
|
||||
* @param type 验证码类型
|
||||
*/
|
||||
void setMessage(String phone, String devCode, String type);
|
||||
String setMessage(String phone, String devCode, String type);
|
||||
|
||||
/**
|
||||
* 手机app注册(所有用户初始统一为游客,需要特定的码升级)
|
||||
|
||||
@@ -68,12 +68,14 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, User> impleme
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void setMessage(String phone, String devCode, String type) {
|
||||
public String setMessage(String phone, String devCode, String type) {
|
||||
if (!PubUtils.match(PatternRegex.PHONE_REGEX, phone)){
|
||||
throw new BusinessException(UserResponseEnum.REGISTER_PHONE_WRONG);
|
||||
}
|
||||
SendSmsResponse sendSmsResponse = null;
|
||||
String msgTemplate = null;
|
||||
String vcode = null;
|
||||
try {
|
||||
String msgTemplate;
|
||||
switch (type) {
|
||||
case "0":
|
||||
msgTemplate = MessageEnum.getTemplateByCode(0);
|
||||
@@ -148,11 +150,11 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, User> impleme
|
||||
request.setSignName("灿能云");
|
||||
//必填:短信模板-可在短信控制台中找到,发送国际/港澳台消息时,请使用国际/港澳台短信模版
|
||||
request.setTemplateCode(msgTemplate);
|
||||
String vcode = getMessageCode();
|
||||
vcode = getMessageCode();
|
||||
String code = "{\"code\":\"" + vcode + "\"}";
|
||||
request.setTemplateParam(code);
|
||||
//请求失败这里会抛ClientException异常
|
||||
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
|
||||
sendSmsResponse = acsClient.getAcsResponse(request);
|
||||
String key = RedisKeyEnum.SMS_LOGIN_KEY.getKey() + phone;
|
||||
if (sendSmsResponse.getCode() != null && "OK".equals(sendSmsResponse.getCode())) {
|
||||
//成功发送短信验证码后,保存进redis,验证码失效为5分钟
|
||||
@@ -160,20 +162,14 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, User> impleme
|
||||
} else {
|
||||
throw new BusinessException(UserResponseEnum.SEND_CODE_FAIL);
|
||||
}
|
||||
AppSendMsg appSendMsg = new AppSendMsg();
|
||||
appSendMsg.setPhone(phone);
|
||||
appSendMsg.setMessage(vcode);
|
||||
appSendMsg.setSendTime(LocalDateTime.now());
|
||||
appSendMsg.setSendStatus(sendSmsResponse.getCode() == null ? "无状态" : sendSmsResponse.getCode());
|
||||
appSendMsgService.save(appSendMsg);
|
||||
//短信入库
|
||||
addSendMessage(phone,vcode,msgTemplate,sendSmsResponse);
|
||||
} catch (Exception e) {
|
||||
logger.error("发送短信异常,异常为:"+e.getMessage());
|
||||
if (e.getMessage().length() < 10) {
|
||||
throw new BusinessException(e.getMessage());
|
||||
} else {
|
||||
throw new BusinessException(UserResponseEnum.SEND_CODE_FAIL);
|
||||
}
|
||||
//短信入库
|
||||
addSendMessage(phone,vcode,msgTemplate,sendSmsResponse);
|
||||
}
|
||||
return sendSmsResponse.getCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -338,23 +334,25 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, User> impleme
|
||||
* @param userId
|
||||
*/
|
||||
public void sendPasswordMessage(String phone, String userId) {
|
||||
String msgTemplate = MessageEnum.getTemplateByCode(3);
|
||||
//开始执行短信发送
|
||||
//设置超时时间-可自行调整
|
||||
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
|
||||
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
|
||||
//初始化ascClient需要的几个参数
|
||||
//短信API产品名称(短信产品名固定,无需修改)
|
||||
final String product = "Dysmsapi";
|
||||
//短信API产品域名(接口地址固定,无需修改)
|
||||
final String domain = "dysmsapi.aliyuncs.com";
|
||||
//替换成你的AK
|
||||
//你的accessKeyId,参考本文档步骤2
|
||||
final String accessKeyId = "LTAI4FxsR76x2dq3w9c5puUe";
|
||||
//你的accessKeySecret,参考本文档步骤2
|
||||
final String accessKeySecret = "GxkTR8fsrvHtixTlD9UPmOGli35tZs";
|
||||
//初始化ascClient,暂时不支持多region(请勿修改)
|
||||
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
|
||||
SendSmsResponse sendSmsResponse = null;
|
||||
String password = null;
|
||||
String msgTemplate = MessageEnum.getTemplateByCode(3);
|
||||
//开始执行短信发送
|
||||
//设置超时时间-可自行调整
|
||||
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
|
||||
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
|
||||
//初始化ascClient需要的几个参数
|
||||
//短信API产品名称(短信产品名固定,无需修改)
|
||||
final String product = "Dysmsapi";
|
||||
//短信API产品域名(接口地址固定,无需修改)
|
||||
final String domain = "dysmsapi.aliyuncs.com";
|
||||
//替换成你的AK
|
||||
//你的accessKeyId,参考本文档步骤2
|
||||
final String accessKeyId = "LTAI4FxsR76x2dq3w9c5puUe";
|
||||
//你的accessKeySecret,参考本文档步骤2
|
||||
final String accessKeySecret = "GxkTR8fsrvHtixTlD9UPmOGli35tZs";
|
||||
//初始化ascClient,暂时不支持多region(请勿修改)
|
||||
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
|
||||
try {
|
||||
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
|
||||
IAcsClient acsClient = new DefaultAcsClient(profile);
|
||||
@@ -365,30 +363,21 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, User> impleme
|
||||
request.setSignName("灿能云");
|
||||
//必填:短信模板-可在短信控制台中找到,发送国际/港澳台消息时,请使用国际/港澳台短信模版
|
||||
request.setTemplateCode(msgTemplate);
|
||||
String password = redisUtil.getStringByKey(userId);
|
||||
password = redisUtil.getStringByKey(userId);
|
||||
String code = "{\"pwd\":\"" + password + "\"}";
|
||||
request.setTemplateParam(code);
|
||||
//请求失败这里会抛ClientException异常
|
||||
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
|
||||
sendSmsResponse = acsClient.getAcsResponse(request);
|
||||
if (sendSmsResponse.getCode() != null && "OK".equals(sendSmsResponse.getCode())) {
|
||||
//成功发送短信验证码后,删除用户密码信息
|
||||
redisUtil.delete(userId);
|
||||
} else {
|
||||
throw new BusinessException(UserResponseEnum.SEND_CODE_FAIL);
|
||||
}
|
||||
AppSendMsg appSendMsg = new AppSendMsg();
|
||||
appSendMsg.setPhone(phone);
|
||||
appSendMsg.setMessage(password);
|
||||
appSendMsg.setSendTime(LocalDateTime.now());
|
||||
appSendMsg.setSendStatus(sendSmsResponse.getCode() == null ? "无状态" : sendSmsResponse.getCode());
|
||||
appSendMsgService.save(appSendMsg);
|
||||
addSendMessage(phone,password,msgTemplate,sendSmsResponse);
|
||||
} catch (ClientException e) {
|
||||
logger.error("发送短信异常,异常为:"+e.getMessage());
|
||||
if (e.getMessage().length() < 10) {
|
||||
throw new BusinessException(e.getMessage());
|
||||
} else {
|
||||
throw new BusinessException(UserResponseEnum.SEND_CODE_FAIL);
|
||||
}
|
||||
addSendMessage(phone,password,msgTemplate,sendSmsResponse);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -426,4 +415,18 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, User> impleme
|
||||
return user;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证码入库
|
||||
*/
|
||||
public void addSendMessage(String phone, String vcode, String template, SendSmsResponse sendSmsResponse) {
|
||||
AppSendMsg appSendMsg = new AppSendMsg();
|
||||
appSendMsg.setPhone(phone);
|
||||
appSendMsg.setMessage(vcode);
|
||||
appSendMsg.setSendTime(LocalDateTime.now());
|
||||
appSendMsg.setSendStatus(sendSmsResponse.getCode() == null ? "无状态" : sendSmsResponse.getCode());
|
||||
appSendMsg.setRemark(sendSmsResponse.getMessage());
|
||||
appSendMsg.setTemplate(template);
|
||||
appSendMsgService.save(appSendMsg);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user