Commit f424fa55 authored by lifei's avatar lifei

增加设置阿里云短信模板接口

parent a1860434
...@@ -101,9 +101,14 @@ public class GroupMsgController { ...@@ -101,9 +101,14 @@ public class GroupMsgController {
} }
@PostMapping("/asyncAliyunSmsTemplate2redis") @PostMapping("/asyncAliyunSmsTemplate2redis")
public CommonResponse asyncAliyunSmsTemplate2redis() { public CommonResponse asyncAliyunSmsTemplate2redis(@RequestParam("template_code_str") String templateCodeStr) {
groupMsgService.asyncAliyunSmsTemplate2redis(); groupMsgService.asyncAliyunSmsTemplate2redis(templateCodeStr);
return CommonResponse.DEFAULT_SUCCESS; return CommonResponse.DEFAULT_SUCCESS;
} }
@GetMapping("/getTemplateCodeListStr")
public String getTemplateCodeListStr() {
return groupMsgService.getTemplateCodeListStr();
}
} }
...@@ -18,6 +18,11 @@ public class SmsTemplateCache { ...@@ -18,6 +18,11 @@ public class SmsTemplateCache {
private final String SMS_TEMPLATE_PREFIX = "mx:AliYunSmsTemplate:"; private final String SMS_TEMPLATE_PREFIX = "mx:AliYunSmsTemplate:";
public void setTemplateCodes(String templateCodeStr) {
CacheUtil.val().set(smsTemplateTemplate, TEMPLATE_CODE_KEY, templateCodeStr);
}
public String getTemplateCodes() { public String getTemplateCodes() {
return CacheUtil.val().get(smsTemplateTemplate, TEMPLATE_CODE_KEY); return CacheUtil.val().get(smsTemplateTemplate, TEMPLATE_CODE_KEY);
} }
......
...@@ -26,5 +26,6 @@ public interface GroupMsgService { ...@@ -26,5 +26,6 @@ public interface GroupMsgService {
PageBean<JSONObject> listAliYunSMSTemplate(Integer offset, Integer size); PageBean<JSONObject> listAliYunSMSTemplate(Integer offset, Integer size);
void asyncAliyunSmsTemplate2redis(); void asyncAliyunSmsTemplate2redis(String templateCodeStr);
String getTemplateCodeListStr();
} }
...@@ -246,12 +246,18 @@ public class GroupMsgServiceImpl implements GroupMsgService { ...@@ -246,12 +246,18 @@ public class GroupMsgServiceImpl implements GroupMsgService {
String content = smsTemplateCache.getSmsTemplate(code); String content = smsTemplateCache.getSmsTemplate(code);
returnList.add(JSON.parseObject(content)); returnList.add(JSON.parseObject(content));
} }
return new PageBean<>(totalPage, totalSize, currentPage, returnList); PageBean<JSONObject> pageBean = new PageBean<>(totalPage, totalSize, currentPage, returnList);
return pageBean;
} }
@Override @Override
public void asyncAliyunSmsTemplate2redis() { public String getTemplateCodeListStr() {
queryAliyunSmsTemplateList(); return smsTemplateCache.getTemplateCodes();
}
@Override
public void asyncAliyunSmsTemplate2redis(String templateCodeStr) {
queryAliyunSmsTemplateList(templateCodeStr);
} }
private GroupMsgHistoryItemVo getHistoryVo(GroupMsgHistory history, boolean needOneDetails) { private GroupMsgHistoryItemVo getHistoryVo(GroupMsgHistory history, boolean needOneDetails) {
...@@ -287,14 +293,14 @@ public class GroupMsgServiceImpl implements GroupMsgService { ...@@ -287,14 +293,14 @@ public class GroupMsgServiceImpl implements GroupMsgService {
} }
/** /**
* 查询阿里云的短信模板,并存入本地Redis中 * 查询阿里云的短信模板,并存入本地Redis中
*/ */
private void queryAliyunSmsTemplateList() { private void queryAliyunSmsTemplateList(String templateCodeStr) {
String templateCodes = smsTemplateCache.getTemplateCodes(); // step1: 充值Redis里mx:AliYunSmsTemplateCode 值
String[] split = templateCodes.split(","); smsTemplateCache.setTemplateCodes(templateCodeStr);
// 参照阿里云sdk demo例子
// step2: 去调阿里API查询具体短信模板的详细内容
IAcsClient client = AliyunSdkAcsClientUtils.getDefaultAcsClient(); IAcsClient client = AliyunSdkAcsClientUtils.getDefaultAcsClient();
CommonRequest request = new CommonRequest(); CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST); request.setSysMethod(MethodType.POST);
...@@ -303,18 +309,17 @@ public class GroupMsgServiceImpl implements GroupMsgService { ...@@ -303,18 +309,17 @@ public class GroupMsgServiceImpl implements GroupMsgService {
request.setSysVersion("2017-05-25"); request.setSysVersion("2017-05-25");
request.setSysAction("QuerySmsTemplate"); request.setSysAction("QuerySmsTemplate");
request.putQueryParameter("RegionId", "cn-hangzhou"); request.putQueryParameter("RegionId", "cn-hangzhou");
String[] templateCodes = templateCodeStr.split(",");
// 通过Code 去查阿里云的模板内容 for (String templateCode : templateCodes) {
for (String templateCode : split) {
try { try {
request.putQueryParameter("TemplateCode", templateCode); request.putQueryParameter("TemplateCode", templateCode);
CommonResponse response = client.getCommonResponse(request); CommonResponse response = client.getCommonResponse(request);
// add to redis // add to redis
smsTemplateCache.setSmsTemplate(templateCode, response.getData()); smsTemplateCache.setSmsTemplate(templateCode, response.getData());
} catch (ServerException e) { } catch (ServerException e) {
log.error("listAliYunSMSTemplate method ServerException :", e); log.error("queryAliyunSmsTemplateList method ServerException :", e);
} catch (ClientException e) { } catch (ClientException e) {
log.error("listAliYunSMSTemplate method ClientException e", e); log.error("queryAliyunSmsTemplateList method ClientException e", e);
} }
} }
} }
......
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