package cn.van.business.util; import cn.hutool.core.util.ObjectUtil; import cn.hutool.http.HttpRequest; import cn.van.business.enums.WXReqType; import cn.van.business.mq.MessageProducerService; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.core.env.Environment; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.CompletableFuture; /** * @author Leo * @version 1.0 * @create 2023/12/22 0022 上午 09:59 * @description: */ @Component public class WXUtil { public static final String default_super_admin_wxid = "wxid_ytpc72mdoskt22"; private static final Logger logger = LoggerFactory.getLogger(WXUtil.class); public static String default_bot_wxid = "wxid_kr145nk7l0an31"; public static Map super_admins = new HashMap<>(); public static Map jdidToWxidMap = new HashMap<>(); public static Map jdidToRemarkMap = new HashMap<>(); public static List notify_wx = new ArrayList<>(); // 群聊管理白名单 public static List chatRoom_admin = new ArrayList<>(); public static List chatRoom_admin_inner = new ArrayList<>(); public static List chatRoom_admin_pl = new ArrayList<>(); // 线报来源群 public static Map chatRoom_xb = new HashMap<>(); // 搬运群,对外带佣金,不带方案 public static String chatRoom_BY = "50400969285@chatroom"; //群(50006079425@chatroom)wxid_cfmrk2upjtf322:1 public static String chatRoom_JD_Order = "50006079425@chatroom"; /** * url http://127.0.0.1:7777/DaenWxHook/httpapi/ * 获取微信列表 (X0000) * 微信状态检测(Q0000) * 发送文本消息(Q0001) * 修改下载图片 (Q0002) * 获取个人信息 (Q0003) * 查询对象信(Q0004) * 获取好友列表(Q0005) * 获取群聊列表(Q0006) * 获取公众号列表(Q0007) * 获取群成员列表(Q0008) * 发送聊天记录(Q0009) * 发送图片 (Q0010) * 发送本地文件(Q0011) * 发送分享链接(Q0012) * 发送小程序(Q0013) * 发送音乐分享(Q0014) * 发送XML (Q0015) * 确认收款 (Q0016) * 同意好友请求(Q0017) * 添加好友通过v3(Q0018) * 添加好友_通过wxid (Q0019) * 查询陌生人信息(Q0020) * 邀请进群(Q0021) * 删除好友(Q0022) * 修改对象备注(Q0023) * 修改群聊名称(Q0024) * 发送名片(Q0025) */ public static String WX_BASE_URL; private final WxtsUtil wxTsUtil; private final MessageProducerService messageProducerService; public int sendTimes = 0; private static boolean restartNoticeSent = false; @Autowired public WXUtil(Environment env, WxtsUtil wxTsUtil, @Lazy MessageProducerService messageProducerService) { this.messageProducerService = messageProducerService; this.wxTsUtil = wxTsUtil; WX_BASE_URL = env.getProperty("config.WX_BASE_URL"); System.out.println("WX_BASE_URL:" + WX_BASE_URL); initSuperAdmins(); } public static String getWxidFromJdid(String jdid) { return jdidToWxidMap.get(jdid); } public static String getRemarkFromJdid(String jdid) { return jdidToRemarkMap.get(jdid); } public static List getSuperAdmins(String wxid) { List result = new ArrayList<>(); for (SuperAdmin admin : super_admins.values()) { if (admin.getWxid().equals(wxid)) { result.add(admin); } } return result; } public static List splitStringByLength(String input, int length) { List result = new ArrayList<>(); // 循环增加长度直到超过字符串长度 for (int start = 0; start < input.length(); start += length) { // 截取字符串,但需要检查边界 int end = Math.min(start + length, input.length()); result.add(input.substring(start, end)); } return result; } // 初始化超级管理员 public void initSuperAdmins() { if (restartNoticeSent) { return; } SuperAdmin admin1 = new SuperAdmin("wxid_ytpc72mdoskt22", "凡", "2014264913", "98e21c89ae5610240ec3f5f575f86a59", "3dcb6b23a1104639ac433fd07adb6dfb"); super_admins.put(admin1.getWxid() + admin1.getUnionId(), admin1); jdidToWxidMap.put(admin1.getUnionId(), admin1.getWxid()); jdidToRemarkMap.put(admin1.getUnionId(), admin1.getName()); SuperAdmin admin2 = new SuperAdmin("wxid_yneqf1implxu12", "源", "2025353364", "e3c161242c8a1416fada5b5564d7ee70", "41ae9aabf03b41e6ba309682e36b323e"); super_admins.put(admin2.getWxid() + admin2.getUnionId(), admin2); jdidToWxidMap.put(admin2.getUnionId(), admin2.getWxid()); jdidToRemarkMap.put(admin2.getUnionId(), admin2.getName()); SuperAdmin admin3 = new SuperAdmin("wxid_ytpc72mdoskt22", "琳", "2035096657", "9c2011409f0fc906b73432dd3687599d", "3ceddff403e544a8a2eacc727cf05dab"); super_admins.put(admin3.getWxid() + admin3.getUnionId(), admin3); jdidToWxidMap.put(admin3.getUnionId(), admin3.getWxid()); jdidToRemarkMap.put(admin3.getUnionId(), admin3.getName()); SuperAdmin admin4 = new SuperAdmin("wxid_ytpc72mdoskt22", "淑", "2036678559", "4dbc8edd74fbb24d537ee64930527e30", "27ee9b5871e547eb83e19027a4ff7e18"); super_admins.put(admin4.getWxid() + admin4.getUnionId(), admin4); jdidToWxidMap.put(admin4.getUnionId(), admin4.getWxid()); jdidToRemarkMap.put(admin4.getUnionId(), admin4.getName()); SuperAdmin admin5 = new SuperAdmin("wxid_ytpc72mdoskt22", "妈", "2035140236", "5cc8da6a55842330775fad4ee9ebf47a", "c4aebcd408924143a75463781f6acd7c"); super_admins.put(admin5.getWxid() + admin5.getUnionId(), admin5); jdidToWxidMap.put(admin5.getUnionId(), admin5.getWxid()); jdidToRemarkMap.put(admin5.getUnionId(), admin5.getName()); SuperAdmin admin6 = new SuperAdmin("wxid_ytpc72mdoskt22", "心", "2019237086", "01381d95e4936f1f3fe643bba2171894", "087c56aa156a4c20be280195be8b3eda"); super_admins.put(admin6.getWxid() + admin6.getUnionId(), admin6); jdidToWxidMap.put(admin6.getUnionId(), admin6.getWxid()); jdidToRemarkMap.put(admin6.getUnionId(), admin6.getName()); SuperAdmin admin7 = new SuperAdmin("wxid_ytpc72mdoskt22", "敏", "2033493067", "66618e24a5e9f44156253c4eb9892388", "15846e2334934482a703417d5058f842"); super_admins.put(admin7.getWxid() + admin7.getUnionId(), admin7); jdidToWxidMap.put(admin7.getUnionId(), admin7.getWxid()); jdidToRemarkMap.put(admin7.getUnionId(), admin7.getName()); //370|10:39:16|wxid_kr145nk7l0an31|发出私聊|【发送】wxid_gca9mnidqhkq11: SuperAdmin admin8 = new SuperAdmin("wxid_gca9mnidqhkq11", "梓豪", "1003068753", "e335738c819a7c3343d4dde52c393880", "b56abdd9b177466ba2afb6f0ee89709c"); super_admins.put(admin8.getWxid() + admin8.getUnionId(), admin8); jdidToWxidMap.put(admin8.getUnionId(), admin8.getWxid()); jdidToRemarkMap.put(admin8.getUnionId(), admin8.getName()); SuperAdmin admin9 = new SuperAdmin("wxid_ytpc72mdoskt22", "楠", "2032071669", "327beaa7ee7b5911a79e241013971403", "9b5110c76a534f09808258057e3cf20b"); super_admins.put(admin9.getWxid() + admin9.getUnionId(), admin9); jdidToWxidMap.put(admin9.getUnionId(), admin9.getWxid()); jdidToRemarkMap.put(admin9.getUnionId(), admin9.getName()); SuperAdmin admin10 = new SuperAdmin("wxid_sr3r8ot0z6do12", "星", "2036719699", "43de3e21ab2936380e558c5606e2bae0", "1fb013c8d5fb4e2597cbdb19fe128b02"); super_admins.put(admin10.getWxid() + admin10.getUnionId(), admin10); jdidToWxidMap.put(admin10.getUnionId(), admin10.getWxid()); jdidToRemarkMap.put(admin10.getUnionId(), admin10.getName()); //102|12:50:55|wxid_kr145nk7l0an31|收到私聊|:1 0213的微信号 SuperAdmin admin11 = new SuperAdmin("wxid_cfmrk2upjtf322", "0213", "", "", ""); super_admins.put(admin11.getWxid() + admin11.getUnionId(), admin11); SuperAdmin admin12 = new SuperAdmin("wxid_ytpc72mdoskt22", "轩", "2036765910", "e9c3f42fb6452af9c3dfc4a8496d6798", "5e56bb788a3c4d9ba032234277c521f2"); super_admins.put(admin12.getWxid() + admin12.getUnionId(), admin12); jdidToWxidMap.put(admin12.getUnionId(), admin12.getWxid()); jdidToRemarkMap.put(admin12.getUnionId(), admin12.getName()); SuperAdmin admin13 = new SuperAdmin("wxid_ytpc72mdoskt22", "牛", "2036827177", "0218a93e14d1a37661889e91333aed30", "c0f5f9a37d904f95a9e968be97ad37bb"); super_admins.put(admin13.getWxid() + admin13.getUnionId(), admin13); jdidToWxidMap.put(admin13.getUnionId(), admin13.getWxid()); jdidToRemarkMap.put(admin13.getUnionId(), admin13.getName()); /* 内部管理群 */ // 方案 //chatRoom_admin.add("50400969285@chatroom"); // 闲鱼 chatRoom_admin.add("50203565991@chatroom"); chatRoom_admin_inner.add("50203565991@chatroom"); // 什么都发 chatRoom_admin.add("49533691813@chatroom"); chatRoom_admin_inner.add("49533691813@chatroom"); // 方案交互群 chatRoom_admin.add("44960628585@chatroom"); chatRoom_admin_inner.add("44960628585@chatroom"); // 评价生成群 大群 chatRoom_admin_pl.add("47484514467@chatroom"); // 评价生成群 小群 群(43745034055@chatroom)wxid_gca9mnidqhkq11:加入群聊 chatRoom_admin_pl.add("43745034055@chatroom"); //群(43835433515@chatroom)wxid_ytpc72mdoskt22:1 chatRoom_admin_pl.add("43835433515@chatroom"); /* 线报采集来源群 */ // 玩了买 chatRoom_xb.put("23143922156@chatroom", "玩乐买"); // 舵手群(48621589056@chatroom):"曲莉亚@河南慧推电子商务有限公司、齐学法@河南慧推电子商务有限公司"为企业微信用户,<_wc_custom_link_ href="https://weixin.qq.com/cgi-bin/newreadtemplate?t=work_wechat/about_group">了解更多。 chatRoom_xb.put("48621589056@chatroom", "舵手"); String messageContent = "Jarvis 更新完成 [亲亲][亲亲][亲亲] "; String fromWxid = default_bot_wxid; // 来源为机器人自身 for (SuperAdmin admin : super_admins.values()) { if (!notify_wx.contains(admin.getWxid())){ try { sendTextMessage(admin.getWxid(), messageContent, 1, fromWxid, false); notify_wx.add(admin.getWxid()); } catch (Exception e) { logger.warn("发送重启通知失败,目标 wxid: {}", admin.getWxid(), e); } } } restartNoticeSent = true; } // 获取微信列表 public JSONObject getWxList() { WxReqDate wxReqDate = createWxReqData(WXReqType.GET_WX_LIST); String responseStr = HttpRequest.post(WX_BASE_URL).body(JSON.toJSONString(wxReqDate)).execute().body(); if (ObjectUtil.isNotEmpty(responseStr)) { return JSON.parseObject(responseStr); } else { return null; } } public void sendTextMessage(String wxid, String content, Integer msgType, String fromwxid, Boolean hiddenTime) { // 全部打印 //logger.info("发送文本消息 msgType: {} wxid: {} fromwxid: {} content: {}", msgType, wxid, fromwxid, content); // 先在content顶部插入时间戳 // 因为引入了消息队列,所以在每条消息都加上时间戳 格式化成 yyyy-MM-dd HH:mm:ss if (!hiddenTime) { content = "[ " + DateUtil.format(new Date(), "HH:mm:ss yyyy-MM-dd") + " ] \r\n" + content; } // 如果是自己的微信,所有信息都加上少爷 //if (wxid.equals(super_admin_wxid) || fromwxid.equals(super_admin_wxid)) { // content = "超管: 凡神 !\r\n" + content; //} List strings = splitStringByLength(content, 4096); int count = 1; for (String string : strings) { if (strings.size() > 1) { string = "---长消息---第:" + count + "条 " + "\r" + string; } count++; //JSONObject wxList = getWxList(); //JSONObject wxBotInfo = (JSONObject) wxList.getJSONArray("result").get(0); //botWxid = wxBotInfo.getString("wxid"); // //// WxReqDate wxReqDate = createWxReqData(WXReqType.SEND_TEXT_MESSAGE); JSONObject data = new JSONObject(); //if ((msgType.equals(1))) { // jsonObject.put("wxid", wxid); // content = content; //} ////[@,wxid=对象wxid,nick=对象昵称,isAuto=true] //if ((msgType.equals(2))) { // jsonObject.put("wxid", fromwxid); // content = "[@,wxid=" + wxid + ",nick=6,isAuto=true] " + content; //} /* * { "wxid": "filehelper", "msg": "666大佬~" }*/ data.put("msg", string); data.put("wxid", wxid); wxReqDate.setData(data); // wxReqDate 转成 JSONObject JSONObject message = JSON.parseObject(JSON.toJSONString(wxReqDate)); //System.out.println(JSON.toJSONString(jsonObject)); //wxReqDate.setData(jsonObject); if (Util.isNotEmpty(wxid)) { // 把消息发送到RocketMQ,使用'wx-message'作为topic,jsonObject作为消息内容。 messageProducerService.sendMessage(message); } } } public void sendImageMessage(String wxid, String imagePath) { WxReqDate wxReqDate = createWxReqData(WXReqType.SEND_IMAGE); JSONObject data = new JSONObject(); data.put("wxid", wxid); data.put("path", imagePath); String[] split = imagePath.split("/"); data.put("fileName", split[split.length - 1]); wxReqDate.setData(data); JSONObject message = JSON.parseObject(JSON.toJSONString(wxReqDate)); if (Util.isNotEmpty(wxid)) { // 把消息发送到RocketMQ,使用'wx-message'作为topic,jsonObject作为消息内容。 messageProducerService.sendMessage(message); } } /** * { * "type": "Q0016", * "data": { * "wxid": "wxid_3sx9sjgq99kd22", * "transferid": "1000050001202207161417697440336" * } * } */ public JSONObject queRenShouKuan(String wxid, String transferid) { String botWxid = ""; JSONObject wxList = getWxList(); JSONObject wxBotInfo = (JSONObject) wxList.getJSONArray("result").get(0); botWxid = wxBotInfo.getString("wxid"); WxReqDate wxReqDate = createWxReqData(WXReqType.CONFIRM_RECEIPT); HashMap body = new HashMap<>(); body.put("wxid", wxid); body.put("transferid", transferid); JSONObject jsonObject = new JSONObject(body); wxReqDate.setData(jsonObject); String responseStr = HttpRequest.post(WX_BASE_URL + "?wxid=" + botWxid).body(JSON.toJSONString(wxReqDate)).execute().body(); logger.info("确认收款结果:responseStr: {}", responseStr); if (ObjectUtil.isNotEmpty(responseStr)) { return JSON.parseObject(responseStr); } else { return null; } } //private JSONObject sendWxReq(WxReqDate wxReqDate) { // if (wxReqDate == null) { // return null; // } else { // logger.info("wxReqDate: {}", wxReqDate); // // String responseStr = HttpRequest.post(WX_BASE_URL).body(JSON.toJSONString(wxReqDate)).execute().body(); // if (ObjectUtil.isNotEmpty(responseStr)) { // JSONObject jsonObject = JSON.parseObject(responseStr); // //WxResponse wxResponse = JSON.parseObject(responseStr, WxResponse.class); // //System.out.println(wxResponse); // //if (Objects.equals(String.valueOf(wxResponse.getCode()), "200")) { // // return wxResponse.getData(); // //} // //JSONObject jsonObject = HttpUtil.sendPost(url, wxReqDate.getData()); // return jsonObject; // } // } // // return null; // } //} public WxReqDate createWxReqData(WXReqType wxReqType) { WxReqDate wxReqDate = new WxReqDate(wxReqType.getType(), null); return wxReqDate; } @Scheduled(cron = "0 * * * * ?") public void checkWxStatus() { WxReqDate wxReqDate = createWxReqData(WXReqType.GET_WX_STATUS); JSONObject data = new JSONObject(); data.put("wxid", default_bot_wxid); wxReqDate.setData(data); String responseStr = HttpRequest.post(WX_BASE_URL).body(JSON.toJSONString(wxReqDate)).execute().body(); if (ObjectUtil.isNotEmpty(responseStr)) { JSONObject jsonObject = JSON.parseObject(responseStr); /** * { * "code": 200, * "msg": "正常", * "result": { * "startTimeStamp": "1716467892", * "startTime": "2024年5月23日20时38分12秒", * "runTime": "3分10秒", * "recv": 0, * "send": 0, * "wxNum": "DaenPro", * "nick": "小鹿\\uD83D\\uDE00\\uD83D\\uDE00摸", * "wxid": "wxid_nq6r0w9v12612" * }, * "wxid": "wxid_nq6r0w9v12612", * "port": 7799, * "pid": 18892, * "flag": "7888", * "timestamp": "1716468082967" * } * */ Integer code = jsonObject.getInteger("code"); if (code == 500) { if (sendTimes > 3) { return; } wxTsUtil.sendCriticalAlert("微信状态异常", jsonObject.getString("msg")); sendTimes++; } else if (code == 200) { sendTimes = 0; } } else { // 新建格式化日期 DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒"); wxTsUtil.sendCriticalAlert("千寻框架状态异常", dateFormat.format(new Date())); } } @Data @AllArgsConstructor @NoArgsConstructor private static class WxReqDate { //{ // "type": "X0000", // "data": {} //} private String type; private JSONObject data; } @Data @AllArgsConstructor @NoArgsConstructor public class SuperAdmin { private String wxid; private String name; // 联盟ID private String unionId; private String appKey; private String secretKey; } @Data @AllArgsConstructor @NoArgsConstructor private class WxResponse { /** * { * "code": 200, * "msg": "操作成功", * "result": {}, * "wxid": "wxid_3sq4tklb6c3121", * "port": 7305, * "pid": 12384, * "flag": "7777", * "timestamp": "1657462661814" * } */ private Integer code; private String msg; private JSONObject result; private String wxid; private Integer port; private Integer pid; private String flag; private String timestamp; } }