1
This commit is contained in:
@@ -14,8 +14,11 @@ 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.*;
|
||||
|
||||
/**
|
||||
@@ -27,45 +30,11 @@ import java.util.*;
|
||||
@Component
|
||||
public class WXUtil {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(WXUtil.class);
|
||||
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<String, SuperAdmin> super_admins = new HashMap<>();
|
||||
public static Map<String, String> jdidToWxidMap = new HashMap<>();
|
||||
|
||||
private final WxtsUtil wxTsUtil;
|
||||
private final MessageProducerService messageProducerService;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class SuperAdmin {
|
||||
private String wxid;
|
||||
private String name;
|
||||
// 联盟ID
|
||||
private String unionId;
|
||||
private String appKey;
|
||||
private String secretKey;
|
||||
}
|
||||
|
||||
// 初始化超级管理员
|
||||
public void initSuperAdmins() {
|
||||
SuperAdmin admin1 = new SuperAdmin("wxid_ytpc72mdoskt22", "凡", "2014264913", "98e21c89ae5610240ec3f5f575f86a59", "3dcb6b23a1104639ac433fd07adb6dfb");
|
||||
super_admins.put(admin1.getWxid(), admin1);
|
||||
jdidToWxidMap.put(admin1.getUnionId(), admin1.getWxid());
|
||||
|
||||
SuperAdmin admin2 = new SuperAdmin("wxid_yneqf1implxu12", "源", "2025353364", "e3c161242c8a1416fada5b5564d7ee70", "41ae9aabf03b41e6ba309682e36b323e");
|
||||
super_admins.put(admin2.getWxid(), admin2);
|
||||
jdidToWxidMap.put(admin2.getUnionId(), admin2.getWxid());
|
||||
//wxTsUtil.sendNotify("initSuperAdmins 初始化完成");
|
||||
//发送通知到微信
|
||||
sendTextMessage(default_super_admin_wxid,"Jarvis 重启完成 [亲亲][亲亲][亲亲] ", 1, default_super_admin_wxid);
|
||||
|
||||
}
|
||||
|
||||
public static String getWxidFromJdid(String jdid) {
|
||||
return jdidToWxidMap.get(jdid);
|
||||
}
|
||||
|
||||
/**
|
||||
* url http://127.0.0.1:7777/DaenWxHook/httpapi/
|
||||
* 获取微信列表 (X0000)
|
||||
@@ -97,9 +66,11 @@ public class WXUtil {
|
||||
* 发送名片(Q0025)
|
||||
*/
|
||||
public static String WX_BASE_URL;
|
||||
private final WxtsUtil wxTsUtil;
|
||||
private final MessageProducerService messageProducerService;
|
||||
public int sendTimes = 0;
|
||||
private Environment env;
|
||||
|
||||
|
||||
@Autowired
|
||||
public WXUtil(Environment env, WxtsUtil wxTsUtil, @Lazy MessageProducerService messageProducerService) {
|
||||
this.messageProducerService = messageProducerService;
|
||||
@@ -110,6 +81,36 @@ public class WXUtil {
|
||||
initSuperAdmins();
|
||||
}
|
||||
|
||||
public static String getWxidFromJdid(String jdid) {
|
||||
return jdidToWxidMap.get(jdid);
|
||||
}
|
||||
|
||||
public static List<String> splitStringByLength(String input, int length) {
|
||||
List<String> 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() {
|
||||
SuperAdmin admin1 = new SuperAdmin("wxid_ytpc72mdoskt22", "凡", "2014264913", "98e21c89ae5610240ec3f5f575f86a59", "3dcb6b23a1104639ac433fd07adb6dfb");
|
||||
super_admins.put(admin1.getWxid(), admin1);
|
||||
jdidToWxidMap.put(admin1.getUnionId(), admin1.getWxid());
|
||||
|
||||
SuperAdmin admin2 = new SuperAdmin("wxid_yneqf1implxu12", "源", "2025353364", "e3c161242c8a1416fada5b5564d7ee70", "41ae9aabf03b41e6ba309682e36b323e");
|
||||
super_admins.put(admin2.getWxid(), admin2);
|
||||
jdidToWxidMap.put(admin2.getUnionId(), admin2.getWxid());
|
||||
//wxTsUtil.sendNotify("initSuperAdmins 初始化完成");
|
||||
//发送通知到微信
|
||||
sendTextMessage(default_super_admin_wxid, "Jarvis 重启完成 [亲亲][亲亲][亲亲] ", 1, default_super_admin_wxid);
|
||||
|
||||
}
|
||||
|
||||
// 获取微信列表
|
||||
public JSONObject getWxList() {
|
||||
WxReqDate wxReqDate = createWxReqData(WXReqType.GET_WX_LIST);
|
||||
@@ -122,17 +123,6 @@ public class WXUtil {
|
||||
|
||||
}
|
||||
|
||||
public static List<String> splitStringByLength(String input, int length) {
|
||||
List<String> 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;
|
||||
}
|
||||
|
||||
// 发送文本消息 msgType 1:私聊 2:群发
|
||||
public void sendTextMessage(String wxid, String content, Integer msgType, String fromwxid) {
|
||||
// 全部打印
|
||||
@@ -159,9 +149,8 @@ public class WXUtil {
|
||||
//botWxid = wxBotInfo.getString("wxid");
|
||||
//
|
||||
////
|
||||
//WxReqDate wxReqDate = createWxReqData(WXReqType.SEND_TEXT_MESSAGE);
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("type", WXReqType.SEND_TEXT_MESSAGE.getType());
|
||||
WxReqDate wxReqDate = createWxReqData(WXReqType.SEND_TEXT_MESSAGE);
|
||||
JSONObject data = new JSONObject();
|
||||
//if ((msgType.equals(1))) {
|
||||
// jsonObject.put("wxid", wxid);
|
||||
// content = content;
|
||||
@@ -176,43 +165,37 @@ public class WXUtil {
|
||||
"wxid": "filehelper",
|
||||
"msg": "666大佬~"
|
||||
}*/
|
||||
JSONObject data = new JSONObject();
|
||||
data.put("msg", string);
|
||||
data.put("wxid", wxid);
|
||||
jsonObject.put("data", data);
|
||||
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(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));
|
||||
|
||||
//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;
|
||||
// }
|
||||
//}
|
||||
if (Util.isNotEmpty(wxid)) {
|
||||
// 把消息发送到RocketMQ,使用'wx-message'作为topic,jsonObject作为消息内容。
|
||||
messageProducerService.sendMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {
|
||||
@@ -244,6 +227,29 @@ public class WXUtil {
|
||||
|
||||
}
|
||||
|
||||
//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);
|
||||
@@ -251,6 +257,79 @@ public class WXUtil {
|
||||
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
|
||||
@@ -277,18 +356,4 @@ public class WXUtil {
|
||||
private String timestamp;
|
||||
}
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
private static class WxReqDate {
|
||||
//{
|
||||
// "type": "X0000",
|
||||
// "data": {}
|
||||
//}
|
||||
private String type;
|
||||
private JSONObject data;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user