2023-11-07 09:32:12 +08:00
|
|
|
package com.zcloud.util;
|
|
|
|
|
2024-10-21 18:26:02 +08:00
|
|
|
import cn.hutool.core.date.DateUtil;
|
2023-11-07 09:32:12 +08:00
|
|
|
import cn.hutool.http.HttpRequest;
|
2024-10-21 18:26:02 +08:00
|
|
|
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;
|
2024-10-21 18:26:02 +08:00
|
|
|
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-21 18:26:02 +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";
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 发送短信
|
2024-10-21 18:26:02 +08:00
|
|
|
*
|
2023-11-07 09:32:12 +08:00
|
|
|
* @param records
|
2024-10-21 18:26:02 +08:00
|
|
|
* @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
|
|
|
*/
|
2024-10-21 18:26:02 +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();
|
2024-10-21 18:26:02 +08:00
|
|
|
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");
|
|
|
|
//扔参数
|
2024-10-21 18:26:02 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2024-10-21 18:26:02 +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-21 18:26:02 +08:00
|
|
|
// {"msg":"template error","tpId":"null","code":4014,"msgId":"172950016144709288961"}
|
|
|
|
// {"msg":"success","tpId":"121487","code":200,"msgId":"172950025828957168641","invalidList":[]}
|
|
|
|
public static boolean sendSms(String tpId, JSONObject records, String time, String phone) throws ParseException {
|
|
|
|
JSONObject json = new JSONObject();
|
|
|
|
json.put("username", USERNAME);
|
|
|
|
json.put("tKey", System.currentTimeMillis() / 1000);
|
|
|
|
json.put("password", MD5.md5(PASSWORD + json.getLong("tKey")));
|
|
|
|
json.put("signature", SIGNATURE);
|
|
|
|
json.put("tpId", tpId);
|
|
|
|
if (StringUtils.isNotBlank(time) && DateUtil.parseDateTime(time).after(new Date())) {
|
|
|
|
json.put("time", time);
|
|
|
|
}
|
|
|
|
JSONArray array = new JSONArray();
|
|
|
|
array.add(getRecords(phone, records));
|
|
|
|
json.put("records", array);
|
|
|
|
HttpRequest request = HttpRequest.post(URL)
|
|
|
|
.timeout(60000)
|
|
|
|
.body(json.toJSONString(), MediaType.APPLICATION_JSON_UTF8_VALUE);
|
|
|
|
try (HttpResponse response = request.execute()) {
|
|
|
|
return JSON.parseObject(response.body()).getInteger("code") == 200;
|
|
|
|
} catch (Exception e) {
|
|
|
|
log.error("发送短信异常", e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-07 09:32:12 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 封装要发送的电话以及要替换的内容
|
2024-10-21 18:26:02 +08:00
|
|
|
*
|
|
|
|
* @param mobile 手机号
|
2023-11-07 09:32:12 +08:00
|
|
|
* @param tpContent 替换内容
|
2024-10-21 18:26:02 +08:00
|
|
|
* @return 格式实例 var1表示要替换的内容 与要替换的内容保持一致即可 变量1表示替换的内容
|
|
|
|
* {
|
|
|
|
* "mobile":"138****0000",
|
|
|
|
* "tpContent":{
|
|
|
|
* "var1":"变量1",
|
|
|
|
* "var2":"变量2"
|
|
|
|
* }
|
|
|
|
* }
|
2023-11-07 09:32:12 +08:00
|
|
|
*/
|
2024-10-21 18:26:02 +08:00
|
|
|
public static JSONObject getRecords(String mobile, JSONObject tpContent) {
|
2023-11-07 09:32:12 +08:00
|
|
|
JSONObject json = new JSONObject();
|
2024-10-21 18:26:02 +08:00
|
|
|
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-21 18:26:02 +08:00
|
|
|
// public static void main(String[] args) throws ParseException {
|
|
|
|
// //定义替换参数
|
|
|
|
// JSONObject tpContent = new JSONObject();
|
|
|
|
// tpContent.put("HIDDENDESCR", "卓云企业测试");
|
|
|
|
// JSONObject object = sendSms("121487", tpContent, DateUtil.formatDateTime(new Date()), "18630387571");
|
|
|
|
// System.out.println(object.toJSONString());
|
|
|
|
// }
|
2023-11-07 09:32:12 +08:00
|
|
|
|
|
|
|
}
|