发现火点时向用户发送微信消息通知。

This commit is contained in:
liuchengqian 2022-09-01 14:37:43 +08:00
parent 45d82fa088
commit 17c34d95f6
3 changed files with 152 additions and 23 deletions

View File

@ -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 + '\'' + '}';
}
}

View File

@ -11,12 +11,12 @@ import com.xkrs.model.vo.GaoDeIgGeocodeVo;
import com.xkrs.service.FirePointService; import com.xkrs.service.FirePointService;
import com.xkrs.service.GlobalConfigService; import com.xkrs.service.GlobalConfigService;
import com.xkrs.sms.SMSHelper; import com.xkrs.sms.SMSHelper;
import com.xkrs.sms.WeChatMessageHelper;
import com.xkrs.utils.*; import com.xkrs.utils.*;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cglib.beans.BeanCopier; import org.springframework.cglib.beans.BeanCopier;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -95,6 +95,9 @@ public class FirePointServiceImpl implements FirePointService {
@Resource @Resource
private SMSHelper smsHelper; private SMSHelper smsHelper;
@Resource
private WeChatMessageHelper weChatMessageHelper;
/** /**
* 添加火点信息 * 添加火点信息
*/ */
@ -362,10 +365,27 @@ public class FirePointServiceImpl implements FirePointService {
} }
private void sendBroadcast(FirePointEntity firePointEntity) { private void sendBroadcast(FirePointEntity firePointEntity) {
//发送微信消息 //发送微信群聊消息
sendWeiXinMessage(firePointEntity); sendWeChatGroupMessage(firePointEntity);
//发送短信消息
sendSMSMessage(firePointEntity); //过滤短信通知收件人
List<SysUserEntity> sysUserList = sysUserDao.findAll();
List<RelRoleAuthorityEntity> adminList = relRoleAuthorityDao.selectByAuthorityId(1);
boolean normalSmsEnable = 1L == globalConfigService.selectGlobalConfigValue(9L);
boolean vipSmsEnable = 1L == globalConfigService.selectGlobalConfigValue(10L);
List<SysUserEntity> smsReceiverList = FirePointCodeUtils.filterSmsReceiver(firePointEntity.getStreetCode(), sysUserList, adminList, normalSmsEnable, vipSmsEnable);
try {
weChatMessageHelper.dispatchWeChatMessage(smsReceiverList, firePointEntity);
} catch (Exception e) {
throw new RuntimeException(e);
}
//对短信通知收件人进行分组
List<AgentOrgEntity> agentOrgList = agentOrgDao.findAll();
Map<String, List<SysUserEntity>> agentOrgNameGroupMap = FirePointCodeUtils.groupSmsReceiver(smsReceiverList, agentOrgList);
//分发短信通知
smsHelper.dispatchSMSMessage(agentOrgNameGroupMap, new String[]{firePointEntity.getFirePointAddress()});
} }
/** /**
@ -373,7 +393,7 @@ public class FirePointServiceImpl implements FirePointService {
* *
* @param firePointEntity * @param firePointEntity
*/ */
private void sendWeiXinMessage(FirePointEntity firePointEntity) { private void sendWeChatGroupMessage(FirePointEntity firePointEntity) {
List<CountyCodeWeiXinEntity> countyCodeWeiXinList = countyCodeWeiXinDao.findAll(); List<CountyCodeWeiXinEntity> countyCodeWeiXinList = countyCodeWeiXinDao.findAll();
if (countyCodeWeiXinList.isEmpty()) { if (countyCodeWeiXinList.isEmpty()) {
return; return;
@ -429,23 +449,6 @@ public class FirePointServiceImpl implements FirePointService {
return content; return content;
} }
/**
* 发送短信消息
*/
private void sendSMSMessage(FirePointEntity firePointEntity) {
//过滤短信通知收件人
List<SysUserEntity> sysUserList = sysUserDao.findAll();
List<RelRoleAuthorityEntity> adminList = relRoleAuthorityDao.selectByAuthorityId(1);
boolean normalSmsEnable = 1L == globalConfigService.selectGlobalConfigValue(9L);
boolean vipSmsEnable = 1L == globalConfigService.selectGlobalConfigValue(10L);
List<SysUserEntity> smsReceiverList = FirePointCodeUtils.filterSmsReceiver(firePointEntity.getStreetCode(), sysUserList, adminList, normalSmsEnable, vipSmsEnable);
//对短信通知收件人进行分组
List<AgentOrgEntity> agentOrgList = agentOrgDao.findAll();
Map<String, List<SysUserEntity>> agentOrgNameGroupMap = FirePointCodeUtils.groupSmsReceiver(smsReceiverList, agentOrgList);
//分发短信通知
smsHelper.dispatchSMSMessage(agentOrgNameGroupMap, new String[]{firePointEntity.getFirePointAddress()});
}
/** /**
* 下载VIP用户火点数据 * 下载VIP用户火点数据
*/ */

View File

@ -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<SysUserEntity> sysUserList, FirePointEntity firePointEntity) throws Exception {
if (sysUserList == null || sysUserList.isEmpty() || firePointEntity == null) {
return;
}
//获取微信硬件设备消息通知组名
final String belongGroupWeChat = globalConfigurationDao.findAll((root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> 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<GlobalConfigurationEntity> weChatConfigurationList = globalConfigurationDao.findAll((root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> 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<KeyValueBean> 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<GlobalConfigurationEntity> configurationList, String key) {
for (GlobalConfigurationEntity configuration : configurationList) {
if (configuration.getKey().equals(key)) {
return configuration;
}
}
return null;
}
}