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

116 lines
3.6 KiB
Java
Raw Normal View History

2023-11-07 09:32:12 +08:00
package com.zcloud.util;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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;
/**
*
*/
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";
/**
*
* @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"
* }
* }
* ]
*/
public static void sendSms(String tpId,JSONArray records ,String time) throws ParseException {
JSONObject json = new JSONObject();
Long tKey = System.currentTimeMillis()/1000;
String passWord = MD5.md5(PASSWORD+tKey);
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);
}
}
json.put("records",records);
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);
}
/**
*
* @param mobile
* @param tpContent
* @return
* var1 1
* {
* "mobile":"138****0000",
* "tpContent":{
* "var1":"变量1",
* "var2":"变量2"
* }
* }
*/
public static JSONObject getRecords(String mobile, JSONObject tpContent ){
JSONObject json = new JSONObject();
json.put("mobile",mobile);
if(ObjectUtils.hashCode(tpContent)!=0){
json.put("tpContent",tpContent);
}
return json;
}//d22f8a5d4a35b3761dc9525186e652a0
//测试接口
public static void main(String[] args) throws ParseException {
JSONArray jsonArray = new JSONArray();
//接受返回结果
JSONObject records = new JSONObject();
//定义替换参数
//JSONObject tpContent = new JSONObject();
//tpContent.put("userName","卓云企业");
//tpContent.put("time","2023-09-21 15:56:20");
//records = getRecords("18617456701",tpContent);
jsonArray.add(records);
sendSms("null",jsonArray,null);
}
}