|
|
|
|
@ -9,6 +9,8 @@ import com.ynxbd.common.bean.sms.SmsTemplate; |
|
|
|
|
import com.ynxbd.common.config.EhCacheConfig; |
|
|
|
|
import com.ynxbd.common.helper.ProperHelper; |
|
|
|
|
import com.ynxbd.common.helper.http.OkHttpHelper; |
|
|
|
|
import com.ynxbd.common.result.Result; |
|
|
|
|
import com.ynxbd.common.result.ResultEnum; |
|
|
|
|
import com.ynxbd.common.service.UsageCountService; |
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
import okhttp3.FormBody; |
|
|
|
|
@ -19,6 +21,9 @@ import org.apache.commons.lang3.StringUtils; |
|
|
|
|
import org.ehcache.Cache; |
|
|
|
|
|
|
|
|
|
import java.util.Date; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
@Slf4j |
|
|
|
|
public class SmsHelper { |
|
|
|
|
@ -214,14 +219,28 @@ public class SmsHelper { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 天助平台(短信平台) |
|
|
|
|
* [天助平台-旦米短信] |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
public static boolean sendTemplateDanMi(String tel, String template, JSONObject smsJson) { |
|
|
|
|
Map<String, String> map = SmsHelper.jsonObjToMap(smsJson); |
|
|
|
|
if (map == null) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
String content = SmsHelper.replaceTemplate(template, map); |
|
|
|
|
return sendDanMi("1000000002608", tel, content); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* [天助平台-旦米短信] |
|
|
|
|
* |
|
|
|
|
* @param templateId 模板id |
|
|
|
|
* @param tel 电话号码 |
|
|
|
|
* @param content 发送内容 |
|
|
|
|
* @return string |
|
|
|
|
*/ |
|
|
|
|
public static String sendDanMi(String templateId, String tel, String content) { |
|
|
|
|
public static boolean sendDanMi(String templateId, String tel, String content) { |
|
|
|
|
String accountSid = "301d69a6ea6ed34b0827a4dec3ca1ff8"; |
|
|
|
|
String authToken = "94f37caeddeb36a8400ad0ecc9b04ece"; |
|
|
|
|
String timestamp = String.valueOf(new Date().getTime()); |
|
|
|
|
@ -233,14 +252,57 @@ public class SmsHelper { |
|
|
|
|
.add("param", content) |
|
|
|
|
.add("sig", DigestUtils.md5Hex(accountSid + authToken + timestamp)) |
|
|
|
|
.build(); |
|
|
|
|
String result = OkHttpHelper.post("https://openapi.danmi.com/textSMS/sendSMS/V1", requestBody, headers -> { |
|
|
|
|
String respJsonStr = OkHttpHelper.post("https://openapi.danmi.com/textSMS/sendSMS/V1", requestBody, headers -> { |
|
|
|
|
headers.add("content-type", "application/x-www-form-urlencoded"); |
|
|
|
|
}); |
|
|
|
|
new UsageCountService().updateUsageCount(UsageCountEnum.TZ_SMS, null, true); // 人数统计
|
|
|
|
|
log.info("[天助平台] 短信发送 result-{},tel-{},templateId-{},content-{}", result, tel, templateId, content); |
|
|
|
|
log.info("[天助平台-旦米短信]发送 resp-{}, tel-{}, templateId-{}, content-{}", respJsonStr, tel, templateId, content); |
|
|
|
|
if (ObjectUtils.isEmpty(respJsonStr)) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
JSONObject respJson = JsonHelper.parseObject(respJsonStr); |
|
|
|
|
Boolean success = respJson.getBoolean("success"); |
|
|
|
|
return success != null && success; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 通用模板替换工具 |
|
|
|
|
* |
|
|
|
|
* @param template 带[p1][p2]模板字符串 |
|
|
|
|
* @param map key:p1/p2/p3/p4 value:替换内容 |
|
|
|
|
* @return 替换完成文本 |
|
|
|
|
*/ |
|
|
|
|
public static String replaceTemplate(String template, Map<String, String> map) { |
|
|
|
|
if (template == null) { |
|
|
|
|
return ""; |
|
|
|
|
} |
|
|
|
|
if (map == null || map.isEmpty()) { |
|
|
|
|
return template; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 转换key为 ${xxx} 格式,过滤value为null的键值对
|
|
|
|
|
Map<String, String> replaceMap = map.entrySet().stream() |
|
|
|
|
.filter(entry -> entry.getValue() != null) // 剔除value为null,避免替换时报错
|
|
|
|
|
.collect(Collectors.toMap(entry -> "${" + entry.getKey() + "}", Map.Entry::getValue)); |
|
|
|
|
|
|
|
|
|
String result = template; |
|
|
|
|
for (Map.Entry<String, String> entry : replaceMap.entrySet()) { |
|
|
|
|
String key = entry.getKey(); |
|
|
|
|
String value = entry.getValue(); |
|
|
|
|
result = result.replace(key, value); // 空字符串仍执行替换,把占位符清空;不需要替换可加判断跳过
|
|
|
|
|
} |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// public static void main(String[] args) {
|
|
|
|
|
// String template = "尊敬的${p1},温馨提醒:您上次${p2}就诊时预约了${p3}到${p4}复诊,无法就诊请提前改期。";
|
|
|
|
|
// Map<String, String> param = new HashMap<>();
|
|
|
|
|
// param.put("p1", "李四");
|
|
|
|
|
// param.put("p3", "2026-08-01");
|
|
|
|
|
// param.put("p4", "2026-08-05");
|
|
|
|
|
// String result = replaceTemplate(template, param);
|
|
|
|
|
// System.out.println(result);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 是否重复发送 |
|
|
|
|
@ -327,6 +389,17 @@ public class SmsHelper { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Map<String, String> jsonObjToMap(JSONObject jsonObj) { |
|
|
|
|
if (jsonObj == null) { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
Map<String, String> map = new HashMap<>(); |
|
|
|
|
for (String key : jsonObj.keySet()) { |
|
|
|
|
map.put(key, jsonObj.getString(key)); |
|
|
|
|
} |
|
|
|
|
return map; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static String encodeRSA(String data) { |
|
|
|
|
return RSAHelper.encrypt(data, PUBLIC_KEY); |
|
|
|
|
} |
|
|
|
|
@ -336,7 +409,6 @@ public class SmsHelper { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// private void isCache() {
|
|
|
|
|
// if (CACHE == null) { // 判断是否重复发送验证码
|
|
|
|
|
// // 建立一个缓存实例
|
|
|
|
|
|