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(); + } }