|
|
@@ -0,0 +1,405 @@
|
|
|
+package com.zd.laboratory.utils;
|
|
|
+
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.zd.common.core.utils.HttpUtils;
|
|
|
+import com.zd.common.core.utils.MD5Utils;
|
|
|
+import com.zd.laboratory.domain.vo.IotTextMessageVo;
|
|
|
+import org.apache.http.HttpStatus;
|
|
|
+
|
|
|
+import java.time.ZonedDateTime;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Author donggaosheng
|
|
|
+ * @Date 2024-02-05
|
|
|
+ * @description 短信发送类
|
|
|
+ **/
|
|
|
+public class TextMessageUtils {
|
|
|
+
|
|
|
+ //将长链接转换成短链
|
|
|
+ private static final String URL_CONVERT="https://api.mix2.zthysms.com/v1/shortLink/add";
|
|
|
+
|
|
|
+ //客户报备需要发送的短信模板,审核通过之后可以直接通过模板进行发送
|
|
|
+ private static final String TEMPLATE_URL="https://api.mix2.zthysms.com/sms/v2/template";
|
|
|
+
|
|
|
+ //通过已报备的短信模板进行发送。
|
|
|
+ private static final String SEND_TEMPLATE_URL="https://api.mix2.zthysms.com/v2/sendSmsTp";
|
|
|
+
|
|
|
+ //接口发送语音通知需要通过此接口发送
|
|
|
+ private static final String VOICE_URL="https://api-gateway.zthysms.com/voice/v1/notice/send";
|
|
|
+
|
|
|
+ //获取账号的语音验证码余额
|
|
|
+ private static final String BALANCE_URL="https://api-gateway.zthysms.com/voice/v1/captcha/balance";
|
|
|
+
|
|
|
+ //短信模板查询列表接口
|
|
|
+ private static final String TEMPLATE_LIST_URL="https://api.mix2.zthysms.com/sms/v2/template/list";
|
|
|
+
|
|
|
+ //查询模版查询接口
|
|
|
+ private static final String TEMPLATE_QUERY_URL="https://api.mix2.zthysms.com/sms/v2/template/query";
|
|
|
+
|
|
|
+ //客户主动抓取短信下行状态报告。通过本接口长短信可以抓取多条状态报告
|
|
|
+ private static final String MESSAGE_BATCH_REPORT="https://api.mix2.zthysms.com/v2/batchReport";
|
|
|
+
|
|
|
+ //获取语音短信回执信息接口。
|
|
|
+ private static final String VOICE_BATCH_REPORT="https://api-gateway.zthysms.com/voice/v1/report";
|
|
|
+
|
|
|
+ //短连接域名
|
|
|
+ private static final String DOMAIN_URL="zt81.cn/";
|
|
|
+
|
|
|
+ //请求数据格式
|
|
|
+ private static final String JSON_TYPE="application/json";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将长连接转换成短连接
|
|
|
+ * @param url
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String convertUrl(String userName,String passWord,String url){
|
|
|
+ JSONObject jsonObject=getCommon(userName,passWord);;
|
|
|
+ jsonObject.put("originalUrl",url);
|
|
|
+ String data=HttpUtils.sendPost(URL_CONVERT,jsonObject.toJSONString(),JSON_TYPE);
|
|
|
+ JSONObject jsonData=JSONObject.parseObject(data);
|
|
|
+ if(jsonData.getIntValue("code")== HttpStatus.SC_OK){
|
|
|
+ return jsonData.getString("shortCode");
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 客户主动抓取短信下行状态报告。通过本接口长短信可以抓取多条状态报告
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static JSONObject getMessageReport(String userName,String passWord){
|
|
|
+ JSONObject jsonObject=getCommon(userName,passWord);;
|
|
|
+ String data=HttpUtils.sendPost(MESSAGE_BATCH_REPORT,jsonObject.toJSONString(),JSON_TYPE);
|
|
|
+ JSONObject jsonData=JSONObject.parseObject(data);
|
|
|
+ return jsonData;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取语音短信回执信息接口。
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static JSONObject getVoiceReport(String userName,String passWord){
|
|
|
+ HttpResponse reponse = HttpRequest.get(VOICE_BATCH_REPORT)
|
|
|
+ .basicAuth(userName,passWord)
|
|
|
+ .header("Content-Type", "application/json")
|
|
|
+ .charset("utf-8")
|
|
|
+ .execute();
|
|
|
+ System.out.println(reponse.toString());
|
|
|
+ return JSONObject.parseObject(reponse.body());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通用参数处理方法
|
|
|
+ * @param userName
|
|
|
+ * @param passWord
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static JSONObject getCommon(String userName,String passWord) {
|
|
|
+ String timeStamp = String.valueOf(ZonedDateTime.now().toEpochSecond());
|
|
|
+ String passWd=passwordEncrypt(passWord,timeStamp);
|
|
|
+ JSONObject jsonObject=new JSONObject();
|
|
|
+ jsonObject.put("username",userName);
|
|
|
+ jsonObject.put("password",passWd);
|
|
|
+ jsonObject.put("tKey",timeStamp);
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据模版信息查询模版内容
|
|
|
+ * @param userName
|
|
|
+ * @param passWord
|
|
|
+ * @param tempId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static JSONObject queryMessageTemp(String userName,String passWord,Long tempId){
|
|
|
+ JSONObject jsonObject=getCommon(userName,passWord);
|
|
|
+ jsonObject.put("temId",tempId);
|
|
|
+ String data=HttpUtils.sendPost(TEMPLATE_QUERY_URL,jsonObject.toJSONString(),JSON_TYPE);
|
|
|
+ return JSONObject.parseObject(data);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建模版
|
|
|
+ * @param tempName
|
|
|
+ * @param tempContent
|
|
|
+ * @param variableList
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static JSONObject createMessageTemp(String userName,String passWord,String tempName, String tempContent, List<IotTextMessageVo> variableList){
|
|
|
+ JSONObject jsonObject=getCommon(userName,passWord);
|
|
|
+ jsonObject.put("temName",tempName);
|
|
|
+ jsonObject.put("temType",2);
|
|
|
+ JSONObject paramJson=new JSONObject();
|
|
|
+ List<Object> textList=new ArrayList<>();
|
|
|
+ variableList.forEach(variable->{
|
|
|
+ paramJson.put(variable.getVariable(),"others");
|
|
|
+ if(variable.getIsUrl()){
|
|
|
+ String url=DOMAIN_URL+"{"+variable.getVariable()+"}";
|
|
|
+ textList.add(url);
|
|
|
+ }else{
|
|
|
+ textList.add(variable.getVariable());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ tempContent=StringAssembler.replacePlaceholders(tempContent,textList,DOMAIN_URL);
|
|
|
+ jsonObject.put("temContent",tempContent);
|
|
|
+ jsonObject.put("paramJson",paramJson);
|
|
|
+ jsonObject.put("remark",tempName);
|
|
|
+ String data=HttpUtils.sendPost(TEMPLATE_URL,jsonObject.toJSONString(),JSON_TYPE);
|
|
|
+ return JSONObject.parseObject(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取模版真实内容
|
|
|
+ * @param tempContent
|
|
|
+ * @param variableList
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getTemplateContent(String userName,String passWord,String tempContent,List<IotTextMessageVo> variableList){
|
|
|
+ variableList.forEach(variable->{
|
|
|
+ if(variable.getIsUrl()){
|
|
|
+ String shortUrl= convertUrl(userName,passWord,variable.getValue());
|
|
|
+ variable.setValue(shortUrl);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ tempContent=StringAssembler.replacePlaceholders(tempContent,variableList);
|
|
|
+ return tempContent;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param userName
|
|
|
+ * @param passWord
|
|
|
+ * @param tempContent
|
|
|
+ * @param variableStrList
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getSimpleTemplateContent(String userName,String passWord,String tempContent,List<String> variableStrList){
|
|
|
+ List<IotTextMessageVo> variableList= getCommonList(tempContent,variableStrList);
|
|
|
+ variableList.forEach(variable->{
|
|
|
+ if(variable.getIsUrl()){
|
|
|
+ String shortUrl= convertUrl(userName,passWord,variable.getValue());
|
|
|
+ variable.setValue(shortUrl);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ tempContent=StringAssembler.replacePlaceholders(tempContent,variableList);
|
|
|
+ return tempContent;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通用的方法转换
|
|
|
+ * @param tempContent
|
|
|
+ * @param variableStrList
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static List<IotTextMessageVo> getCommonList(String tempContent, List<String> variableStrList){
|
|
|
+ List<IotTextMessageVo> variableList=new ArrayList<>();
|
|
|
+ List<String> tempList=StringAssembler.extractPlaceholders(tempContent);
|
|
|
+ System.out.println("tempList:"+tempList);
|
|
|
+ for(int i=0;i<variableStrList.size();i++){
|
|
|
+ IotTextMessageVo messageVo=new IotTextMessageVo();
|
|
|
+ String value=variableStrList.get(i);
|
|
|
+ if(value.contains("http") || value.contains("https")){
|
|
|
+ messageVo.setIsUrl(Boolean.TRUE);
|
|
|
+ }else{
|
|
|
+ messageVo.setIsUrl(Boolean.FALSE);
|
|
|
+ }
|
|
|
+ messageVo.setVariable(tempList.get(i));
|
|
|
+ messageVo.setValue(value);
|
|
|
+ variableList.add(messageVo);
|
|
|
+ }
|
|
|
+ return variableList;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 发送模版消息
|
|
|
+ * @param tpId
|
|
|
+ * @param phoneList
|
|
|
+ * @param variableList
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static JSONObject sendMessageTemp(String userName,String passWord,String signature,String tpId,List<String> phoneList,List<IotTextMessageVo> variableList){
|
|
|
+ JSONObject jsonObject=getCommon(userName,passWord);
|
|
|
+ jsonObject.put("tpId",tpId);
|
|
|
+ jsonObject.put("signature",signature);
|
|
|
+ jsonObject.put("ext","");
|
|
|
+ jsonObject.put("extend","");
|
|
|
+ JSONArray jsonArray=new JSONArray();
|
|
|
+ variableList.forEach(variable->{
|
|
|
+ if(variable.getIsUrl()){
|
|
|
+ variable.setValue(convertUrl(userName,passWord,variable.getValue()));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ phoneList.forEach(phone->{
|
|
|
+ StringBuilder stringBuilder=new StringBuilder("{");
|
|
|
+ JSONObject phoneObject=new JSONObject();
|
|
|
+ phoneObject.put("mobile",phone);
|
|
|
+ variableList.forEach(variable->{
|
|
|
+ String content="\""+variable.getVariable()+"\""+":"+"\""+variable.getValue()+"\"";
|
|
|
+ stringBuilder.append(content).append(",");
|
|
|
+ });
|
|
|
+ stringBuilder.append("}");
|
|
|
+ String strBuilder=stringBuilder.toString();
|
|
|
+ if(strBuilder.length()>2){
|
|
|
+ strBuilder=strBuilder.replace(",}","}");
|
|
|
+ }
|
|
|
+ JSONObject jsonBuilder=JSONObject.parseObject(strBuilder);
|
|
|
+ phoneObject.put("tpContent",jsonBuilder);
|
|
|
+ jsonArray.add(phoneObject);
|
|
|
+ });
|
|
|
+ jsonObject.put("records",jsonArray);
|
|
|
+ String data=HttpUtils.sendPost(SEND_TEMPLATE_URL,jsonObject.toJSONString(),JSON_TYPE);
|
|
|
+ return JSONObject.parseObject(data);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 发送模版消息
|
|
|
+ * @param tpId
|
|
|
+ * @param phoneList
|
|
|
+ * @param variableStrList
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static JSONObject sendSimpleMessageTemp(String userName,String passWord,String signature,String tpId,String tempContent,List<String> phoneList,List<String> variableStrList) {
|
|
|
+ List<IotTextMessageVo> variableList=getCommonList(tempContent,variableStrList);
|
|
|
+ return sendMessageTemp(userName,passWord,signature,tpId,phoneList,variableList);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送语音通知
|
|
|
+ * @param content
|
|
|
+ * @param phoneList
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static JSONObject sendMessageVoice(String userName,String passWord,String content,List<String> phoneList){
|
|
|
+ JSONObject jsonObject=new JSONObject();
|
|
|
+ jsonObject.put("content",content);
|
|
|
+ String phoneStr="";
|
|
|
+ for(String phone:phoneList){
|
|
|
+ phoneStr+=phone+",";
|
|
|
+ }
|
|
|
+ if(phoneStr.length()>0){
|
|
|
+ phoneStr=phoneStr.substring(0,phoneStr.length()-1);
|
|
|
+ }
|
|
|
+ jsonObject.put("phone",phoneStr);
|
|
|
+ HttpResponse reponse = HttpRequest.get(VOICE_URL)
|
|
|
+ .basicAuth(userName,passWord).body(jsonObject.toString())
|
|
|
+ .header("Content-Type", "application/json")
|
|
|
+ .charset("utf-8")
|
|
|
+ .execute();
|
|
|
+ System.out.println(reponse.toString());
|
|
|
+ return JSONObject.parseObject(reponse.body());
|
|
|
+ }
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 获取模版列表(只获取审核通过的模版)
|
|
|
+ * @param page
|
|
|
+ * @param size
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static JSONObject getTemplateList(String userName,String passWord,Integer page,Integer size){
|
|
|
+ JSONObject jsonObject=getCommon(userName,passWord);
|
|
|
+ jsonObject.put("page",page);
|
|
|
+ jsonObject.put("size",size);
|
|
|
+ jsonObject.put("auditResult",2);
|
|
|
+ String data=HttpUtils.sendPost(TEMPLATE_LIST_URL,jsonObject.toJSONString(),JSON_TYPE);
|
|
|
+ return JSONObject.parseObject(data);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取短信余额
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static JSONObject getMessageBalance(String userName,String passWord){
|
|
|
+ HttpResponse reponse = HttpRequest.get(BALANCE_URL)
|
|
|
+ .basicAuth(userName,passWord)
|
|
|
+ .header("Content-Type", "application/json")
|
|
|
+ .charset("utf-8")
|
|
|
+ .execute();
|
|
|
+ return JSONObject.parseObject(reponse.body());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 公共的密码加密方式
|
|
|
+ * @param password
|
|
|
+ * @param timeKey
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static String passwordEncrypt(String password,String timeKey){
|
|
|
+ return MD5Utils.encrypt(MD5Utils.encrypt(password)+timeKey);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ //String tempContent="{school}-{lab}-{labno}发生{sennor}安全预警,请尽快处理!具体情况请查看zt81.cn/{url}";
|
|
|
+
|
|
|
+ String tempContent="{lab}预约审批已经通过详情请查看 https://lab.zjznai.com/{url}";
|
|
|
+ String userName="jszjznkeyxgs";
|
|
|
+ String passWord="Qwerqaz2024!@#$";
|
|
|
+ String signature="【忠江实验室安全管理】";
|
|
|
+ String tipId="137664";
|
|
|
+ List<String> phoneList=new ArrayList<>();
|
|
|
+ phoneList.add("18681887295");
|
|
|
+ List<IotTextMessageVo> iotTextMessageVoList=new ArrayList<>();
|
|
|
+ IotTextMessageVo iotTextMessageVo=new IotTextMessageVo();
|
|
|
+ iotTextMessageVo.setVariable("school");
|
|
|
+ iotTextMessageVo.setValue("交大");
|
|
|
+ iotTextMessageVo.setIsUrl(false);
|
|
|
+ IotTextMessageVo iotTextMessageVo1=new IotTextMessageVo();
|
|
|
+ iotTextMessageVo1.setVariable("lab");
|
|
|
+ iotTextMessageVo1.setValue("c111大气污染实验室");
|
|
|
+ iotTextMessageVo1.setIsUrl(false);
|
|
|
+ IotTextMessageVo iotTextMessageVo2=new IotTextMessageVo();
|
|
|
+ iotTextMessageVo2.setVariable("labno");
|
|
|
+ iotTextMessageVo2.setValue("c111-1");
|
|
|
+ iotTextMessageVo2.setIsUrl(false);
|
|
|
+ IotTextMessageVo iotTextMessageVo3=new IotTextMessageVo();
|
|
|
+ iotTextMessageVo3.setVariable("sennor");
|
|
|
+ iotTextMessageVo3.setValue("大气压");
|
|
|
+ iotTextMessageVo3.setIsUrl(false);
|
|
|
+ IotTextMessageVo iotTextMessageVo4=new IotTextMessageVo();
|
|
|
+ iotTextMessageVo4.setVariable("url");
|
|
|
+ iotTextMessageVo4.setValue("http://www.baidu.com/");
|
|
|
+ iotTextMessageVo4.setIsUrl(true);
|
|
|
+ iotTextMessageVoList.add(iotTextMessageVo);
|
|
|
+ iotTextMessageVoList.add(iotTextMessageVo1);
|
|
|
+ iotTextMessageVoList.add(iotTextMessageVo2);
|
|
|
+ iotTextMessageVoList.add(iotTextMessageVo3);
|
|
|
+ iotTextMessageVoList.add(iotTextMessageVo4);
|
|
|
+ //sendMessageTemp(userName,passWord,signature,tipId,phoneList,iotTextMessageVoList);
|
|
|
+ String tempContents="{school}-{lab}-{labno}发生{sennor}安全预警,请尽快处理!具体情况请查看zt81.cn/{url}";
|
|
|
+ //System.out.println(getTemplateContent(userName,passWord,tempContents,iotTextMessageVoList));
|
|
|
+ List<String> variable=new ArrayList<>();
|
|
|
+// variable.add("交大");
|
|
|
+// variable.add("b102化工实验室");
|
|
|
+// variable.add("b102-105263");
|
|
|
+// variable.add("防爆烟雾");
|
|
|
+// variable.add("http://www.baidu.com");
|
|
|
+// System.out.println("variable:"+variable);
|
|
|
+
|
|
|
+ variable.add("b102化工实验室");
|
|
|
+ variable.add("http://www.baidu.com");
|
|
|
+
|
|
|
+ System.out.println(sendSimpleMessageTemp(userName,passWord,signature,tipId,tempContent,phoneList,variable));
|
|
|
+ }
|
|
|
+
|
|
|
+}
|