From bf9c0571f8f995a6aaf8424a6027e51fe8590616 Mon Sep 17 00:00:00 2001 From: Binary Wang <binarywang@gmail.com> Date: Sun, 30 Oct 2022 14:31:34 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=A0=BC=E5=BC=8F=E5=8C=96=E8=A7=84?= =?UTF-8?q?=E8=8C=83=E5=8C=96=E9=83=A8=E5=88=86=E6=B5=8B=E8=AF=95=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weixin/cp/api/WxCpMsgAuditTest.java | 253 +++++++++--------- .../src/test/resources/test-config.sample.xml | 1 + 2 files changed, 126 insertions(+), 128 deletions(-) diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpMsgAuditTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpMsgAuditTest.java index 0822bfb49..573b997f7 100644 --- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpMsgAuditTest.java +++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpMsgAuditTest.java @@ -25,9 +25,10 @@ import java.util.List; /** * 企业微信会话内容存档测试类. - * 官方文档:https://developer.work.weixin.qq.com/document/path/91360 + * <a href="https://developer.work.weixin.qq.com/document/path/91360">官方文档</a> * - * @author <a href="https://github.com/0katekate0">Wang_Wong</a> created on 2022-01-17 + * @author <a href="https://github.com/0katekate0">Wang_Wong</a> + * created on 2022-01-17 */ @Slf4j public class WxCpMsgAuditTest { @@ -37,27 +38,22 @@ public class WxCpMsgAuditTest { /** * Test. - * - * @throws Exception the exception */ // com.binarywang.spring.starter.wxjava.cp.config.WxCpServiceAutoConfiguration // WxCpServiceImpl.getAccessToken() - @BeforeTest - private void initCpservice() { - if(cpService == null) { + private void initCpService() { + if (cpService == null) { InputStream inputStream = ClassLoader.getSystemResourceAsStream("test-config.xml"); - WxCpDemoInMemoryConfigStorage config = WxCpDemoInMemoryConfigStorage.fromXml(inputStream); - config.setMsgAuditLibPath("/E:/IDEA_WORKSPACE/saisc/crs-member-java/target/classes/wework/libcrypto-1_1-x64.dll,libssl-1_1-x64.dll,libcurl-x64.dll,WeWorkFinanceSdk.dll"); - wxCpConfigStorage = config; + wxCpConfigStorage = WxCpDemoInMemoryConfigStorage.fromXml(inputStream); cpService = new WxCpServiceImpl(); - cpService.setWxCpConfigStorage(config); + cpService.setWxCpConfigStorage(this.wxCpConfigStorage); } } @Test public void test() throws Exception { - /** + /* * 客户同意进行聊天内容存档事件回调 * 配置了客户联系功能的成员添加外部联系人同意进行聊天内容存档时,回调该事件。 * @@ -79,7 +75,7 @@ public class WxCpMsgAuditTest { msgAuditApprovedXmlMsg.setAllFieldsMap(XmlUtils.xml2Map(msgAuditApprovedXml)); log.info("msgAuditApprovedXmlMsg:{}", JSON.toString(msgAuditApprovedXmlMsg)); - /** + /* * 产生会话回调事件 * 为了提升企业会话存档的使用性能,降低无效的轮询次数。 * 当企业收到或发送新消息时,企业微信可以以事件的形式推送到企业指定的url。回调间隔为15秒,在15秒内若有消息则触发回调,若无消息则不会触发回调。 @@ -99,13 +95,13 @@ public class WxCpMsgAuditTest { msgAuditNotifyXmlMsg.setAllFieldsMap(XmlUtils.xml2Map(msgAuditNotifyXml)); log.info("msgAuditNotifyXmlMsg:{}", JSON.toString(msgAuditNotifyXmlMsg)); - /** + /* * 增加变更事件类型:产生会话回调事件 */ String msgauditNotify = WxCpConsts.EventType.MSGAUDIT_NOTIFY; - /** + /* * 仔细配置: * <xml> * <corpId>ww45xxx88865xxx</corpId> @@ -161,12 +157,12 @@ public class WxCpMsgAuditTest { * openssl rsa -in private_key.pem -pubout -out public_key.pem * / - /** + /* * 建议放到redis,本次请求获取消息记录开始的seq值。首次访问填写0,非首次使用上次企业微信返回的最大seq。允许从任意seq重入拉取。 */ long seq = 0L; - /** + /* * 图片,语音,视频,表情,文件,音频存档消息,音频共享文档消息调用 获取媒体消息 */ List<String> mediaType = Arrays.asList("image", "voice", "video", "emotion", "file", @@ -201,7 +197,7 @@ public class WxCpMsgAuditTest { chatData, 2); log.info("获取消息数据为:{}", decryptData.toJson()); - /** + /* * 注意: * 根据上面返回的文件类型来获取媒体文件, * 不同的文件类型,拼接好存放文件的绝对路径,写入文件流,获取媒体文件。(拼接绝对文件路径的原因,以便上传到腾讯云或阿里云对象存储) @@ -278,7 +274,7 @@ public class WxCpMsgAuditTest { return; } - /** + /* * 拉取媒体文件 * * 注意: @@ -300,7 +296,7 @@ public class WxCpMsgAuditTest { } - /** + /* * 文本 */ // String text = "{\"msgid\":\"CAQQluDa4QUY0On2rYSAgAMgzPrShAE=\",\"action\":\"send\",\"from\":\"XuJinSheng\", @@ -313,7 +309,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelText.toJson()); - /** + /* * 图片 */ String image = "{\"msgid\":\"CAQQvPnc4QUY0On2rYSAgAMgooLa0Q8=\",\"action\":\"send\",\"from\":\"XuJinSheng\"," + @@ -325,7 +321,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelImage.toJson()); - /** + /* * 撤回消息 */ String revoke = "{\"msgid\":\"15775510700152506326_1603875615\",\"action\":\"recall\",\"from\":\"kenshin\"," + @@ -335,7 +331,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelRevoke.toJson()); - /** + /* * 同意会话聊天内容 */ String agree = "{\"msgid\":\"8891446340739254950_1603875826\",\"action\":\"send\"," + @@ -352,7 +348,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelDisagree.toJson()); - /** + /* * 语音 */ String voice = "{\"msgid\":\"10958372969718811103_1603875609\",\"action\":\"send\"," + @@ -365,7 +361,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelVoice.toJson()); - /** + /* * 视频 */ String video = "{\"msgid\":\"17955920891003447432_1603875627\",\"action\":\"send\",\"from\":\"kenshin\"," + @@ -378,7 +374,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelVideo.toJson()); - /** + /* * 名片 */ String card = "{\"msgid\":\"13714216591700685558_1603875680\",\"action\":\"send\",\"from\":\"kenshin\"," + @@ -388,7 +384,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelCard.toJson()); - /** + /* * 位置 */ String location = "{\"msgid\":\"2641513858500683770_1603876152\",\"action\":\"send\",\"from\":\"icefog\"," + @@ -399,7 +395,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelLocation.toJson()); - /** + /* * 表情 */ String emotion = "{\"msgid\":\"6623217619416669654_1603875612\",\"action\":\"send\",\"from\":\"icef\"," + @@ -411,7 +407,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelEmotion.toJson()); - /** + /* * 文件 */ String file = "{\"msgid\":\"18039699423706571225_1603875608\",\"action\":\"send\",\"from\":\"kens\"," + @@ -424,7 +420,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelFile.toJson()); - /** + /* * 链接 */ String link = "{\"msgid\":\"11788441727514772650_1603875624\",\"action\":\"send\",\"from\":\"kenshin\"," + @@ -435,7 +431,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelLink.toJson()); - /** + /* * 小程序消息 */ String weapp = "{\"msgid\":\"11930598857592605935_1603875608\",\"action\":\"send\",\"from\":\"kens\"," + @@ -446,7 +442,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelWeapp.toJson()); - /** + /* * 会话记录消息 */ String chatrecord = "{\"msgid\":\"11354299838102555191_1603875658\",\"action\":\"send\",\"from\":\"ken\"," + @@ -458,7 +454,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelChatRecord.toJson()); - /** + /* * 填表消息 */ String collect = "{\"msgid\":\"2500536226619379797_1576034482\",\"action\":\"send\",\"from\":\"nick\"," + @@ -471,7 +467,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelCollect.toJson()); - /** + /* * 红包消息 */ String redpacket = "{\"msgid\":\"333590477316965370_1603877439\",\"action\":\"send\",\"from\":\"kens\"," + @@ -481,7 +477,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelRedpacket.toJson()); - /** + /* * 会议邀请信息 */ String meeting = "{\"msgid\":\"5935786683775673543_1603877328\",\"action\":\"send\",\"from\":\"ken\"," + @@ -492,7 +488,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelMeeting.toJson()); - /** + /* * 切换企业日志 */ String switchlog = "{\"msgid\":\"125289002219525886280\",\"action\":\"switch\",\"time\":1554119421840," + @@ -501,7 +497,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelSwitchLog.toJson()); - /** + /* * 在线文档消息 */ String docMsg = "{\"msgid\":\"9732089160923053207_1603877765\",\"action\":\"send\",\"from\":\"ken\"," + @@ -512,7 +508,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelDocMsg.toJson()); - /** + /* * MarkDown格式消息 */ String markDown = "{\"msgid\":\"7546287934688259248_1603875715\",\"action\":\"send\",\"from\":\"ken\"," + @@ -522,7 +518,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelMarkDown.toJson()); - /** + /* * 图文消息 */ String news = "{\"msgid\":\"118732825779547782215\",\"action\":\"send\",\"from\":\"kens\",\"tolist\":[\"icef\"," + @@ -533,7 +529,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelNews.toJson()); - /** + /* * 日程消息 */ String calendar = "{\"msgid\":\"2345881211604379705_1603877680\",\"action\":\"send\",\"from\":\"ken\"," + @@ -545,7 +541,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelCalendar.toJson()); - /** + /* * 混合消息 */ String mixed = "{\"msgid\":\"DAQQluDa4QUY0On4kYSABAMgzPrShAE=\",\"action\":\"send\",\"from\":\"HeMiao\"," + @@ -564,7 +560,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelMixed.toJson()); - /** + /* * 音频存档消息 */ String meetingVoiceCall = "{\"msgid\":\"17952229780246929345_1594197637\",\"action\":\"send\"," + @@ -577,7 +573,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelMeetingVoiceCall.toJson()); - /** + /* * 音频共享文档消息 */ String voipDocShare = "{\"msgid\":\"16527954622422422847_1594199256\",\"action\":\"send\"," + @@ -591,7 +587,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelVoipDocShare.toJson()); - /** + /* * 互通红包消息 */ String externalRedpacket = "{\"msgid\":\"8632214264349267353_1603786184\",\"action\":\"send\"," + @@ -603,7 +599,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelExternalRedpacket.toJson()); - /** + /* * 视频号消息 */ String sphfeed = "{\"msgid\":\"5702551662099334532_1619511584_external\",\"action\":\"send\"," + @@ -614,7 +610,7 @@ public class WxCpMsgAuditTest { log.info("数据为:" + modelSphFeed.toJson()); - /** + /* * 获取会话内容存档开启成员列表 */ List<String> permitUserList = cpService.getMsgAuditService().getPermitUserList(null); @@ -623,7 +619,7 @@ public class WxCpMsgAuditTest { ArrayList<WxCpCheckAgreeRequest.Info> userList = Lists.newArrayList(); WxCpCheckAgreeRequest checkAgreeRequest = new WxCpCheckAgreeRequest(); - /** + /* * 获取会话同意情况 */ WxCpCheckAgreeRequest.Info info = new WxCpCheckAgreeRequest.Info(); @@ -638,20 +634,21 @@ public class WxCpMsgAuditTest { log.info(wxCpAgreeInfo.toJson()); - /** + /* * 获取会话内容存档内部群信息 */ WxCpGroupChat room = cpService.getMsgAuditService().getGroupChat("wrOQpTDwAAyPl84GBJ40W5eWxWtixSCA"); log.info(room.toJson()); - /** + /* * 获取access_token * https://developer.work.weixin.qq.com/document/path/91039 * https://www.jianshu.com/p/dde171887d63 */ String getUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s"; - String data = cpService.get(String.format(getUrl, wxCpConfigStorage.getCorpId(), wxCpConfigStorage.getCorpSecret()), null); + String data = cpService.get(String.format(getUrl, wxCpConfigStorage.getCorpId(), + wxCpConfigStorage.getCorpSecret()), null); } @@ -662,97 +659,97 @@ public class WxCpMsgAuditTest { WxCpChatDatas chatDatas = msgAuditService.getChatDatas(0L, 100L, null, null, 10); for (WxCpChatDatas.WxCpChatData chatDatum : chatDatas.getChatData()) { WxCpChatModel decryptData = msgAuditService.getDecryptData(chatDatas.getSdk(), chatDatum, 2); - // 文件后缀 - String suffix = ""; - // 文件名md5 - String md5Sum = ""; - // sdkFileId - String sdkFileId = ""; + // 文件后缀 + String suffix = ""; + // 文件名md5 + String md5Sum = ""; + // sdkFileId + String sdkFileId = ""; String msgType = decryptData.getMsgType(); - if(msgType == null ) msgType = ""; + if (msgType == null) msgType = ""; switch (msgType) { - case "image": - suffix = ".jpg"; - md5Sum = decryptData.getImage().getMd5Sum(); - sdkFileId = decryptData.getImage().getSdkFileId(); - break; - case "voice": - suffix = ".amr"; - md5Sum = decryptData.getVoice().getMd5Sum(); - sdkFileId = decryptData.getVoice().getSdkFileId(); - break; - case "video": - suffix = ".mp4"; - md5Sum = decryptData.getVideo().getMd5Sum(); - sdkFileId = decryptData.getVideo().getSdkFileId(); - break; - case "emotion": - md5Sum = decryptData.getEmotion().getMd5Sum(); - sdkFileId = decryptData.getEmotion().getSdkFileId(); - int type = decryptData.getEmotion().getType(); - switch (type) { - case 1: - suffix = ".gif"; - break; - case 2: - suffix = ".png"; - break; - default: - return; - } - break; - case "file": - md5Sum = decryptData.getFile().getMd5Sum(); - suffix = "." + decryptData.getFile().getFileExt(); - sdkFileId = decryptData.getFile().getSdkFileId(); - break; - // 音频存档消息 - case "meeting_voice_call": + case "image": + suffix = ".jpg"; + md5Sum = decryptData.getImage().getMd5Sum(); + sdkFileId = decryptData.getImage().getSdkFileId(); + break; + case "voice": + suffix = ".amr"; + md5Sum = decryptData.getVoice().getMd5Sum(); + sdkFileId = decryptData.getVoice().getSdkFileId(); + break; + case "video": + suffix = ".mp4"; + md5Sum = decryptData.getVideo().getMd5Sum(); + sdkFileId = decryptData.getVideo().getSdkFileId(); + break; + case "emotion": + md5Sum = decryptData.getEmotion().getMd5Sum(); + sdkFileId = decryptData.getEmotion().getSdkFileId(); + int type = decryptData.getEmotion().getType(); + switch (type) { + case 1: + suffix = ".gif"; + break; + case 2: + suffix = ".png"; + break; + default: + return; + } + break; + case "file": + md5Sum = decryptData.getFile().getMd5Sum(); + suffix = "." + decryptData.getFile().getFileExt(); + sdkFileId = decryptData.getFile().getSdkFileId(); + break; + // 音频存档消息 + case "meeting_voice_call": - md5Sum = decryptData.getVoiceId(); - sdkFileId = decryptData.getMeetingVoiceCall().getSdkFileId(); - for (WxCpChatModel.MeetingVoiceCall.DemoFileData demofiledata : - decryptData.getMeetingVoiceCall().getDemoFileData()) { - String demoFileDataFileName = demofiledata.getFileName(); - suffix = demoFileDataFileName.substring(demoFileDataFileName.lastIndexOf(".") + 1); - } + md5Sum = decryptData.getVoiceId(); + sdkFileId = decryptData.getMeetingVoiceCall().getSdkFileId(); + for (WxCpChatModel.MeetingVoiceCall.DemoFileData demofiledata : + decryptData.getMeetingVoiceCall().getDemoFileData()) { + String demoFileDataFileName = demofiledata.getFileName(); + suffix = demoFileDataFileName.substring(demoFileDataFileName.lastIndexOf(".") + 1); + } - break; - // 音频共享文档消息 - case "voip_doc_share": + break; + // 音频共享文档消息 + case "voip_doc_share": - md5Sum = decryptData.getVoipId(); - WxCpFileItem docShare = decryptData.getVoipDocShare(); - String fileName = docShare.getFileName(); - suffix = fileName.substring(fileName.lastIndexOf(".") + 1); - break; - default: - continue; - } + md5Sum = decryptData.getVoipId(); + WxCpFileItem docShare = decryptData.getVoipDocShare(); + String fileName = docShare.getFileName(); + suffix = fileName.substring(fileName.lastIndexOf(".") + 1); + break; + default: + continue; + } - /** - * 拉取媒体文件 - * - * 传入一个each函数,用于遍历每个分片的数据 - */ + /* + * 拉取媒体文件 + * + * 传入一个each函数,用于遍历每个分片的数据 + */ String path = Thread.currentThread().getContextClassLoader().getResource("").getPath(); String targetPath = path + "testfile/" + md5Sum + suffix; File file = new File(targetPath); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); - }else{ + } else { file.delete(); } - cpService.getMsgAuditService().getMediaFile(chatDatas.getSdk(), sdkFileId, null, null, 1000L, data -> { - try { - // 大于512k的文件会分片拉取,此处需要使用追加写,避免后面的分片覆盖之前的数据。 - FileOutputStream outputStream = new FileOutputStream(targetPath, true); - outputStream.write(data); - outputStream.close(); - } catch (Exception e) { - e.printStackTrace(); - } - }); + cpService.getMsgAuditService().getMediaFile(chatDatas.getSdk(), sdkFileId, null, null, 1000L, data -> { + try { + // 大于512k的文件会分片拉取,此处需要使用追加写,避免后面的分片覆盖之前的数据。 + FileOutputStream outputStream = new FileOutputStream(targetPath, true); + outputStream.write(data); + outputStream.close(); + } catch (Exception e) { + e.printStackTrace(); + } + }); } Finance.DestroySdk(chatDatas.getSdk()); } diff --git a/weixin-java-cp/src/test/resources/test-config.sample.xml b/weixin-java-cp/src/test/resources/test-config.sample.xml index 05b112850..5aa02af94 100644 --- a/weixin-java-cp/src/test/resources/test-config.sample.xml +++ b/weixin-java-cp/src/test/resources/test-config.sample.xml @@ -13,6 +13,7 @@ <webhookKey>webhook链接地址的key值</webhookKey> <!-- 企业微信会话存档,私钥,windows以及linux环境sdk路径 --> <msgAuditSecret>会话存档的secret</msgAuditSecret> + <msgAuditLibPath>会话存档的lib path</msgAuditLibPath> <msgAuditPriKey>-----BEGIN RSA PRIVATE KEY----- MIICXAIBAAKBgQCTfm5cxqfglfOV7b/Z7OtTZZoZpk2EPTvVhn/ngsfKR899xRdR 25s4h8HkK0XhxqYdOGoAdG3Gms+DvCSY/vu3UtImf0eZSNXpKZJBUnvUVjX4ivnr