Commit 8386fe38 by 叶立庆

[+]短信发送服务调用

1 parent 4717190b
package cn.com.glsx.execute.modules.model.sms;
import lombok.Data;
import lombok.ToString;
/**
* @Author yelq
* @Version 1.0
* @Description:
* @Date 2023/9/7 17:53
*/
@ToString
@Data
public class SmsResponseDTO {
private String code;
private String message;
private Object datas;
}
package cn.com.glsx.execute.modules.service.proxy; package cn.com.glsx.execute.modules.service.proxy;
import cn.com.glsx.execute.modules.model.sms.SmsResponseDTO;
import cn.com.glsx.execute.utils.RopHttpUtil; import cn.com.glsx.execute.utils.RopHttpUtil;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum; import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
import cn.com.glsx.framework.core.exception.RpcServiceException; import cn.com.glsx.framework.core.exception.RpcServiceException;
...@@ -28,8 +29,8 @@ public class SendMessageService { ...@@ -28,8 +29,8 @@ public class SendMessageService {
* @param type 类型 * @param type 类型
* @param context 发送内容 * @param context 发送内容
*/ */
public void sendMessage(String phone, String source, String type, String context) { public SmsResponseDTO sendMessage(String phone, String source, String type, String context) {
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>(4); MultiValueMap<String, String> params = new LinkedMultiValueMap<>(4);
try { try {
params.add("phone", phone); params.add("phone", phone);
params.add("source", source); params.add("source", source);
...@@ -37,11 +38,12 @@ public class SendMessageService { ...@@ -37,11 +38,12 @@ public class SendMessageService {
params.add("type", type); params.add("type", type);
params.add("context", context); params.add("context", context);
log.info("短信发送参数:{}", JSON.toJSONString(params)); log.info("短信发送参数:{}", JSON.toJSONString(params));
String post = RopHttpUtil.postE(params, String.class, sendMessageUrl + "?method=" + sendMessageMethod, sendMessageMethod,null); SmsResponseDTO post = RopHttpUtil.postE(params, SmsResponseDTO.class, sendMessageUrl + "?method=" + sendMessageMethod, sendMessageMethod,null);
log.info("发送短信结果{}",post); log.info("发送短信结果{}",post);
return post;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
log.info("发送短信异常,{}", e); log.info("发送短信异常:", e);
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR); throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR);
} }
} }
......
package cn.com.glsx.execute.utils;
import java.security.MessageDigest;
/**
* @author zhouhaibao
* @version 1.0
* @description: TODO
* @date 2021/11/23 21:39
*/
public class MD5 {
/**
* 32位MD5加密算法
* @param s
* @return
*/
public static String convert32(String s){
char hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
try {
byte[] bytes = s.getBytes();
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(bytes);
bytes = md.digest();
int j = bytes.length;
char[] chars = new char[j * 2];
int k = 0;
for (int i = 0; i < bytes.length; i++) {
byte b = bytes[i];
chars[k++] = hexChars[b >>> 4 & 0xf];
chars[k++] = hexChars[b & 0xf];
}
return new String(chars);
}
catch (Exception e){
return null;
}
}
/**
* 16位MD5加密算法
* @param s
* @return
*/
public static String convert16(String s){
char hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
try {
byte[] bytes = s.getBytes();
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(bytes);
bytes = md.digest();
int j = bytes.length;
char[] chars = new char[j * 2];
int k = 0;
for (int i = 0; i < bytes.length; i++) {
byte b = bytes[i];
chars[k++] = hexChars[b >>> 4 & 0xf];
chars[k++] = hexChars[b & 0xf];
}
return new String(chars).substring(8, 24);
}
catch (Exception e){
return null;
}
}
}
...@@ -103,7 +103,7 @@ public class RopHttpUtil { ...@@ -103,7 +103,7 @@ public class RopHttpUtil {
params.remove(entry.getKey()); params.remove(entry.getKey());
} }
} }
String sign = RopUtils.sign(params.toSingleValueMap(), appKeyEnum == null ? secret : appKeyEnum.getSecret()); String sign = RopUtils.signE(params.toSingleValueMap(),null, appKeyEnum == null ? secret : appKeyEnum.getSecret());
params.add("sign", sign); params.add("sign", sign);
return new RestTemplate().postForObject(baseurl, params, responseType); return new RestTemplate().postForObject(baseurl, params, responseType);
} }
......
...@@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory; ...@@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.*; import java.util.*;
...@@ -16,17 +17,17 @@ public class RopUtils { ...@@ -16,17 +17,17 @@ public class RopUtils {
} }
public static String sign(Map<String, String> paramValues, String secret) { public static String sign(Map<String, String> paramValues, String secret) {
return sign(paramValues, (List) null, secret); return sign(paramValues, null, secret);
} }
public static String sign(Map<String, String> paramValues, List<String> ignoreParamNames, String secret) { public static String sign(Map<String, String> paramValues, List<String> ignoreParamNames, String secret) {
try { try {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
List<String> paramNames = new ArrayList(paramValues.size()); List<String> paramNames = new ArrayList<>(paramValues.size());
paramNames.addAll(paramValues.keySet()); paramNames.addAll(paramValues.keySet());
Iterator var5; Iterator var5;
String paramName; String paramName;
if (ignoreParamNames != null && ignoreParamNames.size() > 0) { if (ignoreParamNames != null && !ignoreParamNames.isEmpty()) {
var5 = ignoreParamNames.iterator(); var5 = ignoreParamNames.iterator();
while (var5.hasNext()) { while (var5.hasNext()) {
...@@ -41,7 +42,7 @@ public class RopUtils { ...@@ -41,7 +42,7 @@ public class RopUtils {
while (var5.hasNext()) { while (var5.hasNext()) {
paramName = (String) var5.next(); paramName = (String) var5.next();
sb.append(paramName).append((String) paramValues.get(paramName)); sb.append(paramName).append(paramValues.get(paramName));
} }
sb.append(secret); sb.append(secret);
...@@ -55,31 +56,25 @@ public class RopUtils { ...@@ -55,31 +56,25 @@ public class RopUtils {
public static String utf8Encoding(String value, String sourceCharsetName) { public static String utf8Encoding(String value, String sourceCharsetName) {
try { try {
return new String(value.getBytes(sourceCharsetName), "UTF-8"); return new String(value.getBytes(sourceCharsetName), StandardCharsets.UTF_8);
} catch (UnsupportedEncodingException var3) { } catch (UnsupportedEncodingException var3) {
throw new IllegalArgumentException(var3); throw new IllegalArgumentException(var3);
} }
} }
private static byte[] getSHA1Digest(String data) throws IOException { private static byte[] getSHA1Digest(String data) throws IOException {
Object var1 = null;
try { try {
MessageDigest md = MessageDigest.getInstance("SHA-1"); MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] bytes = md.digest(data.getBytes("UTF-8")); return md.digest(data.getBytes(StandardCharsets.UTF_8));
return bytes;
} catch (GeneralSecurityException var3) { } catch (GeneralSecurityException var3) {
throw new IOException(var3); throw new IOException(var3);
} }
} }
private static byte[] getMD5Digest(String data) throws IOException { private static byte[] getMD5Digest(String data) throws IOException {
Object var1 = null;
try { try {
MessageDigest md = MessageDigest.getInstance("MD5"); MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = md.digest(data.getBytes("UTF-8")); return md.digest(data.getBytes(StandardCharsets.UTF_8));
return bytes;
} catch (GeneralSecurityException var3) { } catch (GeneralSecurityException var3) {
throw new IOException(var3); throw new IOException(var3);
} }
...@@ -88,8 +83,8 @@ public class RopUtils { ...@@ -88,8 +83,8 @@ public class RopUtils {
private static String byte2hex(byte[] bytes) { private static String byte2hex(byte[] bytes) {
StringBuilder sign = new StringBuilder(); StringBuilder sign = new StringBuilder();
for (int i = 0; i < bytes.length; ++i) { for (byte aByte : bytes) {
String hex = Integer.toHexString(bytes[i] & 255); String hex = Integer.toHexString(aByte & 255);
if (hex.length() == 1) { if (hex.length() == 1) {
sign.append("0"); sign.append("0");
} }
...@@ -105,4 +100,23 @@ public class RopUtils { ...@@ -105,4 +100,23 @@ public class RopUtils {
return uuid.toString().toUpperCase(); return uuid.toString().toUpperCase();
} }
public static String signE(Map<String, String> paramValues, List<String> ignoreParamNames, String secret) {
StringBuilder sb = new StringBuilder();
List<String> paramNames = new ArrayList<>(paramValues.size());
paramNames.addAll(paramValues.keySet());
if (ignoreParamNames != null && !ignoreParamNames.isEmpty()) {
for (String ignoreParamName : ignoreParamNames) {
paramNames.remove(ignoreParamName);
}
}
Collections.sort(paramNames);
sb.append(secret);
for (String paramName : paramNames) {
sb.append(paramName).append(paramValues.get(paramName));
}
sb.append(secret);
return MD5.convert32(sb.toString());
}
} }
package cn.com.glsx.cn.com.glsx.Utils; package cn.com.glsx.cn.com.glsx.Utils;
import cn.com.glsx.execute.modules.model.RopResponseDTO; import cn.com.glsx.execute.modules.model.rop.RopResponseDTO;
import cn.com.glsx.execute.utils.RopHttpUtil; import cn.com.glsx.execute.utils.RopHttpUtil;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
......
package cn.com.glsx.cn.com.glsx.Utils;
import cn.com.glsx.execute.modules.model.sms.SmsResponseDTO;
import cn.com.glsx.execute.modules.service.proxy.SendMessageService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
/**
* @Author yelq
* @Version 1.0
* @Description:
* @Date 2023/9/7 17:14
*/
@ComponentScan(basePackages = "cn.com.glsx.execute,cn.com.glsx.cn.com.glsx")
@SpringBootTest
public class SendMessageServiceTest {
@Autowired
private SendMessageService sendMessageService;
@Autowired
private Environment environment;
@Test
public void Test01() throws Exception{
//sendMessageUrl=http://qcdidicb.glsx.com.cn:7060/router
//sendMessageMethod=glsx.accounts.send.message
// SendMessageService sendMessageService = new SendMessageService();
String text = "【波波的单店门店】SOP营销测试https://dwz.cn/sHkDTMo9";
SmsResponseDTO s = sendMessageService.sendMessage("18126301457", "djsms-marketing", "1", text);
System.out.println(s);
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!