Skip to content

Commit

Permalink
优化配置
Browse files Browse the repository at this point in the history
  • Loading branch information
shuaje committed May 13, 2018
1 parent 17de6d2 commit 6393156
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import top.ibase4j.core.config.WebMvcConfig;
import top.ibase4j.core.filter.TokenFilter;
import top.ibase4j.core.interceptor.EventInterceptor;
import top.ibase4j.core.interceptor.SignInterceptor;

/**
* @author ShenHuaJie
Expand All @@ -21,12 +23,20 @@ public FilterRegistrationBean<TokenFilter> tokenFilterRegistration() {
FilterRegistrationBean<TokenFilter> registration = new FilterRegistrationBean<TokenFilter>(new TokenFilter());
registration.setName("tokenFilter");
registration.addUrlPatterns("/*");
registration.setOrder(4);
registration.setOrder(5);
return registration;
}

@Override
@Bean
public EventInterceptor eventInterceptor() {
return new EventInterceptor();
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
super.addInterceptors(registry);
registry.addInterceptor(new SignInterceptor()).addPathPatterns("/**").excludePathPatterns("/*.ico",
"/*/api-docs", "/swagger**", "/swagger-resources/**", "/webjars/**", "/configuration/**");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

/**
* 发送短信服务
*
*
* @author ShenHuaJie
* @since 2017年3月16日 下午2:38:44
*/
Expand All @@ -52,29 +52,28 @@ public class SendMsgServiceImpl implements ISendMsgService {
@Autowired
private ISysMsgConfigService msgConfigService;

@Override
public void sendMsg(SendMsg sendMsg) {
Map<String, Object> params = InstanceUtil.newHashMap();
List<SysMsgConfig> configList = msgConfigService.queryList(params);
if (configList.isEmpty()) {
throw new RuntimeException("缺少短信平台配置.");
}
SysMsgConfig config = configList.get(0);
try {
Map<String, Object> params = InstanceUtil.newHashMap();
List<SysMsgConfig> configList = msgConfigService.queryList(params);
if (configList.isEmpty()) {
throw new RuntimeException("缺少短信平台配置.");
}
SysMsgConfig config = configList.get(0);

String type = "SMS_TYPE_" + sendMsg.getBizType();
String templateCode = paramService.getValue(type);
if (StringUtils.isBlank(templateCode)) {
throw new RuntimeException("不支持的短信类型:" + sendMsg.getBizType());
}
String sender = StringUtils.defaultIfBlank(sendMsg.getSender(), config.getSenderName());
String type = "SMS_TYPE_" + sendMsg.getBizType();
String templateCode = paramService.getValue(type);
if (StringUtils.isBlank(templateCode)) {
throw new RuntimeException("不支持的短信类型:" + sendMsg.getBizType());
}
String sender = StringUtils.defaultIfBlank(sendMsg.getSender(), config.getSenderName());

setParams(sender, sendMsg);
setParams(sendMsg);

// 设置超时时间-可自行调整
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
SendSmsResponse response;
SysMsg record = new SysMsg();
try {
// 设置超时时间-可自行调整
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
// 初始化ascClient,暂时不支持多region
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", config.getSmsPlatAccount(),
config.getSmsPlatPassword());
Expand All @@ -88,7 +87,7 @@ public void sendMsg(SendMsg sendMsg) {
// 必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
request.setPhoneNumbers(sendMsg.getPhone());
// 必填:短信签名-可在短信控制台中找到
request.setSignName(config.getSenderName());
request.setSignName(sender);
// 必填:短信模板-可在短信控制台中找到
request.setTemplateCode(templateCode);
// 可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
Expand All @@ -97,8 +96,9 @@ public void sendMsg(SendMsg sendMsg) {
// 可选-上行短信扩展码(扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段)
// request.setSmsUpExtendCode("90997");
// 请求失败这里会抛ClientException异常
response = acsClient.getAcsResponse(request);
SendSmsResponse response = acsClient.getAcsResponse(request);
logger.info(JSON.toJSONString(response));
SysMsg record = new SysMsg();
if (response.getCode() != null) {
record.setBizId(response.getRequestId());
if (response.getCode().equals("OK")) {
Expand All @@ -116,38 +116,36 @@ public void sendMsg(SendMsg sendMsg) {
record.setPhone(sendMsg.getPhone());
record.setContent(sendMsg.getParams());
msgService.update(record);

if ("0".equals(record.getSendState())) {
throw new RuntimeException(response.getCode() + response.getMessage());
}
} catch (Exception e) {
throw new RuntimeException(e);
}
if ("0".equals(record.getSendState())) {
throw new RuntimeException(response.getMessage());
}
}

@Override
public void sendTts(SendMsg sendMsg) {
Map<String, Object> params = InstanceUtil.newHashMap();
List<SysMsgConfig> configList = msgConfigService.queryList(params);
if (configList.isEmpty()) {
throw new RuntimeException("缺少短信平台配置.");
}
SysMsgConfig config = configList.get(0);
try {
Map<String, Object> params = InstanceUtil.newHashMap();
List<SysMsgConfig> configList = msgConfigService.queryList(params);
if (configList.isEmpty()) {
throw new RuntimeException("缺少短信平台配置.");
}
SysMsgConfig config = configList.get(0);

String type = "TTS_TYPE_" + sendMsg.getBizType();
String templateCode = paramService.getValue(type);
if (StringUtils.isBlank(templateCode)) {
throw new RuntimeException("不支持的短信类型:" + sendMsg.getBizType());
}
String sender = StringUtils.defaultIfBlank(sendMsg.getSender(), config.getSenderName());
String type = "TTS_TYPE_" + sendMsg.getBizType();
String templateCode = paramService.getValue(type);
if (StringUtils.isBlank(templateCode)) {
throw new RuntimeException("不支持的短信类型:" + sendMsg.getBizType());
}

setParams(sender, sendMsg);
setParams(sendMsg);

// 设置超时时间-可自行调整
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
// 请求失败这里会抛ClientException异常
SingleCallByTtsResponse response;
SysMsg record = new SysMsg();
try {
// 设置超时时间-可自行调整
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
// 初始化ascClient,暂时不支持多region
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", config.getSmsPlatAccount(),
config.getSmsPlatPassword());
Expand All @@ -164,8 +162,10 @@ public void sendTts(SendMsg sendMsg) {
request.setTtsCode(templateCode);
// 可选-当模板中存在变量时需要设置此值
request.setTtsParam(sendMsg.getParams());
response = acsClient.getAcsResponse(request);
// 请求失败这里会抛ClientException异常
SingleCallByTtsResponse response = acsClient.getAcsResponse(request);
logger.info(JSON.toJSONString(response));
SysMsg record = new SysMsg();
if (response.getCode() != null) {
record.setBizId(response.getRequestId());
if (response.getCode().equals("OK")) {
Expand All @@ -182,58 +182,57 @@ public void sendTts(SendMsg sendMsg) {
record.setPhone(sendMsg.getPhone());
record.setContent(sendMsg.getParams());
msgService.update(record);

if ("0".equals(record.getSendState())) {
throw new RuntimeException(response.getMessage());
}
} catch (Exception e) {
throw new RuntimeException(e);
}

if ("0".equals(record.getSendState())) {
throw new RuntimeException(response.getMessage());
}
}

/** 设置参数 */
private void setParams(String sender, SendMsg sendMsg) {
private void setParams(SendMsg sendMsg) {
String cacheKey1, cacheKey2;
switch (sendMsg.getBizType()) {
case "1":// 用户注册验证码
cacheKey2 = MSGCHKTYPE.REGISTER + sendMsg.getPhone();
sendRandomCode(sender, sendMsg, cacheKey2);
sendRandomCode(sendMsg, cacheKey2);
break;
case "2":// 登录确认验证码
cacheKey1 = MSGCHKTYPE.LOGIN + DateUtil.getDate() + "_" + sendMsg.getPhone();
cacheKey2 = MSGCHKTYPE.LOGIN + sendMsg.getPhone();
String times = StringUtils.defaultIfBlank(paramService.getValue("LOGIN_DAILY_TIMES"), "3");
String msg = StringUtils.defaultIfBlank(paramService.getValue("LOGIN_LIMIT_MSG"), "您今天登录的次数已达到最大限制。");
CacheUtil.refreshTimes(cacheKey1, 60 * 60 * 24, Integer.parseInt(times), msg);
sendRandomCode(sender, sendMsg, cacheKey2);
sendRandomCode(sendMsg, cacheKey2);
break;
case "3":// 修改密码验证码
cacheKey2 = MSGCHKTYPE.CHGPWD + sendMsg.getPhone();
sendRandomCode(sender, sendMsg, cacheKey2);
sendRandomCode(sendMsg, cacheKey2);
break;
case "4":// 身份验证验证码
cacheKey2 = MSGCHKTYPE.VLDID + sendMsg.getPhone();
sendRandomCode(sender, sendMsg, cacheKey2);
sendRandomCode(sendMsg, cacheKey2);
break;
case "5":// 信息变更验证码
cacheKey2 = MSGCHKTYPE.CHGINFO + sendMsg.getPhone();
sendRandomCode(sender, sendMsg, cacheKey2);
sendRandomCode(sendMsg, cacheKey2);
break;
case "6":// 活动确认验证码
cacheKey2 = MSGCHKTYPE.AVTCMF + sendMsg.getPhone();
sendRandomCode(sender, sendMsg, cacheKey2);
sendRandomCode(sendMsg, cacheKey2);
break;
default:
break;
}
}

/** 发送验证码 */
private void sendRandomCode(String sender, SendMsg sendMsg, String cacheKey) {
private void sendRandomCode(SendMsg sendMsg, String cacheKey) {
Integer random = RandomUtils.nextInt(123456, 999999);
Map<String, String> param = InstanceUtil.newHashMap();
param.put("code", random.toString());
param.put("product", sender);
if ("6".equals(sendMsg.getBizType())) {
param.put("", sendMsg.getParams());
}
Expand Down

0 comments on commit 6393156

Please sign in to comment.