设计消息推送数据表、开发消息推送相关逻辑方法。
This commit is contained in:
+105
@@ -0,0 +1,105 @@
|
||||
package com.ruoyi.rocketmq.controller;
|
||||
|
||||
import java.util.List;
|
||||
import java.io.IOException;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import com.ruoyi.common.log.annotation.Log;
|
||||
import com.ruoyi.common.log.enums.BusinessType;
|
||||
import com.ruoyi.common.security.annotation.RequiresPermissions;
|
||||
import com.ruoyi.rocketmq.domain.RmAlarmPushConfig;
|
||||
import com.ruoyi.rocketmq.service.IRmAlarmPushConfigService;
|
||||
import com.ruoyi.common.core.web.controller.BaseController;
|
||||
import com.ruoyi.common.core.web.domain.AjaxResult;
|
||||
import com.ruoyi.common.core.utils.poi.ExcelUtil;
|
||||
import com.ruoyi.common.core.web.page.TableDataInfo;
|
||||
|
||||
/**
|
||||
* 告警推送配置Controller
|
||||
*
|
||||
* @author gyt
|
||||
* @date 2025-11-11
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/alarmPushConfig")
|
||||
public class RmAlarmPushConfigController extends BaseController
|
||||
{
|
||||
@Autowired
|
||||
private IRmAlarmPushConfigService rmAlarmPushConfigService;
|
||||
|
||||
/**
|
||||
* 查询告警推送配置列表
|
||||
*/
|
||||
@RequiresPermissions("rocketmq:alarmPushConfig:list")
|
||||
@GetMapping("/list")
|
||||
public TableDataInfo list(RmAlarmPushConfig rmAlarmPushConfig)
|
||||
{
|
||||
startPage();
|
||||
List<RmAlarmPushConfig> list = rmAlarmPushConfigService.selectRmAlarmPushConfigList(rmAlarmPushConfig);
|
||||
return getDataTable(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出告警推送配置列表
|
||||
*/
|
||||
@RequiresPermissions("rocketmq:alarmPushConfig:export")
|
||||
@Log(title = "告警推送配置", businessType = BusinessType.EXPORT)
|
||||
@PostMapping("/export")
|
||||
public void export(HttpServletResponse response, RmAlarmPushConfig rmAlarmPushConfig)
|
||||
{
|
||||
List<RmAlarmPushConfig> list = rmAlarmPushConfigService.selectRmAlarmPushConfigList(rmAlarmPushConfig);
|
||||
ExcelUtil<RmAlarmPushConfig> util = new ExcelUtil<RmAlarmPushConfig>(RmAlarmPushConfig.class);
|
||||
util.exportExcel(response, list, "告警推送配置数据");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取告警推送配置详细信息
|
||||
*/
|
||||
@RequiresPermissions("rocketmq:alarmPushConfig:query")
|
||||
@GetMapping(value = "/{id}")
|
||||
public AjaxResult getInfo(@PathVariable("id") Long id)
|
||||
{
|
||||
return success(rmAlarmPushConfigService.selectRmAlarmPushConfigById(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增告警推送配置
|
||||
*/
|
||||
@RequiresPermissions("rocketmq:alarmPushConfig:add")
|
||||
@Log(title = "告警推送配置", businessType = BusinessType.INSERT)
|
||||
@PostMapping
|
||||
public AjaxResult add(@RequestBody RmAlarmPushConfig rmAlarmPushConfig)
|
||||
{
|
||||
return toAjax(rmAlarmPushConfigService.insertRmAlarmPushConfig(rmAlarmPushConfig));
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改告警推送配置
|
||||
*/
|
||||
@RequiresPermissions("rocketmq:alarmPushConfig:edit")
|
||||
@Log(title = "告警推送配置", businessType = BusinessType.UPDATE)
|
||||
@PutMapping
|
||||
public AjaxResult edit(@RequestBody RmAlarmPushConfig rmAlarmPushConfig)
|
||||
{
|
||||
return toAjax(rmAlarmPushConfigService.updateRmAlarmPushConfig(rmAlarmPushConfig));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除告警推送配置
|
||||
*/
|
||||
@RequiresPermissions("rocketmq:alarmPushConfig:remove")
|
||||
@Log(title = "告警推送配置", businessType = BusinessType.DELETE)
|
||||
@DeleteMapping("/{ids}")
|
||||
public AjaxResult remove(@PathVariable Long[] ids)
|
||||
{
|
||||
return toAjax(rmAlarmPushConfigService.deleteRmAlarmPushConfigByIds(ids));
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
package com.ruoyi.rocketmq.domain;
|
||||
|
||||
import java.util.Date;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
import com.ruoyi.common.core.annotation.Excel;
|
||||
import com.ruoyi.common.core.web.domain.BaseEntity;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 客户端告警信息对象 rm_alarm_log
|
||||
@@ -13,6 +13,7 @@ import com.ruoyi.common.core.web.domain.BaseEntity;
|
||||
* @author gyt
|
||||
* @date 2025-11-10
|
||||
*/
|
||||
@Data
|
||||
public class RmAlarmLog extends BaseEntity
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
@@ -25,8 +26,8 @@ public class RmAlarmLog extends BaseEntity
|
||||
private String clientId;
|
||||
|
||||
/** 告警时间 */
|
||||
@JsonFormat(pattern = "yyyy-MM-dd")
|
||||
@Excel(name = "告警时间", width = 30, dateFormat = "yyyy-MM-dd")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@Excel(name = "告警时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
|
||||
private Date alarmTime;
|
||||
|
||||
/** 管理网-公网IP */
|
||||
@@ -40,80 +41,4 @@ public class RmAlarmLog extends BaseEntity
|
||||
/** 告警内容 */
|
||||
@Excel(name = "告警内容")
|
||||
private String alarmContent;
|
||||
|
||||
public void setId(Long id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setClientId(String clientId)
|
||||
{
|
||||
this.clientId = clientId;
|
||||
}
|
||||
|
||||
public String getClientId()
|
||||
{
|
||||
return clientId;
|
||||
}
|
||||
|
||||
public void setAlarmTime(Date alarmTime)
|
||||
{
|
||||
this.alarmTime = alarmTime;
|
||||
}
|
||||
|
||||
public Date getAlarmTime()
|
||||
{
|
||||
return alarmTime;
|
||||
}
|
||||
|
||||
public void setMgmPublicIp(String mgmPublicIp)
|
||||
{
|
||||
this.mgmPublicIp = mgmPublicIp;
|
||||
}
|
||||
|
||||
public String getMgmPublicIp()
|
||||
{
|
||||
return mgmPublicIp;
|
||||
}
|
||||
|
||||
public void setAlarmType(String alarmType)
|
||||
{
|
||||
this.alarmType = alarmType;
|
||||
}
|
||||
|
||||
public String getAlarmType()
|
||||
{
|
||||
return alarmType;
|
||||
}
|
||||
|
||||
public void setAlarmContent(String alarmContent)
|
||||
{
|
||||
this.alarmContent = alarmContent;
|
||||
}
|
||||
|
||||
public String getAlarmContent()
|
||||
{
|
||||
return alarmContent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
|
||||
.append("id", getId())
|
||||
.append("clientId", getClientId())
|
||||
.append("alarmTime", getAlarmTime())
|
||||
.append("mgmPublicIp", getMgmPublicIp())
|
||||
.append("alarmType", getAlarmType())
|
||||
.append("alarmContent", getAlarmContent())
|
||||
.append("createTime", getCreateTime())
|
||||
.append("updateTime", getUpdateTime())
|
||||
.append("createBy", getCreateBy())
|
||||
.append("updateBy", getUpdateBy())
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.ruoyi.rocketmq.domain;
|
||||
|
||||
import com.ruoyi.common.core.annotation.Excel;
|
||||
import com.ruoyi.common.core.web.domain.BaseEntity;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 告警推送配置对象 rm_alarm_push_config
|
||||
*
|
||||
* @author gyt
|
||||
* @date 2025-11-11
|
||||
*/
|
||||
@Data
|
||||
public class RmAlarmPushConfig extends BaseEntity
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** 主键ID */
|
||||
private Long id;
|
||||
|
||||
/** 配置名称 */
|
||||
@Excel(name = "配置名称")
|
||||
private String configName;
|
||||
|
||||
/** 推送方式 */
|
||||
@Excel(name = "推送方式")
|
||||
private String pushMethod;
|
||||
|
||||
/** 推送地址 */
|
||||
@Excel(name = "推送地址")
|
||||
private String pushAddress;
|
||||
|
||||
/** 推送告警类型 */
|
||||
@Excel(name = "推送告警类型")
|
||||
private String pushAlarmTypes;
|
||||
|
||||
/** 消息内容 */
|
||||
@Excel(name = "消息内容")
|
||||
private String messageContent;
|
||||
|
||||
/** 消息提示人手机号 */
|
||||
@Excel(name = "消息提示人手机号")
|
||||
private String contactPhones;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.ruoyi.rocketmq.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum AlarmTypeEnum {
|
||||
服务器下线("1", "服务器下线");
|
||||
private final String code;
|
||||
private final String msg;
|
||||
AlarmTypeEnum(String code, String msg){
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.ruoyi.rocketmq.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum PushMethodEnum {
|
||||
企业微信("1", "企业微信");
|
||||
private final String code;
|
||||
private final String msg;
|
||||
PushMethodEnum(String code, String msg){
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
}
|
||||
}
|
||||
@@ -10,9 +10,12 @@ import com.ruoyi.rocketmq.domain.vo.CollectDataVo;
|
||||
import com.ruoyi.rocketmq.domain.vo.RegisterMsgVo;
|
||||
import com.ruoyi.rocketmq.domain.vo.RspResultVo;
|
||||
import com.ruoyi.rocketmq.domain.vo.RspVo;
|
||||
import com.ruoyi.rocketmq.enums.AlarmTypeEnum;
|
||||
import com.ruoyi.rocketmq.enums.PushMethodEnum;
|
||||
import com.ruoyi.rocketmq.service.*;
|
||||
import com.ruoyi.rocketmq.utils.DataProcessUtil;
|
||||
import com.ruoyi.rocketmq.utils.JsonDataParser;
|
||||
import com.ruoyi.rocketmq.utils.WeChatWorkBot;
|
||||
import com.ruoyi.system.api.RemoteRevenueConfigService;
|
||||
import com.ruoyi.system.api.domain.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -89,6 +92,8 @@ public class MessageHandler {
|
||||
private IInitialBandwidthTrafficTempService initialBandwidthTrafficTempService;
|
||||
@Autowired
|
||||
private IRmAlarmLogService rmAlarmLogService;
|
||||
@Autowired
|
||||
private IRmAlarmPushConfigService rmAlarmPushConfigService;
|
||||
|
||||
|
||||
/**
|
||||
@@ -1014,6 +1019,8 @@ public class MessageHandler {
|
||||
if (lostCount >= 3) {
|
||||
log.warn("客户端ID: {} 连续三次心跳丢失,触发告警", clientId);
|
||||
insertHeartbeatLog(clientId, "3", "连续三次心跳丢失");
|
||||
// 告警
|
||||
insertAlarmRecords(clientId);
|
||||
redisTemplate.opsForValue().set(HEARTBEAT_ALERT_PREFIX + clientId, "1");
|
||||
// 设置告警后删除timeKey和statusKey
|
||||
redisTemplate.delete(timeKey);
|
||||
@@ -1103,12 +1110,31 @@ public class MessageHandler {
|
||||
// 查询此告警是否存在
|
||||
RmAlarmLog query = new RmAlarmLog();
|
||||
query.setClientId(clientId);
|
||||
query.setAlarmType("1");
|
||||
query.setAlarmType(AlarmTypeEnum.服务器下线.getCode());
|
||||
List<RmAlarmLog> alarmLog = rmAlarmLogService.selectRmAlarmLogList(query);
|
||||
if(alarmLog != null && !alarmLog.isEmpty()){
|
||||
rmAlarmLogService.updateRmAlarmLog(rmAlarmLog);
|
||||
}else{
|
||||
rmAlarmLogService.insertRmAlarmLog(rmAlarmLog);
|
||||
// 拿到消息模板
|
||||
RmAlarmPushConfig rmAlarmPushConfig = new RmAlarmPushConfig();
|
||||
rmAlarmPushConfig.setPushMethod(PushMethodEnum.企业微信.getCode());
|
||||
rmAlarmPushConfig.setPushAlarmTypes(AlarmTypeEnum.服务器下线.getCode());
|
||||
List<RmAlarmPushConfig> alarmConfigList = rmAlarmPushConfigService.selectRmAlarmPushConfigList(rmAlarmPushConfig);
|
||||
if(alarmConfigList != null && !alarmConfigList.isEmpty()){
|
||||
for (RmAlarmPushConfig alarmPushConfig : alarmConfigList) {
|
||||
String contentTemplate = alarmPushConfig.getMessageContent();
|
||||
String webhookUrl = alarmPushConfig.getPushAddress();
|
||||
Map<String, Object> alarmMap = new HashMap<>();
|
||||
alarmMap.put("告警时间", rmAlarmLog.getAlarmTime());
|
||||
alarmMap.put("管理网-公网IP", rmAlarmLog.getMgmPublicIp());
|
||||
alarmMap.put("告警类型", AlarmTypeEnum.服务器下线.getMsg());
|
||||
alarmMap.put("告警设备", rmAlarmLog.getClientId());
|
||||
alarmMap.put("告警内容", rmAlarmLog.getAlarmContent());
|
||||
// 推送消息到企业微信
|
||||
WeChatWorkBot.sendTemplateMessage(webhookUrl, contentTemplate, alarmMap);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.ruoyi.rocketmq.mapper;
|
||||
|
||||
import java.util.List;
|
||||
import com.ruoyi.rocketmq.domain.RmAlarmPushConfig;
|
||||
|
||||
/**
|
||||
* 告警推送配置Mapper接口
|
||||
*
|
||||
* @author gyt
|
||||
* @date 2025-11-11
|
||||
*/
|
||||
public interface RmAlarmPushConfigMapper
|
||||
{
|
||||
/**
|
||||
* 查询告警推送配置
|
||||
*
|
||||
* @param id 告警推送配置主键
|
||||
* @return 告警推送配置
|
||||
*/
|
||||
public RmAlarmPushConfig selectRmAlarmPushConfigById(Long id);
|
||||
|
||||
/**
|
||||
* 查询告警推送配置列表
|
||||
*
|
||||
* @param rmAlarmPushConfig 告警推送配置
|
||||
* @return 告警推送配置集合
|
||||
*/
|
||||
public List<RmAlarmPushConfig> selectRmAlarmPushConfigList(RmAlarmPushConfig rmAlarmPushConfig);
|
||||
|
||||
/**
|
||||
* 新增告警推送配置
|
||||
*
|
||||
* @param rmAlarmPushConfig 告警推送配置
|
||||
* @return 结果
|
||||
*/
|
||||
public int insertRmAlarmPushConfig(RmAlarmPushConfig rmAlarmPushConfig);
|
||||
|
||||
/**
|
||||
* 修改告警推送配置
|
||||
*
|
||||
* @param rmAlarmPushConfig 告警推送配置
|
||||
* @return 结果
|
||||
*/
|
||||
public int updateRmAlarmPushConfig(RmAlarmPushConfig rmAlarmPushConfig);
|
||||
|
||||
/**
|
||||
* 删除告警推送配置
|
||||
*
|
||||
* @param id 告警推送配置主键
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteRmAlarmPushConfigById(Long id);
|
||||
|
||||
/**
|
||||
* 批量删除告警推送配置
|
||||
*
|
||||
* @param ids 需要删除的数据主键集合
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteRmAlarmPushConfigByIds(Long[] ids);
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
package com.ruoyi.rocketmq.service;
|
||||
|
||||
import java.util.List;
|
||||
import com.ruoyi.rocketmq.domain.RmAlarmPushConfig;
|
||||
|
||||
/**
|
||||
* 告警推送配置Service接口
|
||||
*
|
||||
* @author gyt
|
||||
* @date 2025-11-11
|
||||
*/
|
||||
public interface IRmAlarmPushConfigService
|
||||
{
|
||||
/**
|
||||
* 查询告警推送配置
|
||||
*
|
||||
* @param id 告警推送配置主键
|
||||
* @return 告警推送配置
|
||||
*/
|
||||
public RmAlarmPushConfig selectRmAlarmPushConfigById(Long id);
|
||||
|
||||
/**
|
||||
* 查询告警推送配置列表
|
||||
*
|
||||
* @param rmAlarmPushConfig 告警推送配置
|
||||
* @return 告警推送配置集合
|
||||
*/
|
||||
public List<RmAlarmPushConfig> selectRmAlarmPushConfigList(RmAlarmPushConfig rmAlarmPushConfig);
|
||||
|
||||
/**
|
||||
* 新增告警推送配置
|
||||
*
|
||||
* @param rmAlarmPushConfig 告警推送配置
|
||||
* @return 结果
|
||||
*/
|
||||
public int insertRmAlarmPushConfig(RmAlarmPushConfig rmAlarmPushConfig);
|
||||
|
||||
/**
|
||||
* 修改告警推送配置
|
||||
*
|
||||
* @param rmAlarmPushConfig 告警推送配置
|
||||
* @return 结果
|
||||
*/
|
||||
public int updateRmAlarmPushConfig(RmAlarmPushConfig rmAlarmPushConfig);
|
||||
|
||||
/**
|
||||
* 批量删除告警推送配置
|
||||
*
|
||||
* @param ids 需要删除的告警推送配置主键集合
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteRmAlarmPushConfigByIds(Long[] ids);
|
||||
|
||||
/**
|
||||
* 删除告警推送配置信息
|
||||
*
|
||||
* @param id 告警推送配置主键
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteRmAlarmPushConfigById(Long id);
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
package com.ruoyi.rocketmq.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
import com.ruoyi.common.core.utils.DateUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.ruoyi.rocketmq.mapper.RmAlarmPushConfigMapper;
|
||||
import com.ruoyi.rocketmq.domain.RmAlarmPushConfig;
|
||||
import com.ruoyi.rocketmq.service.IRmAlarmPushConfigService;
|
||||
|
||||
/**
|
||||
* 告警推送配置Service业务层处理
|
||||
*
|
||||
* @author gyt
|
||||
* @date 2025-11-11
|
||||
*/
|
||||
@Service
|
||||
public class RmAlarmPushConfigServiceImpl implements IRmAlarmPushConfigService
|
||||
{
|
||||
@Autowired
|
||||
private RmAlarmPushConfigMapper rmAlarmPushConfigMapper;
|
||||
|
||||
/**
|
||||
* 查询告警推送配置
|
||||
*
|
||||
* @param id 告警推送配置主键
|
||||
* @return 告警推送配置
|
||||
*/
|
||||
@Override
|
||||
public RmAlarmPushConfig selectRmAlarmPushConfigById(Long id)
|
||||
{
|
||||
return rmAlarmPushConfigMapper.selectRmAlarmPushConfigById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询告警推送配置列表
|
||||
*
|
||||
* @param rmAlarmPushConfig 告警推送配置
|
||||
* @return 告警推送配置
|
||||
*/
|
||||
@Override
|
||||
public List<RmAlarmPushConfig> selectRmAlarmPushConfigList(RmAlarmPushConfig rmAlarmPushConfig)
|
||||
{
|
||||
return rmAlarmPushConfigMapper.selectRmAlarmPushConfigList(rmAlarmPushConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增告警推送配置
|
||||
*
|
||||
* @param rmAlarmPushConfig 告警推送配置
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int insertRmAlarmPushConfig(RmAlarmPushConfig rmAlarmPushConfig)
|
||||
{
|
||||
rmAlarmPushConfig.setCreateTime(DateUtils.getNowDate());
|
||||
return rmAlarmPushConfigMapper.insertRmAlarmPushConfig(rmAlarmPushConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改告警推送配置
|
||||
*
|
||||
* @param rmAlarmPushConfig 告警推送配置
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int updateRmAlarmPushConfig(RmAlarmPushConfig rmAlarmPushConfig)
|
||||
{
|
||||
rmAlarmPushConfig.setUpdateTime(DateUtils.getNowDate());
|
||||
return rmAlarmPushConfigMapper.updateRmAlarmPushConfig(rmAlarmPushConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除告警推送配置
|
||||
*
|
||||
* @param ids 需要删除的告警推送配置主键
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int deleteRmAlarmPushConfigByIds(Long[] ids)
|
||||
{
|
||||
return rmAlarmPushConfigMapper.deleteRmAlarmPushConfigByIds(ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除告警推送配置信息
|
||||
*
|
||||
* @param id 告警推送配置主键
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int deleteRmAlarmPushConfigById(Long id)
|
||||
{
|
||||
return rmAlarmPushConfigMapper.deleteRmAlarmPushConfigById(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,285 @@
|
||||
package com.ruoyi.rocketmq.utils;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class WeChatWorkBot {
|
||||
private static final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
/**
|
||||
* 发送基于模板的文本消息
|
||||
* @param webhookUrl webhook地址
|
||||
* @param template 消息模板,例如:"项目[项目名称]在[时间]发生[事件类型]"
|
||||
* @param fieldValues 字段值的映射,key为中文字段名,value为实际值(支持String、Number、Boolean等)
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
public static boolean sendTemplateMessage(String webhookUrl, String template,
|
||||
Map<String, Object> fieldValues) {
|
||||
return sendTemplateMessage(webhookUrl, template, fieldValues, null, null, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送基于模板的文本消息(支持@功能)
|
||||
* @param webhookUrl webhook地址
|
||||
* @param template 消息模板
|
||||
* @param fieldValues 字段值的映射
|
||||
* @param mentionedList 被@的用户列表(手机号)
|
||||
* @param mentionedAll 是否@所有人
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
public static boolean sendTemplateMessage(String webhookUrl, String template,
|
||||
Map<String, Object> fieldValues,
|
||||
String[] mentionedList, boolean mentionedAll) {
|
||||
return sendTemplateMessage(webhookUrl, template, fieldValues, null, mentionedList, mentionedAll);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送基于模板的文本消息(完整参数)
|
||||
* @param webhookUrl webhook地址
|
||||
* @param template 消息模板
|
||||
* @param fieldValues 字段值的映射
|
||||
* @param defaultValue 未找到字段时的默认值
|
||||
* @param mentionedList 被@的用户列表(手机号)
|
||||
* @param mentionedAll 是否@所有人
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
public static boolean sendTemplateMessage(String webhookUrl, String template,
|
||||
Map<String, Object> fieldValues, Object defaultValue,
|
||||
String[] mentionedList, boolean mentionedAll) {
|
||||
try {
|
||||
String actualContent = processTemplate(template, fieldValues, defaultValue);
|
||||
return sendTextMessage(webhookUrl, actualContent, mentionedList, mentionedAll);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理模板,替换字段占位符
|
||||
* @param template 消息模板
|
||||
* @param fieldValues 字段值映射
|
||||
* @param defaultValue 默认值
|
||||
* @return 处理后的消息内容
|
||||
*/
|
||||
public static String processTemplate(String template, Map<String, Object> fieldValues,
|
||||
Object defaultValue) {
|
||||
if (template == null) return "";
|
||||
if (fieldValues == null || fieldValues.isEmpty()) {
|
||||
return template;
|
||||
}
|
||||
|
||||
Pattern pattern = Pattern.compile("\\[(.*?)\\]");
|
||||
Matcher matcher = pattern.matcher(template);
|
||||
StringBuffer result = new StringBuffer();
|
||||
|
||||
while (matcher.find()) {
|
||||
String fieldName = matcher.group(1);
|
||||
Object fieldValueObj = fieldValues.get(fieldName);
|
||||
String fieldValue = convertToString(fieldValueObj);
|
||||
|
||||
// 如果字段值为空,使用默认值或保留原占位符
|
||||
if (fieldValue == null || fieldValue.trim().isEmpty()) {
|
||||
fieldValue = convertToString(defaultValue);
|
||||
if (fieldValue == null) {
|
||||
fieldValue = "[" + fieldName + "]";
|
||||
}
|
||||
}
|
||||
|
||||
// 对替换值进行转义,防止正则表达式特殊字符问题
|
||||
matcher.appendReplacement(result, Matcher.quoteReplacement(fieldValue));
|
||||
}
|
||||
matcher.appendTail(result);
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将对象转换为字符串
|
||||
* @param obj 要转换的对象
|
||||
* @return 字符串表示
|
||||
*/
|
||||
private static String convertToString(Object obj) {
|
||||
if (obj == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (obj instanceof String) {
|
||||
return (String) obj;
|
||||
} else if (obj instanceof Number || obj instanceof Boolean) {
|
||||
return String.valueOf(obj);
|
||||
} else if (obj instanceof java.util.Date) {
|
||||
return new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((java.util.Date) obj);
|
||||
} else {
|
||||
return obj.toString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证模板中的字段是否都有对应的值
|
||||
* @param template 消息模板
|
||||
* @param fieldValues 字段值映射
|
||||
* @return 是否所有字段都有值
|
||||
*/
|
||||
public static boolean validateTemplate(String template, Map<String, Object> fieldValues) {
|
||||
if (template == null || fieldValues == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Pattern pattern = Pattern.compile("\\[(.*?)\\]");
|
||||
Matcher matcher = pattern.matcher(template);
|
||||
|
||||
while (matcher.find()) {
|
||||
String fieldName = matcher.group(1);
|
||||
Object fieldValue = fieldValues.get(fieldName);
|
||||
String strValue = convertToString(fieldValue);
|
||||
|
||||
if (!fieldValues.containsKey(fieldName) ||
|
||||
strValue == null ||
|
||||
strValue.trim().isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取模板中的所有字段名
|
||||
* @param template 消息模板
|
||||
* @return 字段名列表
|
||||
*/
|
||||
public static java.util.List<String> getTemplateFields(String template) {
|
||||
java.util.List<String> fields = new java.util.ArrayList<>();
|
||||
if (template == null) {
|
||||
return fields;
|
||||
}
|
||||
|
||||
Pattern pattern = Pattern.compile("\\[(.*?)\\]");
|
||||
Matcher matcher = pattern.matcher(template);
|
||||
|
||||
while (matcher.find()) {
|
||||
fields.add(matcher.group(1));
|
||||
}
|
||||
return fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送Markdown模板消息
|
||||
* @param webhookUrl webhook地址
|
||||
* @param template Markdown模板
|
||||
* @param fieldValues 字段值映射
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
public static boolean sendMarkdownTemplateMessage(String webhookUrl, String template,
|
||||
Map<String, Object> fieldValues) {
|
||||
return sendMarkdownTemplateMessage(webhookUrl, template, fieldValues, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送Markdown模板消息
|
||||
* @param webhookUrl webhook地址
|
||||
* @param template Markdown模板
|
||||
* @param fieldValues 字段值映射
|
||||
* @param defaultValue 默认值
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
public static boolean sendMarkdownTemplateMessage(String webhookUrl, String template,
|
||||
Map<String, Object> fieldValues,
|
||||
Object defaultValue) {
|
||||
try {
|
||||
String actualContent = processTemplate(template, fieldValues, defaultValue);
|
||||
return sendMarkdownMessage(webhookUrl, actualContent);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送文本消息
|
||||
*/
|
||||
public static boolean sendTextMessage(String webhookUrl, String content) {
|
||||
return sendTextMessage(webhookUrl, content, null, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送文本消息(支持@功能)
|
||||
*/
|
||||
public static boolean sendTextMessage(String webhookUrl, String content,
|
||||
String[] mentionedList, boolean mentionedAll) {
|
||||
try {
|
||||
Map<String, Object> message = new HashMap<>();
|
||||
message.put("msgtype", "text");
|
||||
|
||||
Map<String, Object> textContent = new HashMap<>();
|
||||
textContent.put("content", content);
|
||||
|
||||
if (mentionedAll) {
|
||||
textContent.put("mentioned_list", new String[]{"@all"});
|
||||
} else if (mentionedList != null && mentionedList.length > 0) {
|
||||
textContent.put("mentioned_list", mentionedList);
|
||||
}
|
||||
|
||||
message.put("text", textContent);
|
||||
|
||||
return sendMessage(webhookUrl, mapper.writeValueAsString(message));
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送Markdown消息
|
||||
*/
|
||||
public static boolean sendMarkdownMessage(String webhookUrl, String content) {
|
||||
try {
|
||||
Map<String, Object> message = new HashMap<>();
|
||||
message.put("msgtype", "markdown");
|
||||
|
||||
Map<String, Object> markdownContent = new HashMap<>();
|
||||
markdownContent.put("content", content);
|
||||
|
||||
message.put("markdown", markdownContent);
|
||||
|
||||
return sendMessage(webhookUrl, mapper.writeValueAsString(message));
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean sendMessage(String webhookUrl, String jsonBody) {
|
||||
try {
|
||||
URL url = new URL(webhookUrl);
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestProperty("Content-Type", "application/json");
|
||||
connection.setDoOutput(true);
|
||||
connection.setConnectTimeout(5000);
|
||||
connection.setReadTimeout(10000);
|
||||
|
||||
try (OutputStream os = connection.getOutputStream()) {
|
||||
byte[] input = jsonBody.getBytes("UTF-8");
|
||||
os.write(input, 0, input.length);
|
||||
}
|
||||
|
||||
int responseCode = connection.getResponseCode();
|
||||
return responseCode == 200;
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user