fire_point/src/main/java/com/xkrs/sms/WeChatMessageHelper.java

90 lines
5.1 KiB
Java

package com.xkrs.sms;
import com.xkrs.dao.GlobalConfigurationDao;
import com.xkrs.model.entity.FirePointEntity;
import com.xkrs.model.entity.GlobalConfigurationEntity;
import com.xkrs.straw.model.entity.SysUserEntity;
import com.xkrs.service.GlobalConfigurationService;
import com.xkrs.straw.model.bean.KeyValueBean;
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;
}
}