From 1aa7182a9acdd2d5e980b51e661d927bff5a61b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=B6=9B?= <2450572350@qq.com> Date: Fri, 2 Jun 2023 14:22:41 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=9A=82=E6=97=B6=E4=B8=8D=E5=85=B3?= =?UTF-8?q?=E9=97=ADhook=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/example/wxhk/tcp/vertx/InitWeChat.java | 5 ++--- .../test/java/com/example/wxhk/util/HttpSendUtilTest.java | 4 ++++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/java_client/src/main/java/com/example/wxhk/tcp/vertx/InitWeChat.java b/java_client/src/main/java/com/example/wxhk/tcp/vertx/InitWeChat.java index 9c5eab5..fb3a231 100644 --- a/java_client/src/main/java/com/example/wxhk/tcp/vertx/InitWeChat.java +++ b/java_client/src/main/java/com/example/wxhk/tcp/vertx/InitWeChat.java @@ -150,9 +150,8 @@ public class InitWeChat implements CommandLineRunner { } }); - Runtime.getRuntime().addShutdownHook(new Thread(() -> { - HttpSyncUtil.exec(HttpAsyncUtil.Type.关闭hook, new JsonObject()); - })); + // FIXME: 2023/6/2 程序结束后关闭hook会偶尔出现微信闪退情况,暂时禁用 +// Runtime.getRuntime().addShutdownHook(new Thread(HttpSendUtil::关闭hook)); //netstat -aon|findstr "端口号" // c.exe -I 4568 -p D:\exec\wxhelper.dll -m 4568 } diff --git a/java_client/src/test/java/com/example/wxhk/util/HttpSendUtilTest.java b/java_client/src/test/java/com/example/wxhk/util/HttpSendUtilTest.java index 1b384e0..442acae 100644 --- a/java_client/src/test/java/com/example/wxhk/util/HttpSendUtilTest.java +++ b/java_client/src/test/java/com/example/wxhk/util/HttpSendUtilTest.java @@ -32,6 +32,10 @@ class HttpSendUtilTest { void 开启hook() { } + @Test + void 关闭ook() { + HttpSendUtil.关闭hook(); + } @Test void 获取群成员() { From 6062a75b0228efefd3d968ede38af2d625583091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=B6=9B?= <2450572350@qq.com> Date: Tue, 6 Jun 2023 16:48:12 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=8F=90=E5=8F=96=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E7=B1=BB,=E5=8F=AA=E9=9C=80=E8=A6=81=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=8D=B3=E5=8F=AF=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/wxhk/constant/WxMsgType.java | 2 + .../wxhk/model/dto/PayoutInformation.java | 21 ++++++ .../com/example/wxhk/msg/WxMsgHandle.java | 73 +++++++++---------- .../com/example/wxhk/server/WxSmgServer.java | 30 ++++++++ .../wxhk/server/impl/WxSmgServerImpl.java | 66 +++++++++++++++++ .../com/example/wxhk/util/HttpSendUtil.java | 5 ++ .../java/com/example/wxhk/tcp/XmlTest.java | 8 ++ 7 files changed, 167 insertions(+), 38 deletions(-) create mode 100644 java_client/src/main/java/com/example/wxhk/model/dto/PayoutInformation.java create mode 100644 java_client/src/main/java/com/example/wxhk/server/WxSmgServer.java create mode 100644 java_client/src/main/java/com/example/wxhk/server/impl/WxSmgServerImpl.java diff --git a/java_client/src/main/java/com/example/wxhk/constant/WxMsgType.java b/java_client/src/main/java/com/example/wxhk/constant/WxMsgType.java index d46ffde..de9f7ad 100644 --- a/java_client/src/main/java/com/example/wxhk/constant/WxMsgType.java +++ b/java_client/src/main/java/com/example/wxhk/constant/WxMsgType.java @@ -17,6 +17,8 @@ public enum WxMsgType { 表情(47), 转账和收款(49), 收到转账之后或者文件助手等信息(51), + + 入群(10000), /** * 扫码触发,会触发2次, 有一次有编号,一次没有,还有登陆之后也有,很多情况都会调用这个 */ diff --git a/java_client/src/main/java/com/example/wxhk/model/dto/PayoutInformation.java b/java_client/src/main/java/com/example/wxhk/model/dto/PayoutInformation.java new file mode 100644 index 0000000..5ed4396 --- /dev/null +++ b/java_client/src/main/java/com/example/wxhk/model/dto/PayoutInformation.java @@ -0,0 +1,21 @@ +package com.example.wxhk.model.dto; + +import java.math.BigDecimal; + +/** + * 支付信息 + * + * @author wt + * @param receiverUsername 付款人 + * @param decimal 收款金额 + * @param remark 备注 + * @param transcationid + * @param transferid + * @date 2023/06/06 + */ +public record PayoutInformation(String receiverUsername, BigDecimal decimal, String remark,String transcationid,String transferid) implements java.io.Serializable { + + public PayoutInformation(String receiverUsername, BigDecimal decimal, String remark) { + this(receiverUsername, decimal, remark, null, null); + } +} \ No newline at end of file diff --git a/java_client/src/main/java/com/example/wxhk/msg/WxMsgHandle.java b/java_client/src/main/java/com/example/wxhk/msg/WxMsgHandle.java index b43769b..a1453ed 100644 --- a/java_client/src/main/java/com/example/wxhk/msg/WxMsgHandle.java +++ b/java_client/src/main/java/com/example/wxhk/msg/WxMsgHandle.java @@ -2,15 +2,13 @@ package com.example.wxhk.msg; import com.example.wxhk.constant.WxMsgType; import com.example.wxhk.model.PrivateChatMsg; +import com.example.wxhk.model.dto.PayoutInformation; +import com.example.wxhk.server.WxSmgServer; import com.example.wxhk.tcp.vertx.InitWeChat; -import com.example.wxhk.util.HttpAsyncUtil; -import com.example.wxhk.util.HttpSendUtil; -import com.example.wxhk.util.HttpSyncUtil; -import io.vertx.core.json.JsonObject; import jakarta.annotation.PostConstruct; -import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.util.XmlUtil; import org.dromara.hutool.log.Log; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -20,7 +18,6 @@ import org.w3c.dom.NodeList; import java.math.BigDecimal; import java.util.Iterator; import java.util.Map; -import java.util.Objects; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.locks.ReentrantReadWriteLock; @@ -38,40 +35,34 @@ public class WxMsgHandle { */ public static ConcurrentHashMap collection_code_caching = new ConcurrentHashMap<>(); + + public static WxSmgServer wxSmgServer; /** * 看 */ public static final ReentrantReadWriteLock LOOK = new ReentrantReadWriteLock(); + @Autowired + public void setWxSmgServer(WxSmgServer wxSmgServer) { + WxMsgHandle.wxSmgServer = wxSmgServer; + } @PostConstruct public void init() { add(chatMsg -> { - if (Objects.equals(chatMsg.getIsSendMsg(), 1) && Objects.equals(chatMsg.getIsSendByPhone(), 1)) { - log.info("手机端对:{}发出:{}", chatMsg.getFromUser(), chatMsg.getContent()); - return 1; - } - return 1; + wxSmgServer.私聊(chatMsg); + return null; }, WxMsgType.私聊信息); add(chatMsg -> { - if (FILEHELPER.equals(chatMsg.getFromUser())) { - log.info("文件助手:{},", chatMsg.getContent()); - } + wxSmgServer.文件助手(chatMsg); return 1; }, WxMsgType.收到转账之后或者文件助手等信息); add(chatMsg -> { - if (FILEHELPER.equals(chatMsg.getFromUser())) { - Document document = XmlUtil.parseXml(chatMsg.getContent()); - Element documentElement = document.getDocumentElement(); - String username = documentElement.getAttribute("username"); - if (StrUtil.isNotBlank(username)) { - HttpSendUtil.发送文本(username); - } - } + wxSmgServer.收到名片(chatMsg); return 1; }, WxMsgType.收到名片); add(chatMsg -> { - HttpSendUtil.通过好友请求(chatMsg); + wxSmgServer.收到好友请求(chatMsg); return 1; }, WxMsgType.好友请求);// 好友请求 add(chatMsg -> { @@ -148,7 +139,7 @@ public class WxMsgHandle { String substring = monery.substring(1); BigDecimal decimal = new BigDecimal(substring); log.info("扫码收款:{},付款人:{},付款备注:{}", decimal.stripTrailingZeros().toPlainString(), next.getValue(), remark); - HttpSendUtil.发送文本(next.getValue(), StrUtil.format("扫码收款:{},备注:{}", decimal.stripTrailingZeros().toPlainString(), remark)); + wxSmgServer.扫码收款(new PayoutInformation(next.getValue(),decimal,remark)); iterator.remove(); return false; } @@ -174,18 +165,18 @@ public class WxMsgHandle { String remark = documentElement.getElementsByTagName("pay_memo").item(0).getTextContent(); String monery = documentElement.getElementsByTagName("feedesc").item(0).getTextContent(); String receiver_username = documentElement.getElementsByTagName("receiver_username").item(0).getTextContent(); - if (InitWeChat.WXID_MAP.contains(receiver_username)) { - // 如果是自己转出去的,则不需要解析了 - return false; + // 如果是机器人发出的,则跳过解析 + if (InitWeChat.WXID_MAP.contains(receiver_username) ) { + return false; } - if (monery.startsWith("¥")) { String substring = monery.substring(1); BigDecimal decimal = new BigDecimal(substring); - log.info("收款:{},付款人:{},付款备注:{}", decimal.stripTrailingZeros().toPlainString(), receiver_username, remark); - HttpSendUtil.发送文本(receiver_username, StrUtil.format("收到款项:{},备注:{}", decimal.stripTrailingZeros().toPlainString(), remark)); + log.info("收款:{},付款人:{},付款备注:{}", decimal.stripTrailingZeros().toPlainString(), chatMsg.getFromUser(), remark); + wxSmgServer.收款之后(new PayoutInformation(chatMsg.getFromUser(), decimal, remark)); return false; - } + }; + } } } catch (Exception e) { @@ -207,7 +198,7 @@ public class WxMsgHandle { String content = chatMsg.getContent(); Document document = XmlUtil.parseXml(content); NodeList paysubtype1 = document.getElementsByTagName("paysubtype"); - if(paysubtype1.getLength()==0){ + if (paysubtype1.getLength() == 0) { return true; } Node paysubtype = paysubtype1.item(0); @@ -218,12 +209,18 @@ public class WxMsgHandle { // 如果不是机器人收款,则认为不需要解析了,大概率是机器人自己发出去的 return false; } - Node transcationid = document.getDocumentElement().getElementsByTagName("transcationid").item(0); - Node transferid = document.getDocumentElement().getElementsByTagName("transferid").item(0); - HttpSyncUtil.exec(HttpAsyncUtil.Type.确认收款, new JsonObject().put("wxid", chatMsg.getFromUser()) - .put("transcationId", transcationid.getTextContent()) - .put("transferId", transferid.getTextContent())); - return false; + + String remark = document.getElementsByTagName("pay_memo").item(0).getTextContent(); + String monery = document.getElementsByTagName("feedesc").item(0).getTextContent(); + String receiver_username = document.getElementsByTagName("receiver_username").item(0).getTextContent(); + if (monery.startsWith("¥")) { + String substring = monery.substring(1); + BigDecimal decimal = new BigDecimal(substring); + Node transcationid = document.getDocumentElement().getElementsByTagName("transcationid").item(0); + Node transferid = document.getDocumentElement().getElementsByTagName("transferid").item(0); + wxSmgServer.接到收款(new PayoutInformation(chatMsg.getFromUser(), decimal, remark, transcationid.getTextContent(), transferid.getTextContent())); + return false; + } } } catch (Exception e) { diff --git a/java_client/src/main/java/com/example/wxhk/server/WxSmgServer.java b/java_client/src/main/java/com/example/wxhk/server/WxSmgServer.java new file mode 100644 index 0000000..fe2bd3a --- /dev/null +++ b/java_client/src/main/java/com/example/wxhk/server/WxSmgServer.java @@ -0,0 +1,30 @@ +package com.example.wxhk.server; + +import com.example.wxhk.model.PrivateChatMsg; +import com.example.wxhk.model.dto.PayoutInformation; + +/** + * 微信消息处理提取 + * @author wt + * @date 2023/06/06 + */ +public interface WxSmgServer { + /** + * 接到收款 + * + * @param payoutInformation 支付信息 + */ + void 接到收款(PayoutInformation payoutInformation); + + void 收款之后(PayoutInformation pay); + + void 私聊(PrivateChatMsg chatMsg); + + void 文件助手(PrivateChatMsg chatMsg); + + void 收到名片(PrivateChatMsg chatMsg); + + void 收到好友请求(PrivateChatMsg chatMsg); + + void 扫码收款(PayoutInformation payoutInformation); +} diff --git a/java_client/src/main/java/com/example/wxhk/server/impl/WxSmgServerImpl.java b/java_client/src/main/java/com/example/wxhk/server/impl/WxSmgServerImpl.java new file mode 100644 index 0000000..fcae972 --- /dev/null +++ b/java_client/src/main/java/com/example/wxhk/server/impl/WxSmgServerImpl.java @@ -0,0 +1,66 @@ +package com.example.wxhk.server.impl; + +import com.example.wxhk.model.PrivateChatMsg; +import com.example.wxhk.model.dto.PayoutInformation; +import com.example.wxhk.model.request.ConfirmThePayment; +import com.example.wxhk.util.HttpSendUtil; +import org.dromara.hutool.core.text.StrUtil; +import org.dromara.hutool.core.util.XmlUtil; +import org.dromara.hutool.log.Log; +import org.springframework.stereotype.Service; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import java.util.Objects; + +@Service +public class WxSmgServerImpl implements com.example.wxhk.server.WxSmgServer { + + protected static final Log log=Log.get(); + + public static final String FILEHELPER = "filehelper"; + @Override + public void 接到收款(PayoutInformation payoutInformation) { + HttpSendUtil.确认收款(new ConfirmThePayment().setWxid(payoutInformation.receiverUsername()).setTranscationId(payoutInformation.transcationid()).setTransferId(payoutInformation.transferid())); + } + @Override + public void 收款之后(PayoutInformation pay) { + HttpSendUtil.发送文本(pay.receiverUsername(), StrUtil.format("收到款项:{},备注:{}", pay.decimal().stripTrailingZeros().toPlainString(), pay.remark())); + } + + @Override + public void 私聊(PrivateChatMsg chatMsg) { + if (Objects.equals(chatMsg.getIsSendMsg(), 1) && Objects.equals(chatMsg.getIsSendByPhone(), 1)) { + log.info("手机端对:{}发出:{}", chatMsg.getFromUser(), chatMsg.getContent()); + } + } + + @Override + public void 文件助手(PrivateChatMsg chatMsg) { + if (FILEHELPER.equals(chatMsg.getFromUser())) { + log.info("文件助手:{},", chatMsg.getContent()); + } + } + + @Override + public void 收到名片(PrivateChatMsg chatMsg) { + if (FILEHELPER.equals(chatMsg.getFromUser())) { + Document document = XmlUtil.parseXml(chatMsg.getContent()); + Element documentElement = document.getDocumentElement(); + String username = documentElement.getAttribute("username"); + if (StrUtil.isNotBlank(username)) { + HttpSendUtil.发送文本(username); + } + } + } + + @Override + public void 收到好友请求(PrivateChatMsg chatMsg) { + HttpSendUtil.通过好友请求(chatMsg); + } + + @Override + public void 扫码收款(PayoutInformation payoutInformation) { + HttpSendUtil.发送文本(payoutInformation.receiverUsername(), StrUtil.format("扫码收款:{},备注:{}", payoutInformation.decimal().stripTrailingZeros().toPlainString(), payoutInformation.remark())); + } +} diff --git a/java_client/src/main/java/com/example/wxhk/util/HttpSendUtil.java b/java_client/src/main/java/com/example/wxhk/util/HttpSendUtil.java index d1eb918..281db83 100644 --- a/java_client/src/main/java/com/example/wxhk/util/HttpSendUtil.java +++ b/java_client/src/main/java/com/example/wxhk/util/HttpSendUtil.java @@ -117,6 +117,11 @@ public class HttpSendUtil { } + public static JsonObject 确认收款(ConfirmThePayment payment){ + return HttpSyncUtil.exec(HttpAsyncUtil.Type.确认收款, payment.toJson()); + } + + @Deprecated public static com.example.wxhk.infe.SendMsg of(HttpAsyncUtil.Type type) { switch (type) { diff --git a/java_client/src/test/java/com/example/wxhk/tcp/XmlTest.java b/java_client/src/test/java/com/example/wxhk/tcp/XmlTest.java index a26aeef..78587d1 100644 --- a/java_client/src/test/java/com/example/wxhk/tcp/XmlTest.java +++ b/java_client/src/test/java/com/example/wxhk/tcp/XmlTest.java @@ -81,4 +81,12 @@ public class XmlTest { String alias = documentElement.getAttribute("alias"); Console.log(alias,username); } + + @Test + void 公众号(){ + String con = "{\"content\":\"请问您指的是账单的什么问题呢?请回复数字选择:\\n1.如何导出微信账单记录\\n2.如何查看已删除账单\\n3.怎么删除交易记录\",\"fromGroup\":\"gh_3dfda90e39d6\",\"fromUser\":\"gh_3dfda90e39d6\",\"isSendMsg\":0,\"msgId\":9025889923001869810,\"pid\":9920,\"sign\":\"0a66d3dab6b64ca646f512cd278d5f3d\",\"signature\":\"\\n\\t3\\n\\t\\n\\t\\t3\\n\\t\\t\\n\\t\\n\\t1\\n\\t1\\n\\t13\\n\\tv1_iJNyfNLb\\n\\t\\n\\t\\t<![CDATA[]]>\\n\\t\\n\\n\",\"time\":\"2023-06-05 11:32:17\",\"timestamp\":1685935937,\"type\":1}"; + PrivateChatMsg privateChatMsg = new JsonObject(con).mapTo(PrivateChatMsg.class); + Document document = XmlUtil.parseXml(privateChatMsg.getSignature()); + Element documentElement = document.getDocumentElement(); + } } From 88e5c8fb9cb5b1e74827b68cc3594294709d48a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=B6=9B?= <2450572350@qq.com> Date: Tue, 6 Jun 2023 16:50:52 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=8F=90=E5=8F=96=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E7=B1=BB,=E5=8F=AA=E9=9C=80=E8=A6=81=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=8D=B3=E5=8F=AF=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- java_client/README.md | 3 +++ .../src/main/java/com/example/wxhk/msg/WxMsgHandle.java | 4 +++- .../java/com/example/wxhk/server/impl/WxSmgServerImpl.java | 4 +--- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/java_client/README.md b/java_client/README.md index 8e2271c..590f061 100644 --- a/java_client/README.md +++ b/java_client/README.md @@ -11,6 +11,9 @@ com.example.wxhk.tcp.vertx.VertxTcp 这个是tcp服务端,接受信息 com.example.wxhk.tcp.vertx.InitWeChat 微信环境初始化 com.example.wxhk.tcp.vertx.ArrHandle 循环消息处理 + +com.example.wxhk.server.WxSmgServer 为消息处理接口,实现其中的方法即可 + ![image](https://github.com/sglmsn/wxhelper/assets/36943585/59d49401-a492-46a9-8ed9-dab7fb1822b4) diff --git a/java_client/src/main/java/com/example/wxhk/msg/WxMsgHandle.java b/java_client/src/main/java/com/example/wxhk/msg/WxMsgHandle.java index a1453ed..dbfc8aa 100644 --- a/java_client/src/main/java/com/example/wxhk/msg/WxMsgHandle.java +++ b/java_client/src/main/java/com/example/wxhk/msg/WxMsgHandle.java @@ -54,7 +54,9 @@ public class WxMsgHandle { return null; }, WxMsgType.私聊信息); add(chatMsg -> { - wxSmgServer.文件助手(chatMsg); + if (FILEHELPER.equals(chatMsg.getFromUser())) { + wxSmgServer.文件助手(chatMsg); + } return 1; }, WxMsgType.收到转账之后或者文件助手等信息); add(chatMsg -> { diff --git a/java_client/src/main/java/com/example/wxhk/server/impl/WxSmgServerImpl.java b/java_client/src/main/java/com/example/wxhk/server/impl/WxSmgServerImpl.java index fcae972..3bfebd9 100644 --- a/java_client/src/main/java/com/example/wxhk/server/impl/WxSmgServerImpl.java +++ b/java_client/src/main/java/com/example/wxhk/server/impl/WxSmgServerImpl.java @@ -37,9 +37,7 @@ public class WxSmgServerImpl implements com.example.wxhk.server.WxSmgServer { @Override public void 文件助手(PrivateChatMsg chatMsg) { - if (FILEHELPER.equals(chatMsg.getFromUser())) { - log.info("文件助手:{},", chatMsg.getContent()); - } + } @Override