evoToK3Cloud/ruoyi-common/src/main/java/com/ruoyi/common/utils/WxRobotUtil.java
tzy 43a4b7a756 feat(common): 新增文件操作与PDF处理工具
- 新增图纸路径常量配置
  - 新增DeleteFile文件删除工具类
  - 新增PDFDocHelper用于PDF水印、合并等操作
  - 新增FTPDownload支持FTP文件批量下载
  - 新增FtpUtil提供完整的FTP客户端功能,支持文件上传下载及目录管理
2025-07-28 08:42:58 +08:00

201 lines
7.8 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.ruoyi.common.utils;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
@Slf4j
@Component
public class WxRobotUtil {
@Resource
private HttpRequestUtil httpRequestUtil;
/** 企业微信群上传文件url */
public static final String UPLOAD_FILE_URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media";
/** 发送群消息url */
public static final String SEND_MESSAGE_URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send";
/**
* 上传文件并推送到企业微信群
* @param file
* @param robotId 机器人Id
* @throws ServiceException
*/
public void sendFileToWeChatGroup(File file, String robotId) {
// 上传文件
String urlString = UPLOAD_FILE_URL + "?key=" + robotId + "&type=file";
ResponseEntity<Object> result = httpRequestUtil.doUploadFile(urlString, file);
JSONObject dataObject = JSONObject.parseObject(JSONObject.toJSONString(result.getBody()));
Integer errcode = Integer.valueOf(dataObject.get("errcode").toString());
if (errcode.equals(0)) {
// 推送消息
String mediaid = (String) dataObject.get("media_id");
String sendUrl = SEND_MESSAGE_URL + "?key=" + robotId;
Map<String, Object> mediaMap = new HashMap<>();
mediaMap.put("media_id", mediaid);
Map<String, Object> msgMap = new HashMap<>();
msgMap.put("msgtype", "file");
msgMap.put("file", mediaMap);
httpRequestUtil.doPost(sendUrl, msgMap);
log.info("企业微信推送报表成功,时间:" + new Date());
} else {
log.error("企业微信推送报表失败,时间:" + new Date());
}
}
/**
* 推送信息给企业微信机器人
* @param msg 消息内容
* @param robotId 机器人ID
* @param mentionAll 是否@所有人
*/
public void sendMsgToWeChatGroup(String msg, String robotId, boolean mentionAll) {
HashMap<String, Object> paramMap = new HashMap<>();
HashMap<String, Object> textMap = new HashMap<>();
textMap.put("content", msg);
// 添加@所有人的配置
if (mentionAll) {
textMap.put("mentioned_list", Collections.singletonList("@all"));
}
paramMap.put("msgtype", "text");
paramMap.put("text", textMap);
String sendUrl = SEND_MESSAGE_URL + "?key=" + robotId;
ResponseEntity<Object> result = httpRequestUtil.doPost(sendUrl, paramMap);
JSONObject dataObject = JSONObject.parseObject(JSONObject.toJSONString(result.getBody()));
Integer errcode = Integer.valueOf(dataObject.get("errcode").toString());
if (errcode.equals(0)) {
log.info("企业微信推送消息成功,时间:" + new Date());
} else {
log.error("企业微信推送消息失败,时间:" + new Date());
}
}
// 保留原有方法,默认不@所有人
public void sendMsgToWeChatGroup(String msg, String robotId) {
sendMsgToWeChatGroup(msg, robotId, false);
}
/**
* 推送Markdown格式信息给企业微信机器人
* @param markdownMsg Markdown格式的消息内容
* @param robotId 机器人ID
*/
public void sendMarkdownMsgToWeChatGroup(String markdownMsg, String robotId) {
String messageContent = markdownMsg.toString();
if (messageContent.length() > 4096) {
messageContent = messageContent.substring(0, 4096); // 截断到最大长度
}
HashMap<String, Object> paramMap = new HashMap<>();
HashMap<String, Object> markdownMap = new HashMap<>();
markdownMap.put("content", messageContent);
paramMap.put("msgtype", "markdown");
paramMap.put("markdown", markdownMap);
String sendUrl = SEND_MESSAGE_URL + "?key=" + robotId;
log.info("发送URL: {}", sendUrl);
log.info("发送参数: {}", paramMap);
ResponseEntity<Object> result = httpRequestUtil.doPost(sendUrl, paramMap);
JSONObject dataObject = JSONObject.parseObject(JSONObject.toJSONString(result.getBody()));
Integer errcode = Integer.valueOf(dataObject.get("errcode").toString());
if (errcode.equals(0)) {
log.info("企业微信推送Markdown消息成功时间" + new Date());
} else {
log.error("企业微信推送Markdown消息失败时间" + new Date() + ",错误信息:" + dataObject.toJSONString());
}
}
/**
* 推送图文类型信息给企业微信机器人
* @param title 标题
* @param description 描述
* @param url 点击跳转链接
* @param picUrl 图片链接
* @param robotId 机器人ID
*/
public void sendNewsToWeChatGroup(String title, String description, String url, String picUrl, String robotId) {
HashMap<String, Object> paramMap = new HashMap<>();
// 创建文章对象
HashMap<String, Object> article = new HashMap<>();
article.put("title", title);
article.put("description", description);
article.put("url", url);
article.put("picurl", picUrl);
// 创建文章列表
List<HashMap<String, Object>> articles = new ArrayList<>();
articles.add(article);
// 创建news对象
HashMap<String, Object> newsMap = new HashMap<>();
newsMap.put("articles", articles);
// 设置消息类型和内容
paramMap.put("msgtype", "news");
paramMap.put("news", newsMap);
String sendUrl = SEND_MESSAGE_URL + "?key=" + robotId;
ResponseEntity<Object> result = httpRequestUtil.doPost(sendUrl, paramMap);
JSONObject dataObject = JSONObject.parseObject(JSONObject.toJSONString(result.getBody()));
Integer errcode = Integer.valueOf(dataObject.get("errcode").toString());
if (errcode.equals(0)) {
log.info("企业微信推送图文消息成功,时间:" + new Date());
} else {
log.error("企业微信推送图文消息失败,时间:" + new Date());
}
}
public static String getAccessToken(String corpId, String corpSecret) {
try {
// 构建请求 URL
String urlStr = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpId + "&corpsecret=" + corpSecret;
URL url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
// 读取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
// 解析 JSON 响应
JSONObject jsonResponse = new JSONObject(response.toString().isEmpty());
int errcode = jsonResponse.getObject("errcode",int.class);
if (errcode == 0) {
// 获取 access_token
return jsonResponse.getString("access_token");
} else {
System.out.println("获取 access_token 失败: " + jsonResponse.getString("errmsg"));
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}