提交 0c198e8f authored 作者: cherishsince's avatar cherishsince

- 优化:增加注释,优化格式

上级 20e65bf2
...@@ -80,7 +80,6 @@ public class AliYunSmsClient implements SmsClient { ...@@ -80,7 +80,6 @@ public class AliYunSmsClient implements SmsClient {
request.putQueryParameter("SignName", sign); request.putQueryParameter("SignName", sign);
request.putQueryParameter("TemplateCode", templateCode); request.putQueryParameter("TemplateCode", templateCode);
request.putQueryParameter("TemplateParam", JSON.toJSONString(templateParams)); request.putQueryParameter("TemplateParam", JSON.toJSONString(templateParams));
// 发送请求 // 发送请求
return doSend(request); return doSend(request);
} }
...@@ -88,19 +87,17 @@ public class AliYunSmsClient implements SmsClient { ...@@ -88,19 +87,17 @@ public class AliYunSmsClient implements SmsClient {
@Override @Override
public SendResult batchSend(List<String> mobileList, String sign, String templateCode, public SendResult batchSend(List<String> mobileList, String sign, String templateCode,
String template, Map<String, String> templateParams) { String template, Map<String, String> templateParams) {
// 最大发送数为 1000,我们设置为 500 个, 分段发送 // 最大发送数为 1000,我们设置为 500 个, 分段发送
int maxSendSize = MAX_BATCH_SIZE; int maxSendSize = MAX_BATCH_SIZE;
int maxSendSizeCount = mobileList.size() % maxSendSize == 0 int maxSendSizeCount = mobileList.size() % maxSendSize == 0
? mobileList.size() / maxSendSize ? mobileList.size() / maxSendSize
: mobileList.size() / maxSendSize + 1; : mobileList.size() / maxSendSize + 1;
// 处理批量
SendResult sendResult = null; SendResult sendResult = null;
for (int i = 0; i < maxSendSizeCount; i++) { for (int i = 0; i < maxSendSizeCount; i++) {
// 分批发送 // 分批发送
List<String> batchSendMobile = mobileList List<String> batchSendMobile = mobileList
.subList(i * maxSendSize, (i + 1) * maxSendSize); .subList(i * maxSendSize, (i + 1) * maxSendSize);
// params // params
CommonRequest request = new CommonRequest(); CommonRequest request = new CommonRequest();
request.setMethod(MethodType.POST); request.setMethod(MethodType.POST);
...@@ -111,7 +108,6 @@ public class AliYunSmsClient implements SmsClient { ...@@ -111,7 +108,6 @@ public class AliYunSmsClient implements SmsClient {
request.putQueryParameter("SignNameJson", JSON.toJSONString(Collections.singletonList(sign))); request.putQueryParameter("SignNameJson", JSON.toJSONString(Collections.singletonList(sign)));
request.putQueryParameter("TemplateCode", templateCode); request.putQueryParameter("TemplateCode", templateCode);
request.putQueryParameter("TemplateParamJson", JSON.toJSONString(Collections.singletonList(templateParams))); request.putQueryParameter("TemplateParamJson", JSON.toJSONString(Collections.singletonList(templateParams)));
// 发送请求 // 发送请求
sendResult = doSend(request); sendResult = doSend(request);
} }
...@@ -125,9 +121,7 @@ public class AliYunSmsClient implements SmsClient { ...@@ -125,9 +121,7 @@ public class AliYunSmsClient implements SmsClient {
CommonResponse response = client.getCommonResponse(request); CommonResponse response = client.getCommonResponse(request);
Result result = JSON.parseObject(response.getData(), Result.class); Result result = JSON.parseObject(response.getData(), Result.class);
if (!SUCCESS_CODE.equals(result.getCode())) { if (!SUCCESS_CODE.equals(result.getCode())) {
LOGGER.info("发送验证码失败 params {} res {}", JSON.toJSON(request), JSON.toJSON(result)); LOGGER.info("发送验证码失败 params {} res {}", JSON.toJSON(request), JSON.toJSON(result));
// 错误发送失败 // 错误发送失败
return new SendResult() return new SendResult()
.setIsSuccess(false) .setIsSuccess(false)
...@@ -135,7 +129,6 @@ public class AliYunSmsClient implements SmsClient { ...@@ -135,7 +129,6 @@ public class AliYunSmsClient implements SmsClient {
.setMessage(result.getMessage()); .setMessage(result.getMessage());
} else { } else {
LOGGER.info("发送验证码失败 params {} res", JSON.toJSON(request), JSON.toJSON(result)); LOGGER.info("发送验证码失败 params {} res", JSON.toJSON(request), JSON.toJSON(result));
// 发送成功 // 发送成功
return new SendResult() return new SendResult()
.setIsSuccess(true) .setIsSuccess(true)
......
...@@ -88,7 +88,6 @@ public class YunPianSmsClient implements SmsClient { ...@@ -88,7 +88,6 @@ public class YunPianSmsClient implements SmsClient {
*/ */
private static final String URL_SEND_BATCH = "https://sms.yunpian.com/v2/sms/batch_send.json"; private static final String URL_SEND_BATCH = "https://sms.yunpian.com/v2/sms/batch_send.json";
//编码格式。发送编码格式统一用UTF-8 //编码格式。发送编码格式统一用UTF-8
private static String ENCODING = "UTF-8"; private static String ENCODING = "UTF-8";
...@@ -99,7 +98,6 @@ public class YunPianSmsClient implements SmsClient { ...@@ -99,7 +98,6 @@ public class YunPianSmsClient implements SmsClient {
public SendResult singleSend(String mobile, String sign, String templateCode, String template, Map<String, String> templateParams) { public SendResult singleSend(String mobile, String sign, String templateCode, String template, Map<String, String> templateParams) {
// build 模板 // build 模板
template = buildTemplate(sign, template, templateParams); template = buildTemplate(sign, template, templateParams);
// 请求参数 // 请求参数
Map<String, String> params = new LinkedHashMap<>(); Map<String, String> params = new LinkedHashMap<>();
params.put("apikey", apiKey); params.put("apikey", apiKey);
...@@ -114,7 +112,7 @@ public class YunPianSmsClient implements SmsClient { ...@@ -114,7 +112,7 @@ public class YunPianSmsClient implements SmsClient {
throw new ServiceException(SystemErrorCodeEnum.SMS_PLATFORM_FAIL.getCode(), throw new ServiceException(SystemErrorCodeEnum.SMS_PLATFORM_FAIL.getCode(),
jsonObject.getString("detail")); jsonObject.getString("detail"));
} }
// 转换 result
return new SendResult() return new SendResult()
.setIsSuccess(SUCCESS_CODE == jsonObject.getInteger("code")) .setIsSuccess(SUCCESS_CODE == jsonObject.getInteger("code"))
.setCode(jsonObject.getInteger("code")) .setCode(jsonObject.getInteger("code"))
...@@ -127,7 +125,6 @@ public class YunPianSmsClient implements SmsClient { ...@@ -127,7 +125,6 @@ public class YunPianSmsClient implements SmsClient {
Map<String, String> templateParams) { Map<String, String> templateParams) {
// build 模板 // build 模板
template = buildTemplate(sign, template, templateParams); template = buildTemplate(sign, template, templateParams);
// 最大发送数为 1000,我们设置为 500 个, 分段发送 // 最大发送数为 1000,我们设置为 500 个, 分段发送
int maxSendSize = MAX_BATCH_SIZE; int maxSendSize = MAX_BATCH_SIZE;
int maxSendSizeCount = mobileList.size() % maxSendSize == 0 int maxSendSizeCount = mobileList.size() % maxSendSize == 0
...@@ -142,9 +139,7 @@ public class YunPianSmsClient implements SmsClient { ...@@ -142,9 +139,7 @@ public class YunPianSmsClient implements SmsClient {
sendMobileStr.append(","); sendMobileStr.append(",");
sendMobileStr.append(mobileList.get(k)); sendMobileStr.append(mobileList.get(k));
} }
String dividedMobile = sendMobileStr.toString().substring(1); String dividedMobile = sendMobileStr.toString().substring(1);
// 发送手机号 // 发送手机号
Map<String, String> params = new LinkedHashMap<>(); Map<String, String> params = new LinkedHashMap<>();
params.put("apikey", apiKey); params.put("apikey", apiKey);
...@@ -159,12 +154,10 @@ public class YunPianSmsClient implements SmsClient { ...@@ -159,12 +154,10 @@ public class YunPianSmsClient implements SmsClient {
throw new ServiceException(SystemErrorCodeEnum.SMS_PLATFORM_FAIL.getCode(), throw new ServiceException(SystemErrorCodeEnum.SMS_PLATFORM_FAIL.getCode(),
jsonObject.getString("detail")); jsonObject.getString("detail"));
} }
// 用于递增 maxSendSize // 用于递增 maxSendSize
j = j2; j = j2;
j2 = j + maxSendSize; j2 = j + maxSendSize;
} }
return new SendResult() return new SendResult()
.setIsSuccess(true) .setIsSuccess(true)
.setCode(SUCCESS_CODE) .setCode(SUCCESS_CODE)
...@@ -181,18 +174,17 @@ public class YunPianSmsClient implements SmsClient { ...@@ -181,18 +174,17 @@ public class YunPianSmsClient implements SmsClient {
*/ */
private static String buildTemplate(String sign, String template, private static String buildTemplate(String sign, String template,
Map<String, String> templateParams) { Map<String, String> templateParams) {
// 不处理 empty 数据
if (CollectionUtils.isEmpty(templateParams)) { if (CollectionUtils.isEmpty(templateParams)) {
return template; return template;
} }
// 处理template参数
for (Map.Entry<String, String> entry : templateParams.entrySet()) { for (Map.Entry<String, String> entry : templateParams.entrySet()) {
String paramsKey = entry.getKey(); String paramsKey = entry.getKey();
String value = entry.getValue(); String value = entry.getValue();
String paramPlace = String.format(PARAM_TEMPLATE, paramsKey); String paramPlace = String.format(PARAM_TEMPLATE, paramsKey);
template = template.replaceAll(paramPlace, value); template = template.replaceAll(paramPlace, value);
} }
template = String.format(SIGN_TEMPLATE, sign, template); template = String.format(SIGN_TEMPLATE, sign, template);
return template; return template;
} }
...@@ -234,7 +226,6 @@ public class YunPianSmsClient implements SmsClient { ...@@ -234,7 +226,6 @@ public class YunPianSmsClient implements SmsClient {
e.printStackTrace(); e.printStackTrace();
} }
} }
LOGGER.debug("云片短信平台 res: {}", responseText); LOGGER.debug("云片短信平台 res: {}", responseText);
return responseText; return responseText;
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论