qa-prevention-gwj/src/main/java/com/zcloud/util/SendSmsUtil.java

177 lines
6.6 KiB
Java
Raw Normal View History

2023-11-07 09:32:12 +08:00
package com.zcloud.util;
2024-10-24 18:23:38 +08:00
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
2024-10-24 18:23:38 +08:00
import cn.hutool.core.util.StrUtil;
2023-11-07 09:32:12 +08:00
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
2023-11-07 09:32:12 +08:00
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
2023-11-07 09:32:12 +08:00
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.MediaType;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
2024-10-24 18:23:38 +08:00
import java.util.HashMap;
import java.util.Map;
2023-11-07 09:32:12 +08:00
/**
*
*/
@Slf4j
2023-11-07 09:32:12 +08:00
public class SendSmsUtil {
private static String USERNAME = "qhdzyhy";
private static String PASSWORD = "3ba40593f514f0c1ebdfc278dddfc9ce";
private static String SIGNATURE = "【秦港双控】";
private static String URL = "https://api.mix2.zthysms.com/v2/sendSmsTp";
/**
*
*
2023-11-07 09:32:12 +08:00
* @param records
* @param time
* records records JSONArray getRecords JSONObject JSONArray put
* "records":[
* {
* "mobile":"138****0000",
* "tpContent":{
* "var1":"变量1",
* "var2":"变量2"
* }
* },
* {
* "mobile":"138****0001",
* "tpContent":{
* "var1":"变量2",
* "var2":"变量2"
* }
* }
* ]
2023-11-07 09:32:12 +08:00
*/
public static void sendSms(String tpId, JSONArray records, String time) throws ParseException {
2023-11-07 09:32:12 +08:00
JSONObject json = new JSONObject();
Long tKey = System.currentTimeMillis() / 1000;
String passWord = MD5.md5(PASSWORD + tKey);
2023-11-07 09:32:12 +08:00
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//扔参数
json.put("username", USERNAME);
json.put("password", passWord);
json.put("tKey", tKey);
json.put("signature", SIGNATURE);
json.put("tpId", tpId);
if (StringUtils.isNotBlank(time)) {
if (sdf.parse(time).after(new Date())) {
json.put("time", time);
2023-11-07 09:32:12 +08:00
}
}
json.put("records", records);
2023-11-07 09:32:12 +08:00
System.out.println(json.toJSONString());
String result = HttpRequest.post(URL)
.timeout(60000)
.body(json.toJSONString(), MediaType.APPLICATION_JSON_UTF8_VALUE).execute().body();
System.out.println(result);
}
2024-10-24 18:23:38 +08:00
public static boolean customizeSMSSending(String mobile, String template, Map<String, Object> content) {
String value = SIGNATURE + template;
for (String key : content.keySet()) {
value = StrUtil.replace(value, "${" + key + "}", Convert.toStr(content.get(key)));
}
JSONObject json = new JSONObject();
json.put("username", USERNAME);
json.put("tKey", System.currentTimeMillis() / 1000);
json.put("password", MD5.md5(PASSWORD + json.getLong("tKey")));
2024-10-24 18:23:38 +08:00
json.put("mobile", mobile);
json.put("content", value);
HttpRequest request = HttpRequest.post("https://api-bj-shss01-mix2.zthysms.com/v2/sendSms");
request.body(json.toJSONString(), MediaType.APPLICATION_JSON_UTF8_VALUE);
request.timeout(60000);
String body;
try (HttpResponse response = request.execute()) {
2024-10-24 18:23:38 +08:00
body = response.body();
} catch (Exception e) {
2024-10-24 18:23:38 +08:00
log.error("短信发送异常", e);
return false;
}
2024-10-24 18:23:38 +08:00
JSONObject object = JSON.parseObject(body);
return object == null || object.getInteger("code") == 200;
}
2023-11-07 09:32:12 +08:00
/**
*
*
* @param mobile
2023-11-07 09:32:12 +08:00
* @param tpContent
* @return var1 1
* {
* "mobile":"138****0000",
* "tpContent":{
* "var1":"变量1",
* "var2":"变量2"
* }
* }
2023-11-07 09:32:12 +08:00
*/
public static JSONObject getRecords(String mobile, JSONObject tpContent) {
2023-11-07 09:32:12 +08:00
JSONObject json = new JSONObject();
json.put("mobile", mobile);
if (ObjectUtils.hashCode(tpContent) != 0) {
json.put("tpContent", tpContent);
2023-11-07 09:32:12 +08:00
}
return json;
}//d22f8a5d4a35b3761dc9525186e652a0
//测试接口
2024-10-24 18:23:38 +08:00
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();
2024-10-24 18:23:38 +08:00
// tpContent.put("OUTSOURCEDNAME","测试");
// records = SendSmsUtil.getRecords("18630387571",tpContent);
// jsonArray.add(records);
// SendSmsUtil.sendSms("121451",jsonArray,null);
}
2023-11-07 09:32:12 +08:00
}