封装自定义短信发送

dev-shanao
shanao 2024-10-25 09:00:33 +08:00
parent 73500df19d
commit 93801a5250
2 changed files with 99 additions and 54 deletions

View File

@ -21,7 +21,7 @@ public class InsideMessagesImpl implements PushMessages {
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void push(PushRecords pushRecords, String userId) throws Exception { public void push(PushRecords pushRecords, String userId) throws Exception {
PageData mes = new PageData(); PageData mes = new PageData();
mes.put("BIANMA", pushRecords.getMessageTitle()); mes.put("BIANMA", pushRecords.getTemplateId());
mes.put("SENDER_ID", "九公司应急管理"); mes.put("SENDER_ID", "九公司应急管理");
mes.put("SENDER_NAME", "九公司应急管理"); mes.put("SENDER_NAME", "九公司应急管理");
mes.put("SYNOPSIS", pushRecords.getMessageTitle()); mes.put("SYNOPSIS", pushRecords.getMessageTitle());
@ -29,7 +29,7 @@ public class InsideMessagesImpl implements PushMessages {
mes.put("RECEIVER_ID", userId); mes.put("RECEIVER_ID", userId);
PageData content = new PageData(); PageData content = new PageData();
content.putAll(pushRecords.getMessageContentMap()); content.putAll(pushRecords.getMessageContentMap());
mes.put("CONTENT", content);// 站内信内容 mes.put("CONTENT", content);
noticeCorpService.sendNotice(mes); noticeCorpService.sendNotice(mes);
} }
} }

View File

@ -1,9 +1,8 @@
package com.zcloud.util; package com.zcloud.util;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
@ -17,7 +16,6 @@ import org.springframework.http.MediaType;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
/** /**
@ -82,23 +80,39 @@ public class SendSmsUtil {
System.out.println(result); System.out.println(result);
} }
/**
*
*
* @param mobile
* @param template xxx${var}xxx
* @param content
* @return
*/
public static boolean customizeSMSSending(String mobile, String template, Map<String, Object> content) { public static boolean customizeSMSSending(String mobile, String template, Map<String, Object> content) {
String value = SIGNATURE + template; // 替换模版中的变量 如 ${xxx}
for (String key : content.keySet()) { for (String key : content.keySet()) {
value = StrUtil.replace(value, "${" + key + "}", Convert.toStr(content.get(key))); template = StrUtil.replace(template, "${" + key + "}", Convert.toStr(content.get(key)));
} }
JSONObject json = new JSONObject(); return customizeSMSSending(mobile, template);
json.put("username", USERNAME); }
json.put("tKey", System.currentTimeMillis() / 1000);
json.put("password", MD5.md5(PASSWORD + json.getLong("tKey"))); /**
json.put("mobile", mobile); *
json.put("content", value); *
HttpRequest request = HttpRequest.post("https://api-bj-shss01-mix2.zthysms.com/v2/sendSms"); * @param mobile
request.body(json.toJSONString(), MediaType.APPLICATION_JSON_UTF8_VALUE); * @param content
request.timeout(60000); * @return
*/
private static boolean customizeSMSSending(String mobile, String content) {
if (content.length() >= 1000) {
throw new RuntimeException("短信内容最多支持1000个字符");
}
JSONObject json = createRequestJson(mobile, content);
HttpRequest request = createHttpRequest(json, "https://api-bj-shss01-mix2.zthysms.com/v2/sendSms");
String body; String body;
try (HttpResponse response = request.execute()) { try (HttpResponse response = request.execute()) {
body = response.body(); body = response.body();
log.info("短信发送结果:【{}】", body);
} catch (Exception e) { } catch (Exception e) {
log.error("短信发送异常", e); log.error("短信发送异常", e);
return false; return false;
@ -107,6 +121,37 @@ public class SendSmsUtil {
return object == null || object.getInteger("code") == 200; return object == null || object.getInteger("code") == 200;
} }
/**
*
*
* @param mobile
* @param content
* @return JSONObject
*/
private static JSONObject createRequestJson(String mobile, String content) {
JSONObject json = new JSONObject();
json.put("username", USERNAME);
json.put("tKey", System.currentTimeMillis() / 1000);
json.put("password", DigestUtil.md5(PASSWORD + json.getLong("tKey")));
json.put("mobile", mobile);
// 短信内容 必须要带上备案好的签名
json.put("content", SIGNATURE + content);
return json;
}
/**
*
*
* @param json
* @return HttpRequest
*/
private static HttpRequest createHttpRequest(JSONObject json,String url) {
HttpRequest request = HttpRequest.post(url);
request.body(json.toJSONString(), MediaType.APPLICATION_JSON_UTF8_VALUE);
request.timeout(60000);
return request;
}
/** /**
* *
@ -133,44 +178,44 @@ public class SendSmsUtil {
}//d22f8a5d4a35b3761dc9525186e652a0 }//d22f8a5d4a35b3761dc9525186e652a0
//测试接口 //测试接口
public static void main(String[] args) throws ParseException { // public static void main(String[] args) throws ParseException {
//定义替换参数
Map<String, Object> map = new HashMap<>();
map.put("acci_date", DateUtil.format(new Date(), DatePattern.CHINESE_DATE_PATTERN));
map.put("acci_addr", "xx省xx市xxx");
map.put("acci_type", "测试类型");
map.put("inju_member", "0");
map.put("task_post", "测试");
String value = SIGNATURE + "安全事故通知:${acci_date},在${acci_addr}发生了${acci_type}的安全事故,有${inju_member}的伤亡人数,您的职责是:${task_post}";
for (String key : map.keySet()) {
value = StrUtil.replace(value, "${" + key + "}", Convert.toStr(map.get(key)));
}
if (value.length() >= 1000) {
throw new RuntimeException("短信内容最多支持1000个字符");
}
Long tKey = System.currentTimeMillis() / 1000;
String passWord = MD5.md5(PASSWORD + tKey);
JSONObject map2 = new JSONObject();
map2.put("username", USERNAME);
map2.put("tKey", tKey);
map2.put("password", passWord);
map2.put("mobile", "18630387571");
map2.put("content", value);
HttpRequest request = HttpRequest.post("https://api-bj-shss01-mix2.zthysms.com/v2/sendSms");
request.body(map2.toJSONString(), MediaType.APPLICATION_JSON_UTF8_VALUE);
System.out.println(map2.toJSONString());
request.timeout(60000);
String s = request.execute().body();
System.out.println(s);
// JSONArray jsonArray = new JSONArray();
// //接受返回结果
// JSONObject records = new JSONObject();
// //定义替换参数 // //定义替换参数
// JSONObject tpContent = new JSONObject(); // Map<String, Object> map = new HashMap<>();
// tpContent.put("OUTSOURCEDNAME","测试"); // map.put("acci_date", DateUtil.format(new Date(), DatePattern.CHINESE_DATE_PATTERN));
// records = SendSmsUtil.getRecords("18630387571",tpContent); // map.put("acci_addr", "xx省xx市xxx");
// jsonArray.add(records); // map.put("acci_type", "测试类型");
// SendSmsUtil.sendSms("121451",jsonArray,null); // map.put("inju_member", "0");
} // map.put("task_post", "测试");
// String value = SIGNATURE + "安全事故通知:${acci_date},在${acci_addr}发生了${acci_type}的安全事故,有${inju_member}的伤亡人数,您的职责是:${task_post}";
// for (String key : map.keySet()) {
// value = StrUtil.replace(value, "${" + key + "}", Convert.toStr(map.get(key)));
// }
// if (value.length() >= 1000) {
// throw new RuntimeException("短信内容最多支持1000个字符");
// }
// Long tKey = System.currentTimeMillis() / 1000;
// String passWord = MD5.md5(PASSWORD + tKey);
// JSONObject map2 = new JSONObject();
// map2.put("username", USERNAME);
// map2.put("tKey", tKey);
// map2.put("password", passWord);
// map2.put("mobile", "18630387571");
// map2.put("content", value);
// HttpRequest request = HttpRequest.post("https://api-bj-shss01-mix2.zthysms.com/v2/sendSms");
// request.body(map2.toJSONString(), MediaType.APPLICATION_JSON_UTF8_VALUE);
// System.out.println(map2.toJSONString());
// request.timeout(60000);
// String s = request.execute().body();
// System.out.println(s);
//// JSONArray jsonArray = new JSONArray();
//// //接受返回结果
//// JSONObject records = new JSONObject();
//// //定义替换参数
//// JSONObject tpContent = new JSONObject();
//// tpContent.put("OUTSOURCEDNAME","测试");
//// records = SendSmsUtil.getRecords("18630387571",tpContent);
//// jsonArray.add(records);
//// SendSmsUtil.sendSms("121451",jsonArray,null);
// }
} }