@@ -754,6 +754,76 @@ public void testGetMediaFile() throws Exception {
754754 Finance .DestroySdk (chatDatas .getSdk ());
755755 }
756756
757+ /**
758+ * 测试新的安全API方法(推荐使用)
759+ * 这些方法不需要手动管理SDK生命周期,更加安全
760+ */
761+ @ Test
762+ public void testNewSafeApi () throws Exception {
763+ WxCpMsgAuditService msgAuditService = cpService .getMsgAuditService ();
764+
765+ // 测试新的getChatRecords方法 - 不暴露SDK
766+ List <WxCpChatDatas .WxCpChatData > chatRecords = msgAuditService .getChatRecords (0L , 10L , null , null , 1000L );
767+ log .info ("获取到 {} 条聊天记录" , chatRecords .size ());
768+
769+ for (WxCpChatDatas .WxCpChatData chatData : chatRecords ) {
770+ // 测试新的getDecryptChatData方法 - 不需要传入SDK
771+ WxCpChatModel decryptData = msgAuditService .getDecryptChatData (chatData , 2 );
772+ log .info ("解密数据:{}" , decryptData .toJson ());
773+
774+ // 测试新的getChatRecordPlainText方法 - 不需要传入SDK
775+ String plainText = msgAuditService .getChatRecordPlainText (chatData , 2 );
776+ log .info ("明文数据:{}" , plainText );
777+
778+ // 如果是媒体消息,测试新的downloadMediaFile方法
779+ String msgType = decryptData .getMsgType ();
780+ if ("image" .equals (msgType ) || "voice" .equals (msgType ) || "video" .equals (msgType ) || "file" .equals (msgType )) {
781+ String suffix = "" ;
782+ String md5Sum = "" ;
783+ String sdkFileId = "" ;
784+
785+ switch (msgType ) {
786+ case "image" :
787+ suffix = ".jpg" ;
788+ md5Sum = decryptData .getImage ().getMd5Sum ();
789+ sdkFileId = decryptData .getImage ().getSdkFileId ();
790+ break ;
791+ case "voice" :
792+ suffix = ".amr" ;
793+ md5Sum = decryptData .getVoice ().getMd5Sum ();
794+ sdkFileId = decryptData .getVoice ().getSdkFileId ();
795+ break ;
796+ case "video" :
797+ suffix = ".mp4" ;
798+ md5Sum = decryptData .getVideo ().getMd5Sum ();
799+ sdkFileId = decryptData .getVideo ().getSdkFileId ();
800+ break ;
801+ case "file" :
802+ md5Sum = decryptData .getFile ().getMd5Sum ();
803+ suffix = "." + decryptData .getFile ().getFileExt ();
804+ sdkFileId = decryptData .getFile ().getSdkFileId ();
805+ break ;
806+ }
807+
808+ // 测试新的downloadMediaFile方法 - 不需要传入SDK
809+ String path = Thread .currentThread ().getContextClassLoader ().getResource ("" ).getPath ();
810+ String targetPath = path + "testfile-new/" + md5Sum + suffix ;
811+ File file = new File (targetPath );
812+ if (!file .getParentFile ().exists ()) {
813+ file .getParentFile ().mkdirs ();
814+ } else {
815+ file .delete ();
816+ }
817+
818+ // 使用新的API下载媒体文件
819+ msgAuditService .downloadMediaFile (sdkFileId , null , null , 1000L , targetPath );
820+ log .info ("媒体文件下载成功:{}" , targetPath );
821+ }
822+ }
823+
824+ // 注意:使用新API无需手动调用 Finance.DestroySdk(),SDK由框架自动管理
825+ }
826+
757827 // 测试Uint64类型
758828 public static void main (String [] args ){
759829 /*
0 commit comments