From c0e8c4cc0ed6b80d2c297328390acd4aba2f000a Mon Sep 17 00:00:00 2001 From: chandler <1915724901@qq.com> Date: Thu, 10 Oct 2024 00:10:33 +0800 Subject: [PATCH] =?UTF-8?q?feat(0):=20[java]-[wechat-ferry-mvn]-=E5=8F=91?= =?UTF-8?q?=E9=80=81=E6=B6=88=E6=81=AF=E8=BD=AC=E5=8F=91=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=8F=8A=E6=9F=A5=E8=AF=A2=E8=81=94=E7=B3=BB=E4=BA=BA=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ferry/config/WeChatFerryProperties.java | 18 +--- .../service/impl/WeChatDllServiceImpl.java | 83 +++++++++++++++---- .../src/main/resources/application.yml | 14 ++-- 3 files changed, 75 insertions(+), 40 deletions(-) diff --git a/clients/java/wechat-ferry-mvn/src/main/java/com/wechat/ferry/config/WeChatFerryProperties.java b/clients/java/wechat-ferry-mvn/src/main/java/com/wechat/ferry/config/WeChatFerryProperties.java index e70cd8e..32e0578 100644 --- a/clients/java/wechat-ferry-mvn/src/main/java/com/wechat/ferry/config/WeChatFerryProperties.java +++ b/clients/java/wechat-ferry-mvn/src/main/java/com/wechat/ferry/config/WeChatFerryProperties.java @@ -45,24 +45,14 @@ public class WeChatFerryProperties { private List receiveMsgFwdUrls; /** - * 发送消息前转发开关 + * 发送消息转发标识 1-关闭 2-全转发 3-发送成功才转发 */ - private Boolean sendMsgFrontFwdSwitch = false; + private String sendMsgFwdFlag = "1"; /** - * 发送消息前转发URL + * 发送消息转发URL */ - private List sendMsgFrontFwdUrls; - - /** - * 发送消息后转发开关 - */ - private Boolean sendMsgBackFwdSwitch = false; - - /** - * 发送消息后转发URL - */ - private List sendMsgBackFwdUrls; + private List sendMsgFwdUrls; /** * 调用第三方服务客户端成功状态码 diff --git a/clients/java/wechat-ferry-mvn/src/main/java/com/wechat/ferry/service/impl/WeChatDllServiceImpl.java b/clients/java/wechat-ferry-mvn/src/main/java/com/wechat/ferry/service/impl/WeChatDllServiceImpl.java index 0415d48..3a2c580 100644 --- a/clients/java/wechat-ferry-mvn/src/main/java/com/wechat/ferry/service/impl/WeChatDllServiceImpl.java +++ b/clients/java/wechat-ferry-mvn/src/main/java/com/wechat/ferry/service/impl/WeChatDllServiceImpl.java @@ -9,13 +9,18 @@ import java.util.List; import java.util.Map; import java.util.function.Function; +import javax.annotation.Resource; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONObject; import com.google.protobuf.ByteString; import com.google.protobuf.InvalidProtocolBufferException; +import com.wechat.ferry.config.WeChatFerryProperties; import com.wechat.ferry.entity.proto.Wcf; import com.wechat.ferry.entity.vo.request.WxPpWcfDatabaseSqlReq; import com.wechat.ferry.entity.vo.request.WxPpWcfDatabaseTableReq; @@ -44,6 +49,7 @@ import com.wechat.ferry.enums.SexEnum; import com.wechat.ferry.enums.WxContactsTypeEnum; import com.wechat.ferry.handle.WeChatSocketClient; import com.wechat.ferry.service.WeChatDllService; +import com.wechat.ferry.utils.HttpClientUtil; import lombok.extern.slf4j.Slf4j; @@ -64,6 +70,9 @@ public class WeChatDllServiceImpl implements WeChatDllService { this.wechatSocketClient = wechatSocketClient; } + @Resource + private WeChatFerryProperties weChatFerryProperties; + @Override public Boolean loginStatus() { Boolean status = wechatSocketClient.isLogin(); @@ -152,30 +161,20 @@ public class WeChatDllServiceImpl implements WeChatDllService { mixedNoList.add("filehelper"); // 新闻 mixedNoList.add("newsapp"); - // 公众平台安全助手 - mixedNoList.add("mphelper"); - // 微信公众平台 weixingongzhong - mixedNoList.add("weixinguanhaozhushou"); // 微信团队 mixedNoList.add("weixin"); // 微信支付 wxzhifu - mixedNoList.add("gh_3dfda90e39d6"); + // mixedNoList.add("gh_3dfda90e39d6"); // 微信公开课 wx-gongkaike - mixedNoList.add("gh_c46cbbfa1de9"); - // 微信公开课 wx-gongkaike - mixedNoList.add("gh_c46cbbfa1de9"); + // mixedNoList.add("gh_c46cbbfa1de9"); // 微信运动 WeRun-WeChat - mixedNoList.add("gh_43f2581f6fd6"); + // mixedNoList.add("gh_43f2581f6fd6"); // 微信游戏 game - mixedNoList.add("gh_25d9ac85a4bc"); - // 微信游戏 game - mixedNoList.add("gh_25d9ac85a4bc"); + // mixedNoList.add("gh_25d9ac85a4bc"); // 微信开发者 - mixedNoList.add("gh_56fc3b00cc4f"); + // mixedNoList.add("gh_56fc3b00cc4f"); // 微信搜一搜 wechat_search - mixedNoList.add("gh_f08f54ae25a4"); - // 微信搜一搜 wechat_search - mixedNoList.add("gh_f08f54ae25a4"); + // mixedNoList.add("gh_f08f54ae25a4"); if (rpcContact.getWxid().endsWith(WxContactsTypeEnum.WORK.getAffix())) { // 企微 @@ -194,10 +193,13 @@ public class WeChatDllServiceImpl implements WeChatDllService { vo.setType(WxContactsTypeEnum.OFFICIAL_ACCOUNT.getCode()); vo.setTypeLabel(WxContactsTypeEnum.OFFICIAL_ACCOUNT.getName()); } else if ("wxid_2876568766325".equals(rpcContact.getWxid()) || "wxid_2965349653612".equals(rpcContact.getWxid()) - || "wxid_4302923029011".equals(rpcContact.getWxid())) { + || "wxid_4302923029011".equals(rpcContact.getWxid()) || "mphelper".equals(rpcContact.getWxid()) + || "weixinguanhaozhushou".equals(rpcContact.getWxid())) { // 应用宝 yingyongbao wxid_2876568766325 // i黑马 iheima wxid_2965349653612 // 丁香医生 DingXiangYiSheng wxid_4302923029011 + // 公众平台安全助手 mphelper + // 微信公众平台 weixingongzhong weixinguanhaozhushou vo.setType(WxContactsTypeEnum.OFFICIAL_ACCOUNT.getCode()); vo.setTypeLabel(WxContactsTypeEnum.OFFICIAL_ACCOUNT.getName()); } else { @@ -351,6 +353,15 @@ public class WeChatDllServiceImpl implements WeChatDllService { // 0 为成功,其他失败 int state = wechatSocketClient.sendText(request.getMsgText(), request.getRecipient(), atUser); log.info("[发送消息]-[文本消息]-处理结束"); + // 转发处理 + String stringJson = JSON.toJSONString(request); + if ("2".equals(weChatFerryProperties.getSendMsgFwdFlag())) { + // 不管消息是否发送成功均转发 + sendMsgForward(stringJson); + } else if ("3".equals(weChatFerryProperties.getSendMsgFwdFlag()) && 0 == state) { + // 发送成功才转发 + sendMsgForward(stringJson); + } return null; } @@ -440,4 +451,42 @@ public class WeChatDllServiceImpl implements WeChatDllService { return atUserStr; } + private void sendMsgForward(String jsonString) { + // 开启转发,且转发地址不为空 + if (!CollectionUtils.isEmpty(weChatFerryProperties.getSendMsgFwdUrls())) { + for (String receiveMsgFwdUrl : weChatFerryProperties.getSendMsgFwdUrls()) { + if (!receiveMsgFwdUrl.startsWith("http")) { + continue; + } + try { + String responseStr = HttpClientUtil.doPostJson(receiveMsgFwdUrl, jsonString); + if (judgeSuccess(responseStr)) { + log.error("[发送消息]-消息转发外部接口,获取响应状态失败!-URL:{}", receiveMsgFwdUrl); + } + log.debug("[发送消息]-[转发接收到的消息]-转发消息至:{}", receiveMsgFwdUrl); + } catch (Exception e) { + log.error("[发送消息]-消息转发接口[{}]服务异常:", receiveMsgFwdUrl, e); + } + } + } + } + + private Boolean judgeSuccess(String responseStr) { + // 默认为通过 + boolean passFlag = false; + if (!ObjectUtils.isEmpty(responseStr)) { + JSONObject jSONObject = JSONObject.parseObject(responseStr); + if (!ObjectUtils.isEmpty(jSONObject) && !CollectionUtils.isEmpty(weChatFerryProperties.getThirdPartyOkCodes())) { + Map codeMap = weChatFerryProperties.getThirdPartyOkCodes(); + for (Map.Entry entry : codeMap.entrySet()) { + if (!ObjectUtils.isEmpty(jSONObject.get(entry.getKey())) && jSONObject.get(entry.getKey()).equals(entry.getValue())) { + passFlag = true; + break; + } + } + } + } + return passFlag; + } + } diff --git a/clients/java/wechat-ferry-mvn/src/main/resources/application.yml b/clients/java/wechat-ferry-mvn/src/main/resources/application.yml index c809c49..eef6f4d 100644 --- a/clients/java/wechat-ferry-mvn/src/main/resources/application.yml +++ b/clients/java/wechat-ferry-mvn/src/main/resources/application.yml @@ -39,17 +39,13 @@ wechat: # 接收消息转发URL receive-msg-fwd-urls: - http://localhost:9001/msg - # 发送消息前转发开关 - send-msg-front-fwd-switch: false - # 发送消息前转发URL - send-msg-front-fwd-urls: - - http://localhost:9001/msg - # 发送消息后转发开关 - send-msg-back-fwd-switch: false - # 发送消息后转发URL - send-msg-back-fwd-urls: + # 发送消息转发标识 1-关闭 2-全转发 3-发送成功才转发 + send-msg-fwd-flag: '1' + # 发送消息转发URL + send-msg-fwd-urls: - http://localhost:9001/msg # 调用第三方服务客户端成功状态码 third-party-ok-codes: # key:状态码字段 val:状态码值 code: '200' +