From 73500df19d954985f003526cd1e338de019726ab Mon Sep 17 00:00:00 2001 From: shanao Date: Thu, 24 Oct 2024 18:23:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=9F=AD=E4=BF=A1/=E7=AB=99?= =?UTF-8?q?=E5=86=85=E4=BF=A1=E6=8E=A8=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AppSafetyEnvironmentalController.java | 2 +- .../zcloud/entity/messages/PushRecords.java | 22 +++++- .../datasource/messages/MessagesMapper.java | 1 + .../messages/impl/InsideMessagesImpl.java | 4 +- .../messages/impl/SmsMessagesImpl.java | 21 +++-- .../java/com/zcloud/util/SendSmsUtil.java | 78 ++++++++++++++----- .../inspection/SafetyEnvironmentalMapper.xml | 6 ++ .../datasource/messages/MessagesMapper.xml | 9 +++ 8 files changed, 109 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/zcloud/controller/app/inspection/AppSafetyEnvironmentalController.java b/src/main/java/com/zcloud/controller/app/inspection/AppSafetyEnvironmentalController.java index d3b5b997..cc7f4c68 100644 --- a/src/main/java/com/zcloud/controller/app/inspection/AppSafetyEnvironmentalController.java +++ b/src/main/java/com/zcloud/controller/app/inspection/AppSafetyEnvironmentalController.java @@ -446,7 +446,7 @@ public class AppSafetyEnvironmentalController extends BaseController { String KEYWORDS = pd.getString("KEYWORDS"); //关键词检索条件 if (Tools.notEmpty(KEYWORDS)) pd.put("KEYWORDS", KEYWORDS.trim()); pd.put("waffle", pd.getString("ARCHIVE_USER_ID")); - pd.put("loginUserId", Jurisdiction.getUSER_ID()); + pd.put("loginUserId", pd.getString("ARCHIVE_USER_ID")); page.setPd(pd); List varList = safetyenvironmentalService.checkList(page); //列出SafetyEnvironmental列表 /* 针对验收部分特殊处理 */ diff --git a/src/main/java/com/zcloud/entity/messages/PushRecords.java b/src/main/java/com/zcloud/entity/messages/PushRecords.java index 350370d2..0850e23b 100644 --- a/src/main/java/com/zcloud/entity/messages/PushRecords.java +++ b/src/main/java/com/zcloud/entity/messages/PushRecords.java @@ -1,5 +1,7 @@ package com.zcloud.entity.messages; +import cn.hutool.core.collection.CollUtil; +import com.alibaba.fastjson.JSON; import lombok.Data; import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.Range; @@ -7,6 +9,7 @@ import org.hibernate.validator.constraints.Range; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; import java.io.Serializable; +import java.util.Map; @Data public class PushRecords implements Serializable { @@ -22,9 +25,8 @@ public class PushRecords implements Serializable { @Length(max = 255, message = "消息标题长度不能大于255") private String messageTitle; - @NotBlank(message = "消息内容不能为空") - @Length(max = 255, message = "消息内容长度不能大于255") - private String messageContent; + @NotNull(message = "消息内容不能为空") + private Map messageContent; @NotNull(message = "消息类型不能为空") @Range(min = 1, max = 3, message = "消息类型只能是1或2或3") @@ -34,4 +36,18 @@ public class PushRecords implements Serializable { private String sendTime; private Integer state; + + @NotBlank(message = "消息模版id不能为空") + private String templateId; + + public String getMessageContent() { + if (CollUtil.isEmpty(messageContent)) { + return ""; + } + return JSON.toJSONString(messageContent); + } + + public Map getMessageContentMap() { + return messageContent; + } } diff --git a/src/main/java/com/zcloud/mapper/datasource/messages/MessagesMapper.java b/src/main/java/com/zcloud/mapper/datasource/messages/MessagesMapper.java index b61c1877..d271e511 100644 --- a/src/main/java/com/zcloud/mapper/datasource/messages/MessagesMapper.java +++ b/src/main/java/com/zcloud/mapper/datasource/messages/MessagesMapper.java @@ -4,6 +4,7 @@ import com.zcloud.entity.messages.PushRecords; import org.apache.ibatis.annotations.Param; public interface MessagesMapper { + void install(PushRecords pushRecords); String findUserIdByPhone(@Param("phone") String phone); diff --git a/src/main/java/com/zcloud/service/messages/impl/InsideMessagesImpl.java b/src/main/java/com/zcloud/service/messages/impl/InsideMessagesImpl.java index 603e2db8..f7dab12c 100644 --- a/src/main/java/com/zcloud/service/messages/impl/InsideMessagesImpl.java +++ b/src/main/java/com/zcloud/service/messages/impl/InsideMessagesImpl.java @@ -21,14 +21,14 @@ public class InsideMessagesImpl implements PushMessages { @Transactional(rollbackFor = Exception.class) public void push(PushRecords pushRecords, String userId) throws Exception { PageData mes = new PageData(); - mes.put("BIANMA", "PUSH_RECORDS"); + mes.put("BIANMA", pushRecords.getMessageTitle()); mes.put("SENDER_ID", "九公司应急管理"); mes.put("SENDER_NAME", "九公司应急管理"); mes.put("SYNOPSIS", pushRecords.getMessageTitle()); mes.put("CORPINFO_ID", "48a8ca9815814c979814ddcf041c5cd5"); mes.put("RECEIVER_ID", userId); PageData content = new PageData(); - content.put("msg", pushRecords.getMessageContent()); + content.putAll(pushRecords.getMessageContentMap()); mes.put("CONTENT", content);// 站内信内容 noticeCorpService.sendNotice(mes); } diff --git a/src/main/java/com/zcloud/service/messages/impl/SmsMessagesImpl.java b/src/main/java/com/zcloud/service/messages/impl/SmsMessagesImpl.java index fe5e4aea..4936e717 100644 --- a/src/main/java/com/zcloud/service/messages/impl/SmsMessagesImpl.java +++ b/src/main/java/com/zcloud/service/messages/impl/SmsMessagesImpl.java @@ -1,27 +1,34 @@ package com.zcloud.service.messages.impl; -import com.alibaba.fastjson.JSONObject; +import cn.hutool.core.collection.CollUtil; +import com.zcloud.entity.PageData; import com.zcloud.entity.messages.PushRecords; +import com.zcloud.service.bus.NoticeTemplateService; import com.zcloud.service.messages.PushMessages; import com.zcloud.util.SendSmsUtil; -import lombok.AllArgsConstructor; +import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; import java.text.ParseException; +import java.util.Map; /** * 发送短信 */ @Component("messages1") -@AllArgsConstructor +@RequiredArgsConstructor public class SmsMessagesImpl implements PushMessages { + private final NoticeTemplateService noticeTemplateService; + @Override public void push(PushRecords pushRecords, String userId) throws ParseException { - JSONObject object = new JSONObject(); - object.put("messageContent", pushRecords.getMessageContent()); - // todo 应该是短信模版tpId 没有 别的OK了 - if (!SendSmsUtil.sendSms(null, object, pushRecords.getSendTime(), pushRecords.getPhone())) { + PageData pageData = noticeTemplateService.findByBianMa(pushRecords.getTemplateId()); + if (CollUtil.isEmpty(pageData)) { + throw new RuntimeException("模板未配置"); + } + Map content = pushRecords.getMessageContentMap(); + if (!SendSmsUtil.customizeSMSSending(pushRecords.getPhone(), pageData.getString("CONTENT"), content)) { throw new RuntimeException("系统故障:短信发送失败"); } } diff --git a/src/main/java/com/zcloud/util/SendSmsUtil.java b/src/main/java/com/zcloud/util/SendSmsUtil.java index c7442f49..9bc40ee8 100644 --- a/src/main/java/com/zcloud/util/SendSmsUtil.java +++ b/src/main/java/com/zcloud/util/SendSmsUtil.java @@ -1,6 +1,9 @@ package com.zcloud.util; +import cn.hutool.core.convert.Convert; +import cn.hutool.core.date.DatePattern; import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.StrUtil; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import com.alibaba.fastjson.JSON; @@ -14,6 +17,8 @@ import org.springframework.http.MediaType; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.HashMap; +import java.util.Map; /** * 重点工程发送短信工具类 @@ -77,30 +82,29 @@ public class SendSmsUtil { System.out.println(result); } - // {"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 { + public static boolean customizeSMSSending(String mobile, String template, Map 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"))); - 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); + 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()) { - return JSON.parseObject(response.body()).getInteger("code") == 200; + body = response.body(); } catch (Exception e) { - log.error("发送短信异常", e); + log.error("短信发送异常", e); return false; } + JSONObject object = JSON.parseObject(body); + return object == null || object.getInteger("code") == 200; } @@ -129,12 +133,44 @@ public class SendSmsUtil { }//d22f8a5d4a35b3761dc9525186e652a0 //测试接口 -// public static void main(String[] args) throws ParseException { + public static void main(String[] args) throws ParseException { + //定义替换参数 + Map 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(); -// tpContent.put("HIDDENDESCR", "卓云企业测试"); -// JSONObject object = sendSms("121487", tpContent, DateUtil.formatDateTime(new Date()), "18630387571"); -// System.out.println(object.toJSONString()); -// } +// tpContent.put("OUTSOURCEDNAME","测试"); +// records = SendSmsUtil.getRecords("18630387571",tpContent); +// jsonArray.add(records); +// SendSmsUtil.sendSms("121451",jsonArray,null); + } } diff --git a/src/main/resources/mybatis/datasource/inspection/SafetyEnvironmentalMapper.xml b/src/main/resources/mybatis/datasource/inspection/SafetyEnvironmentalMapper.xml index 18655387..aab394e8 100644 --- a/src/main/resources/mybatis/datasource/inspection/SafetyEnvironmentalMapper.xml +++ b/src/main/resources/mybatis/datasource/inspection/SafetyEnvironmentalMapper.xml @@ -311,6 +311,12 @@ and biss.SITUATION LIKE CONCAT('%', #{pd.INSPECTION_CASE},'%') + + and siu.NAME LIKE CONCAT('%', #{pd.INSPECTION_USER_NAME},'%') + + + and siu.PHONE LIKE CONCAT('%', #{pd.INSPECTION_USER_PHONE},'%') + and f.INSPECTION_TYPE = #{pd.INSPECTION_TYPE} diff --git a/src/main/resources/mybatis/datasource/messages/MessagesMapper.xml b/src/main/resources/mybatis/datasource/messages/MessagesMapper.xml index 58ce6b9e..abd492c9 100644 --- a/src/main/resources/mybatis/datasource/messages/MessagesMapper.xml +++ b/src/main/resources/mybatis/datasource/messages/MessagesMapper.xml @@ -26,6 +26,9 @@ STATE, + + TEMPLATE_ID, + values @@ -50,6 +53,9 @@ #{state}, + + #{templateId}, + @@ -78,6 +84,9 @@ STATE = #{state}, + + TEMPLATE_ID = #{templateId}, + where ID = #{id}