From 1a26abd93a8fb058c60df2becb2cb20eaaf16154 Mon Sep 17 00:00:00 2001 From: shanao Date: Mon, 21 Oct 2024 18:26:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=81=E8=A3=85=E6=95=B0=E6=8D=AE=E5=8F=91?= =?UTF-8?q?=E9=80=81=EF=BC=88=E4=B9=9D=E5=85=AC=E5=8F=B8=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E5=8F=91=E9=80=81=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../messages/MessagesController.java | 6 +- .../zcloud/entity/messages/PushRecords.java | 2 + .../datasource/messages/MessagesMapper.java | 2 + .../zcloud/service/messages/PushMessages.java | 17 +++ .../messages/impl/AllMessagesImpl.java | 23 +++ .../messages/impl/InsideMessagesImpl.java | 33 ++++ .../messages/impl/MessagesServiceImpl.java | 62 ++------ .../messages/impl/SmsMessagesImpl.java | 28 ++++ .../java/com/zcloud/util/SendSmsUtil.java | 141 +++++++++++------- .../datasource/messages/MessagesMapper.xml | 31 ++++ 10 files changed, 234 insertions(+), 111 deletions(-) create mode 100644 src/main/java/com/zcloud/service/messages/PushMessages.java create mode 100644 src/main/java/com/zcloud/service/messages/impl/AllMessagesImpl.java create mode 100644 src/main/java/com/zcloud/service/messages/impl/InsideMessagesImpl.java create mode 100644 src/main/java/com/zcloud/service/messages/impl/SmsMessagesImpl.java diff --git a/src/main/java/com/zcloud/controller/messages/MessagesController.java b/src/main/java/com/zcloud/controller/messages/MessagesController.java index 5160a9c6..6d6d20af 100644 --- a/src/main/java/com/zcloud/controller/messages/MessagesController.java +++ b/src/main/java/com/zcloud/controller/messages/MessagesController.java @@ -47,7 +47,7 @@ public class MessagesController { if (body == null || !body.containsKey("key") || !body.containsKey("value")) { throw new RuntimeException("参数为空"); } - PushRecords pushRecords = JSON.parseObject(decrypt(body, privateKey), PushRecords.class); + PushRecords pushRecords = JSON.parseObject(decrypt(body), PushRecords.class); Set> validate = validator.validate(pushRecords); if (!validate.isEmpty()) { throw new ConstraintViolationException(validate); @@ -62,9 +62,9 @@ public class MessagesController { return result; } - private String decrypt(Map body, String privateKey) { + private String decrypt(Map body) { try { - RSA rsa = new RSA(privateKey, null); + RSA rsa = new RSA(MessagesController.privateKey, null); byte[] aesKey = rsa.decrypt(Convert.toStr(body.get("key")), KeyType.PrivateKey); SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, aesKey); return aes.decryptStr(Convert.toStr(body.get("value")), StandardCharsets.UTF_8); diff --git a/src/main/java/com/zcloud/entity/messages/PushRecords.java b/src/main/java/com/zcloud/entity/messages/PushRecords.java index 3ca02e19..350370d2 100644 --- a/src/main/java/com/zcloud/entity/messages/PushRecords.java +++ b/src/main/java/com/zcloud/entity/messages/PushRecords.java @@ -32,4 +32,6 @@ public class PushRecords implements Serializable { @NotBlank(message = "发送时间不能为空") private String sendTime; + + private Integer state; } 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 a3c36138..b61c1877 100644 --- a/src/main/java/com/zcloud/mapper/datasource/messages/MessagesMapper.java +++ b/src/main/java/com/zcloud/mapper/datasource/messages/MessagesMapper.java @@ -7,4 +7,6 @@ public interface MessagesMapper { void install(PushRecords pushRecords); String findUserIdByPhone(@Param("phone") String phone); + + void updateById(PushRecords pushRecords); } diff --git a/src/main/java/com/zcloud/service/messages/PushMessages.java b/src/main/java/com/zcloud/service/messages/PushMessages.java new file mode 100644 index 00000000..bd556197 --- /dev/null +++ b/src/main/java/com/zcloud/service/messages/PushMessages.java @@ -0,0 +1,17 @@ +package com.zcloud.service.messages; + +import com.zcloud.entity.messages.PushRecords; + +/** + * 发送消息接口 + */ +public interface PushMessages { + + /** + * 发送消息 + * + * @param pushRecords 消息实体 + * @param userId 用户id + */ + void push(PushRecords pushRecords, String userId) throws Exception; +} diff --git a/src/main/java/com/zcloud/service/messages/impl/AllMessagesImpl.java b/src/main/java/com/zcloud/service/messages/impl/AllMessagesImpl.java new file mode 100644 index 00000000..3ec8e98e --- /dev/null +++ b/src/main/java/com/zcloud/service/messages/impl/AllMessagesImpl.java @@ -0,0 +1,23 @@ +package com.zcloud.service.messages.impl; + +import com.zcloud.entity.messages.PushRecords; +import com.zcloud.service.messages.PushMessages; +import lombok.AllArgsConstructor; +import org.springframework.stereotype.Component; + +/** + * 发送站内信 + */ +@Component("messages3") +@AllArgsConstructor +public class AllMessagesImpl implements PushMessages { + + private final InsideMessagesImpl insideMessages; + private final SmsMessagesImpl smsMessages; + + @Override + public void push(PushRecords pushRecords, String userId) throws Exception { + insideMessages.push(pushRecords, userId); + smsMessages.push(pushRecords, userId); + } +} diff --git a/src/main/java/com/zcloud/service/messages/impl/InsideMessagesImpl.java b/src/main/java/com/zcloud/service/messages/impl/InsideMessagesImpl.java new file mode 100644 index 00000000..cf3ae852 --- /dev/null +++ b/src/main/java/com/zcloud/service/messages/impl/InsideMessagesImpl.java @@ -0,0 +1,33 @@ +package com.zcloud.service.messages.impl; + +import com.zcloud.entity.PageData; +import com.zcloud.entity.messages.PushRecords; +import com.zcloud.service.bus.NoticeCorpService; +import com.zcloud.service.messages.PushMessages; +import lombok.AllArgsConstructor; +import org.springframework.stereotype.Component; + +/** + * 发送站内信 + */ +@Component("messages2") +@AllArgsConstructor +public class InsideMessagesImpl implements PushMessages { + + private final NoticeCorpService noticeCorpService; + + @Override + public void push(PushRecords pushRecords, String userId) throws Exception { + PageData mes = new PageData(); + mes.put("BIANMA", "PUSH_RECORDS"); + 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()); + mes.put("CONTENT", content);// 站内信内容 + noticeCorpService.sendNotice(mes); + } +} diff --git a/src/main/java/com/zcloud/service/messages/impl/MessagesServiceImpl.java b/src/main/java/com/zcloud/service/messages/impl/MessagesServiceImpl.java index 72af1bba..0395d214 100644 --- a/src/main/java/com/zcloud/service/messages/impl/MessagesServiceImpl.java +++ b/src/main/java/com/zcloud/service/messages/impl/MessagesServiceImpl.java @@ -1,11 +1,10 @@ package com.zcloud.service.messages.impl; import cn.hutool.core.util.StrUtil; -import com.zcloud.entity.PageData; import com.zcloud.entity.messages.PushRecords; import com.zcloud.mapper.datasource.messages.MessagesMapper; -import com.zcloud.service.bus.NoticeCorpService; import com.zcloud.service.messages.MessagesService; +import com.zcloud.service.messages.PushMessages; import com.zcloud.util.DateUtil; import com.zcloud.util.UuidUtil; import lombok.RequiredArgsConstructor; @@ -13,13 +12,16 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.Map; + @Slf4j @Service @RequiredArgsConstructor public class MessagesServiceImpl implements MessagesService { private final MessagesMapper mapper; - private final NoticeCorpService noticeCorpService; + private final Map messages; + @Override @Transactional(rollbackFor = Exception.class) @@ -27,59 +29,19 @@ public class MessagesServiceImpl implements MessagesService { // 先记录数据 pushRecords.setId(UuidUtil.get32UUID()); pushRecords.setSendTime(DateUtil.getTime()); + pushRecords.setState(1); mapper.install(pushRecords); // 根据手机号查找用户id String userId = mapper.findUserIdByPhone(pushRecords.getPhone()); if (StrUtil.isEmpty(userId)) { throw new RuntimeException("该用户不存在"); } + // 调用对应消息推送实现 Integer type = pushRecords.getMessageType(); - // 1-短信 2-平台信息 3-全发 - if (type == 1) { - pushSms(pushRecords); - } else if (type == 2) { - PageData data = pushInfo(pushRecords, userId); - noticeCorpService.sendNotice(data); - } else if (type == 3) { - pushSms(pushRecords); - PageData data = pushInfo(pushRecords, userId); - noticeCorpService.sendNotice(data); - } else { - throw new RuntimeException("消息类型:【" + type + "】错误"); - } - } - - /** - * 推送平台信息(站内信) - * 注意 不能再此方法中调用数据库 再类中直接调用 会导致事务失效 private 也会导致事务失效 - * - * @param pushRecords 消息 - * @param userId 接收人id - */ - private PageData pushInfo(PushRecords pushRecords, String userId) throws Exception { - PageData mes = new PageData(); - mes.put("BIANMA", "PUSH_RECORDS"); - mes.put("SENDER_ID", "九公司应急管理"); // 发送人员ID - mes.put("SENDER_NAME", "九公司应急管理"); // 发送人员姓名 - mes.put("SYNOPSIS", pushRecords.getMessageTitle()); // 站内信标题 - //mes.put("WORKURL", "/pages/application/high-risk-work/limited-space/gas-analysis/list?NameLikes=" + pd.getString("WORK_PERMIT_NUMBER")); // 操作链接 - mes.put("CORPINFO_ID", "48a8ca9815814c979814ddcf041c5cd5");// 企业id - mes.put("RECEIVER_ID", userId); // 接收人员ID - PageData content = new PageData(); - content.put("msg", pushRecords.getMessageContent());// 作业编号 - mes.put("CONTENT", content);// 站内信内容 - return mes; - } - - /** - * 推送平台信息(站内信) - * 注意 不能再此方法中调用数据库 再类中直接调用 会导致事务失效 private 也会导致事务失效 - * - * @param pushRecords 消息 - */ - private void pushSms(PushRecords pushRecords) { - // todo - log.error("===================>推送短信信息"); - log.error("消息内容:{}", pushRecords); + PushMessages pushMessages = messages.get("messages" + type); + pushMessages.push(pushRecords, userId); + // 修改状态 + pushRecords.setState(2); + mapper.updateById(pushRecords); } } diff --git a/src/main/java/com/zcloud/service/messages/impl/SmsMessagesImpl.java b/src/main/java/com/zcloud/service/messages/impl/SmsMessagesImpl.java new file mode 100644 index 00000000..fe5e4aea --- /dev/null +++ b/src/main/java/com/zcloud/service/messages/impl/SmsMessagesImpl.java @@ -0,0 +1,28 @@ +package com.zcloud.service.messages.impl; + +import com.alibaba.fastjson.JSONObject; +import com.zcloud.entity.messages.PushRecords; +import com.zcloud.service.messages.PushMessages; +import com.zcloud.util.SendSmsUtil; +import lombok.AllArgsConstructor; +import org.springframework.stereotype.Component; + +import java.text.ParseException; + +/** + * 发送短信 + */ +@Component("messages1") +@AllArgsConstructor +public class SmsMessagesImpl implements PushMessages { + + @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())) { + throw new RuntimeException("系统故障:短信发送失败"); + } + } +} diff --git a/src/main/java/com/zcloud/util/SendSmsUtil.java b/src/main/java/com/zcloud/util/SendSmsUtil.java index efaddd2c..c7442f49 100644 --- a/src/main/java/com/zcloud/util/SendSmsUtil.java +++ b/src/main/java/com/zcloud/util/SendSmsUtil.java @@ -1,8 +1,12 @@ package com.zcloud.util; +import cn.hutool.core.date.DateUtil; import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.StringUtils; import org.springframework.http.MediaType; @@ -14,6 +18,7 @@ import java.util.Date; /** * 重点工程发送短信工具类 */ +@Slf4j public class SendSmsUtil { private static String USERNAME = "qhdzyhy"; @@ -27,43 +32,44 @@ public class SendSmsUtil { /** * 发送短信 + * * @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" - * } - * } - * ] + * @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 { + 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); + 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("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); + json.put("records", records); System.out.println(json.toJSONString()); String result = HttpRequest.post(URL) .timeout(60000) @@ -71,45 +77,64 @@ 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 { + 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; + } + } + /** * 封装要发送的电话以及要替换的内容 - * @param mobile 手机号 + * + * @param mobile 手机号 * @param tpContent 替换内容 - * @return - * 格式实例 var1表示要替换的内容 与要替换的内容保持一致即可 变量1表示替换的内容 - * { - * "mobile":"138****0000", - * "tpContent":{ - * "var1":"变量1", - * "var2":"变量2" - * } - * } + * @return 格式实例 var1表示要替换的内容 与要替换的内容保持一致即可 变量1表示替换的内容 + * { + * "mobile":"138****0000", + * "tpContent":{ + * "var1":"变量1", + * "var2":"变量2" + * } + * } */ - public static JSONObject getRecords(String mobile, JSONObject tpContent ){ + 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); + 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); - } +// 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()); +// } } diff --git a/src/main/resources/mybatis/datasource/messages/MessagesMapper.xml b/src/main/resources/mybatis/datasource/messages/MessagesMapper.xml index 56408442..58ce6b9e 100644 --- a/src/main/resources/mybatis/datasource/messages/MessagesMapper.xml +++ b/src/main/resources/mybatis/datasource/messages/MessagesMapper.xml @@ -23,6 +23,9 @@ SEND_TIME, + + STATE, + values @@ -44,10 +47,38 @@ #{sendTime}, + + #{state}, + + + + update push_records + + + PHONE = #{phone}, + + + MESSAGE_TITLE = #{messageTitle}, + + + MESSAGE_CONTENT = #{messageContent}, + + + MESSAGE_TYPE = #{messageType}, + + + SEND_TIME = #{sendTime}, + + + STATE = #{state}, + + + where ID = #{id} + \ No newline at end of file