Commit f424fa55 authored by lifei's avatar lifei 🏀

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

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