修复网易云短信发送失败bug,并更改版本号为3.0

This commit is contained in:
tiejiaxiaobao 2023-08-26 12:35:14 +08:00
parent fe5ce4999d
commit db8d699d19
3 changed files with 55 additions and 6 deletions

View File

@ -9,7 +9,7 @@ package org.dromara.sms4j.comm.constant;
**/ **/
public abstract class Constant { public abstract class Constant {
/** 项目版本号*/ /** 项目版本号*/
public static final String VERSION = "V 2.2.1"; public static final String VERSION = "V 3.0.0";
/** /**
* 用于格式化鉴权头域,"Authorization"参数赋值 * 用于格式化鉴权头域,"Authorization"参数赋值

View File

@ -93,24 +93,27 @@ public class NeteaseSmsImpl extends AbstractSmsBlend {
return getSmsResponse(config.getTemplateUrl(), phones, messageStr, templateId); return getSmsResponse(config.getTemplateUrl(), phones, messageStr, templateId);
} }
private SmsResponse getSmsResponse(String requestUrl, List<String> phones, String message, String templateId) { private SmsResponse getSmsResponse(String requestUrl, List<String> phones, String message, String templateId) {
String nonce = IdUtil.fastSimpleUUID(); String nonce = IdUtil.fastSimpleUUID();
String curTime = String.valueOf(DateUtil.currentSeconds()); String curTime = String.valueOf(DateUtil.currentSeconds());
String checkSum = NeteaseUtils.getCheckSum(config.getAccessKeySecret(), nonce, curTime); String checkSum = NeteaseUtils.getCheckSum(config.getAccessKeySecret(), nonce, curTime);
Map<String, Object> body = new LinkedHashMap<>(4); Map<String, String> body = new LinkedHashMap<>(4);
body.put("templateid", templateId); body.put("templateid", templateId);
JSONArray jsonArray = JSONUtil.createArray(); JSONArray jsonArray = JSONUtil.createArray();
JSONArray messageArray = JSONUtil.createArray();
messageArray.add(message);
jsonArray.addAll(phones); jsonArray.addAll(phones);
body.put("mobiles", jsonArray.toString()); body.put("mobiles", jsonArray.toString());
body.put("params", message); body.put("params", messageArray.toString());
body.put("needUp", config.getNeedUp()); body.put("needUp", config.getNeedUp().toString());
String paramStr = NeteaseUtils.generateParamBody(body);
try(HttpResponse response = HttpRequest.post(requestUrl) try(HttpResponse response = HttpRequest.post(requestUrl)
.header("Content-Type", "application/x-www-form-urlencoded") .header("Content-Type", "application/x-www-form-urlencoded")
.header("AppKey", config.getAccessKeyId()) .header("AppKey", config.getAccessKeyId())
.header("Nonce", nonce) .header("Nonce", nonce)
.header("CurTime", curTime) .header("CurTime", curTime)
.header("CheckSum", checkSum) .header("CheckSum", checkSum)
.body(JSONUtil.toJsonStr(body)) .body(paramStr)
.execute()){ .execute()){
JSONObject res = JSONUtil.parseObj(response.body()); JSONObject res = JSONUtil.parseObj(response.body());
return this.getResponse(res); return this.getResponse(res);

View File

@ -1,8 +1,18 @@
package org.dromara.sms4j.netease.utils; package org.dromara.sms4j.netease.utils;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import org.dromara.sms4j.aliyun.config.AlibabaConfig;
import org.dromara.sms4j.comm.exception.SmsBlendException; import org.dromara.sms4j.comm.exception.SmsBlendException;
import org.dromara.sms4j.netease.config.NeteaseConfig;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** /**
* Created with IntelliJ IDEA. * Created with IntelliJ IDEA.
@ -46,6 +56,42 @@ public class NeteaseUtils {
} }
return buf.toString(); return buf.toString();
} }
/**
* url编码
*/
private static String specialUrlEncode(String value) {
try {
return URLEncoder.encode(value, StandardCharsets.UTF_8.name()).replace("+", "%20")
.replace("*", "%2A").replace("%7E", "~");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
public static String generateParamBody(Map<String, String> body) {
StringBuilder sortQueryString = new StringBuilder();
for (String key : body.keySet()) {
sortQueryString.append("&").append(specialUrlEncode(key)).append("=")
.append(specialUrlEncode(body.get(key)));
}
return sortQueryString.substring(1);
}
private static Map<String, String> generateParamMap(NeteaseConfig neteaseConfig, List<String> phone, String message, String templateId) {
Map<String, String> paramMap = new HashMap<>();
JSONArray messageArray = JSONUtil.createArray();
messageArray.add(message);
JSONArray phoneArray = JSONUtil.createArray();
phoneArray.add(phone);
paramMap.put("mobiles", phoneArray.toString());
paramMap.put("SignName", neteaseConfig.getSignature());
paramMap.put("params", messageArray.toString());
paramMap.put("templateid", templateId);
paramMap.put("needUp", neteaseConfig.getNeedUp().toString());
return paramMap;
}
private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
} }