From 17c34d95f614a19a989b41a403dee3680059a27b Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Thu, 1 Sep 2022 14:37:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E7=8E=B0=E7=81=AB=E7=82=B9=E6=97=B6?= =?UTF-8?q?=E5=90=91=E7=94=A8=E6=88=B7=E5=8F=91=E9=80=81=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E9=80=9A=E7=9F=A5=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xkrs/model/bean/KeyValueBean.java | 37 ++++++++ .../service/impl/FirePointServiceImpl.java | 49 +++++----- .../com/xkrs/sms/WeChatMessageHelper.java | 89 +++++++++++++++++++ 3 files changed, 152 insertions(+), 23 deletions(-) create mode 100644 src/main/java/com/xkrs/model/bean/KeyValueBean.java create mode 100644 src/main/java/com/xkrs/sms/WeChatMessageHelper.java diff --git a/src/main/java/com/xkrs/model/bean/KeyValueBean.java b/src/main/java/com/xkrs/model/bean/KeyValueBean.java new file mode 100644 index 0000000..3380758 --- /dev/null +++ b/src/main/java/com/xkrs/model/bean/KeyValueBean.java @@ -0,0 +1,37 @@ +package com.xkrs.model.bean; + +public class KeyValueBean { + + private String key; + + private String value; + + public KeyValueBean() { + } + + public KeyValueBean(String key, String value) { + this.key = key; + this.value = value; + } + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + @Override + public String toString() { + return "KeyValueBean{" + "key='" + key + '\'' + ", value='" + value + '\'' + '}'; + } +} diff --git a/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java b/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java index f9a4bc3..0231da0 100644 --- a/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java @@ -11,12 +11,12 @@ import com.xkrs.model.vo.GaoDeIgGeocodeVo; import com.xkrs.service.FirePointService; import com.xkrs.service.GlobalConfigService; import com.xkrs.sms.SMSHelper; +import com.xkrs.sms.WeChatMessageHelper; import com.xkrs.utils.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; -import org.springframework.cache.annotation.Cacheable; import org.springframework.cglib.beans.BeanCopier; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.stereotype.Service; @@ -95,6 +95,9 @@ public class FirePointServiceImpl implements FirePointService { @Resource private SMSHelper smsHelper; + @Resource + private WeChatMessageHelper weChatMessageHelper; + /** * 添加火点信息 */ @@ -362,10 +365,27 @@ public class FirePointServiceImpl implements FirePointService { } private void sendBroadcast(FirePointEntity firePointEntity) { - //发送微信消息 - sendWeiXinMessage(firePointEntity); - //发送短信消息 - sendSMSMessage(firePointEntity); + //发送微信群聊消息 + sendWeChatGroupMessage(firePointEntity); + + //过滤短信通知收件人 + List sysUserList = sysUserDao.findAll(); + List adminList = relRoleAuthorityDao.selectByAuthorityId(1); + boolean normalSmsEnable = 1L == globalConfigService.selectGlobalConfigValue(9L); + boolean vipSmsEnable = 1L == globalConfigService.selectGlobalConfigValue(10L); + List smsReceiverList = FirePointCodeUtils.filterSmsReceiver(firePointEntity.getStreetCode(), sysUserList, adminList, normalSmsEnable, vipSmsEnable); + + try { + weChatMessageHelper.dispatchWeChatMessage(smsReceiverList, firePointEntity); + } catch (Exception e) { + throw new RuntimeException(e); + } + + //对短信通知收件人进行分组 + List agentOrgList = agentOrgDao.findAll(); + Map> agentOrgNameGroupMap = FirePointCodeUtils.groupSmsReceiver(smsReceiverList, agentOrgList); + //分发短信通知 + smsHelper.dispatchSMSMessage(agentOrgNameGroupMap, new String[]{firePointEntity.getFirePointAddress()}); } /** @@ -373,7 +393,7 @@ public class FirePointServiceImpl implements FirePointService { * * @param firePointEntity */ - private void sendWeiXinMessage(FirePointEntity firePointEntity) { + private void sendWeChatGroupMessage(FirePointEntity firePointEntity) { List countyCodeWeiXinList = countyCodeWeiXinDao.findAll(); if (countyCodeWeiXinList.isEmpty()) { return; @@ -429,23 +449,6 @@ public class FirePointServiceImpl implements FirePointService { return content; } - /** - * 发送短信消息 - */ - private void sendSMSMessage(FirePointEntity firePointEntity) { - //过滤短信通知收件人 - List sysUserList = sysUserDao.findAll(); - List adminList = relRoleAuthorityDao.selectByAuthorityId(1); - boolean normalSmsEnable = 1L == globalConfigService.selectGlobalConfigValue(9L); - boolean vipSmsEnable = 1L == globalConfigService.selectGlobalConfigValue(10L); - List smsReceiverList = FirePointCodeUtils.filterSmsReceiver(firePointEntity.getStreetCode(), sysUserList, adminList, normalSmsEnable, vipSmsEnable); - //对短信通知收件人进行分组 - List agentOrgList = agentOrgDao.findAll(); - Map> agentOrgNameGroupMap = FirePointCodeUtils.groupSmsReceiver(smsReceiverList, agentOrgList); - //分发短信通知 - smsHelper.dispatchSMSMessage(agentOrgNameGroupMap, new String[]{firePointEntity.getFirePointAddress()}); - } - /** * 下载VIP用户火点数据 */ diff --git a/src/main/java/com/xkrs/sms/WeChatMessageHelper.java b/src/main/java/com/xkrs/sms/WeChatMessageHelper.java new file mode 100644 index 0000000..5194167 --- /dev/null +++ b/src/main/java/com/xkrs/sms/WeChatMessageHelper.java @@ -0,0 +1,89 @@ +package com.xkrs.sms; + +import com.xkrs.dao.GlobalConfigurationDao; +import com.xkrs.model.bean.KeyValueBean; +import com.xkrs.model.entity.FirePointEntity; +import com.xkrs.model.entity.GlobalConfigurationEntity; +import com.xkrs.model.entity.SysUserEntity; +import com.xkrs.service.GlobalConfigurationService; +import com.xkrs.utils.HttpClientUtils; +import org.springframework.data.domain.Sort; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import javax.persistence.criteria.Predicate; +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +@Component +public class WeChatMessageHelper { + + @Resource + private GlobalConfigurationDao globalConfigurationDao; + + @Resource + private GlobalConfigurationService globalConfigurationService; + + public WeChatMessageHelper() { + } + + public void dispatchWeChatMessage(List sysUserList, FirePointEntity firePointEntity) throws Exception { + + if (sysUserList == null || sysUserList.isEmpty() || firePointEntity == null) { + return; + } + //获取微信硬件设备消息通知组名 + final String belongGroupWeChat = globalConfigurationDao.findAll((root, criteriaQuery, criteriaBuilder) -> { + List predicateList = new ArrayList<>(); + predicateList.add(criteriaBuilder.equal(root.get("belongGroup").as(String.class), "root")); + predicateList.add(criteriaBuilder.equal(root.get("key").as(String.class), "weixin_hardware_device_message_key")); + Predicate[] predicateArray = new Predicate[predicateList.size()]; + return criteriaBuilder.and(predicateList.toArray(predicateArray)); + }, Sort.by(Sort.Direction.ASC, "id")).get(0).getValue(); + //获取微信硬件设备消息通知参数实体集合 + List weChatConfigurationList = globalConfigurationDao.findAll((root, criteriaQuery, criteriaBuilder) -> { + List predicateList = new ArrayList<>(); + predicateList.add(criteriaBuilder.equal(root.get("belongGroup").as(String.class), belongGroupWeChat)); + Predicate[] predicateArray = new Predicate[predicateList.size()]; + return criteriaBuilder.and(predicateList.toArray(predicateArray)); + }, Sort.by(Sort.Direction.ASC, "id")); + //获取微信硬件设备消息通知参数 + String grantType = Objects.requireNonNull(obtainValueByKey(weChatConfigurationList, "grant_type")).getValue(); + String appId = Objects.requireNonNull(obtainValueByKey(weChatConfigurationList, "appid")).getValue(); + String secret = Objects.requireNonNull(obtainValueByKey(weChatConfigurationList, "secret")).getValue(); + String to_openid_list = Objects.requireNonNull(obtainValueByKey(weChatConfigurationList, "to_openid_list")).getValue(); + String template_id = Objects.requireNonNull(obtainValueByKey(weChatConfigurationList, "template_id")).getValue(); + String sn = Objects.requireNonNull(obtainValueByKey(weChatConfigurationList, "sn")).getValue(); + String model_id = Objects.requireNonNull(obtainValueByKey(weChatConfigurationList, "model_id")).getValue(); + String page = Objects.requireNonNull(obtainValueByKey(weChatConfigurationList, "page")).getValue(); + String miniprogram_state = Objects.requireNonNull(obtainValueByKey(weChatConfigurationList, "miniprogram_state")).getValue(); + String lang = Objects.requireNonNull(obtainValueByKey(weChatConfigurationList, "lang")).getValue(); + String template_param_count = Objects.requireNonNull(obtainValueByKey(weChatConfigurationList, "template_param_count")).getValue(); + //获取微信硬件设备消息通知参数模板参数 + List templateParamList = new ArrayList<>(); + DecimalFormat decimalFormat = new DecimalFormat("00"); + for (int i = 1; i <= Long.parseLong(template_param_count); i++) { + String formatParamIndex = decimalFormat.format(i); + GlobalConfigurationEntity templateParamKey = obtainValueByKey(weChatConfigurationList, "template_param_key_" + formatParamIndex); + GlobalConfigurationEntity templateParamValue = obtainValueByKey(weChatConfigurationList, "template_param_value_" + formatParamIndex); + templateParamList.add(new KeyValueBean(Objects.requireNonNull(templateParamKey).getValue(), Objects.requireNonNull(templateParamValue).getValue())); + } + //获取AccessToken + String getAccessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token" + "?grant_type=" + grantType + "&appid=" + appId + "&secret=" + secret; + String result = HttpClientUtils.sendHttpsGet(getAccessTokenUrl); + System.out.println(result); + + } + + private GlobalConfigurationEntity obtainValueByKey(List configurationList, String key) { + for (GlobalConfigurationEntity configuration : configurationList) { + if (configuration.getKey().equals(key)) { + return configuration; + } + } + return null; + } + +}