Files
Jarvis_java/src/main/java/cn/van/business/util/WXUtil.java
雷欧(林平凡) 01da4fc4fc 1
2024-11-15 15:50:34 +08:00

260 lines
8.6 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 cn.van.business.util;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import cn.van.business.enums.WXReqType;
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.core.env.Environment;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* @author Leo
* @version 1.0
* @create 2023/12/22 0022 上午 09:59
* @description
*/
@Component
public class WXUtil {
private static final Logger logger = LoggerFactory.getLogger(WXUtil.class);
public static final String super_admin_wxid = "wxid_ytpc72mdoskt22";
/**
* 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 Environment env;
@Autowired
public WXUtil(Environment env) {
this.env = env;
WX_BASE_URL = env.getProperty("config.WX_BASE_URL");
System.out.println("WX_BASE_URL:" + WX_BASE_URL);
}
// 获取微信列表
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 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) {
// 全部打印
logger.info("发送文本消息 msgType: " + msgType + " wxid: " + wxid + " fromwxid: " + fromwxid + " content: " + content);
List<String> strings = splitStringByLength(content, 2048);
int count = 0;
for (String string : strings) {
if (count > 0) {
string = "---长消息---第:" + count + "" + string + "\r";
}
count++;
// 如果是自己的微信,所有信息都加上少爷
if (wxid.equals(super_admin_wxid) || fromwxid.equals(super_admin_wxid)) {
string = "超管: 凡 \r\n" + string;
}
//JSONObject wxList = getWxList();
//JSONObject wxBotInfo = (JSONObject) wxList.getJSONArray("result").get(0);
//botWxid = wxBotInfo.getString("wxid");
//
////
//WxReqDate wxReqDate = createWxReqData(WXReqType.SEND_TEXT_MESSAGE);
JSONObject jsonObject = new JSONObject();
jsonObject.put("type", WXReqType.SEND_TEXT_MESSAGE.getType());
//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大佬~"
}*/
JSONObject data = new JSONObject();
data.put("msg", string);
data.put("wxid", wxid);
jsonObject.put("data", data);
//System.out.println(JSON.toJSONString(jsonObject));
//wxReqDate.setData(jsonObject);
if (Util.isNotEmpty(wxid)) {
String responseStr = HttpRequest.post(WX_BASE_URL)
.body(JSON.toJSONString(jsonObject)).execute().body();
if (ObjectUtil.isNotEmpty(responseStr)) {
JSONObject response = JSON.parseObject(responseStr);
logger.info("消息响应:" + response.toString());
//return response;
continue;
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
//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;
// }
//}
/**
* {
* "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<String, String> 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;
}
}
public WxReqDate createWxReqData(WXReqType wxReqType) {
WxReqDate wxReqDate = new WxReqDate(wxReqType.getType(), null);
return wxReqDate;
}
@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;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
private class WxReqDate {
//{
// "type": "X0000",
// "data": {}
//}
private String type;
private JSONObject data;
}
}