🎨 #2773【企业微信】优化会话存档,增加会话存档的多企业支持
This commit is contained in:
@@ -33,22 +33,24 @@ public interface WxCpMsgAuditService {
|
||||
/**
|
||||
* 获取解密的聊天数据Model
|
||||
*
|
||||
* @param sdk getChatDatas()获取到的sdk
|
||||
* @param chatData getChatDatas()获取到的聊天数据
|
||||
* @param pkcs1 使用什么方式进行解密,1代表使用PKCS1进行解密,2代表PKCS8进行解密 ...
|
||||
* @return 解密后的聊天数据
|
||||
* @throws Exception
|
||||
*/
|
||||
WxCpChatModel getDecryptData(@NonNull WxCpChatDatas.WxCpChatData chatData, @NonNull Integer pkcs1) throws Exception;
|
||||
WxCpChatModel getDecryptData(@NonNull long sdk, @NonNull WxCpChatDatas.WxCpChatData chatData, @NonNull Integer pkcs1) throws Exception;
|
||||
|
||||
/**
|
||||
* 获取解密的聊天数据明文
|
||||
*
|
||||
* @param sdk getChatDatas()获取到的sdk
|
||||
* @param chatData getChatDatas()获取到的聊天数据
|
||||
* @param pkcs1 使用什么方式进行解密,1代表使用PKCS1进行解密,2代表PKCS8进行解密 ...
|
||||
* @return 解密后的明文
|
||||
* @throws Exception
|
||||
*/
|
||||
String getChatPlainText(@NonNull WxCpChatDatas.WxCpChatData chatData, @NonNull Integer pkcs1) throws Exception;
|
||||
String getChatPlainText(@NonNull long sdk, @NonNull WxCpChatDatas.WxCpChatData chatData, @NonNull Integer pkcs1) throws Exception;
|
||||
|
||||
/**
|
||||
* 获取媒体文件
|
||||
@@ -58,6 +60,7 @@ public interface WxCpMsgAuditService {
|
||||
* 根据上面返回的文件类型,拼接好存放文件的绝对路径即可。此时绝对路径写入文件流,来达到获取媒体文件的目的。
|
||||
* 详情可以看官方文档,亦可阅读此接口源码。
|
||||
*
|
||||
* @param sdk getChatDatas()获取到的sdk,注意,每次获取的sdk会不一样
|
||||
* @param sdkfileid 消息体内容中的sdkfileid信息
|
||||
* @param proxy 使用代理的请求,需要传入代理的链接。如:socks5://10.0.0.1:8081 或者 http://10.0.0.1:8081,如果没有传null
|
||||
* @param passwd 代理账号密码,需要传入代理的账号密码。如 user_name:passwd_123,如果没有传null
|
||||
@@ -65,7 +68,7 @@ public interface WxCpMsgAuditService {
|
||||
* @param targetFilePath 目标文件绝对路径+实际文件名,比如:/usr/local/file/20220114/474f866b39d10718810d55262af82662.gif
|
||||
* @throws WxErrorException
|
||||
*/
|
||||
void getMediaFile(@NonNull String sdkfileid, String proxy, String passwd, @NonNull long timeout, @NonNull String targetFilePath) throws WxErrorException;
|
||||
void getMediaFile(@NonNull long sdk, @NonNull String sdkfileid, String proxy, String passwd, @NonNull long timeout, @NonNull String targetFilePath) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 获取会话内容存档开启成员列表
|
||||
|
||||
@@ -76,11 +76,11 @@ public class WxCpMsgAuditServiceImpl implements WxCpMsgAuditService {
|
||||
osLib.addAll(fileLib);
|
||||
|
||||
Finance.loadingLibraries(osLib, prefixPath);
|
||||
long sdk = Finance.SingletonSDK();
|
||||
long sdk = Finance.NewSdk();
|
||||
|
||||
long ret = Finance.Init(sdk, cpService.getWxCpConfigStorage().getCorpId(), cpService.getWxCpConfigStorage().getCorpSecret());
|
||||
if (ret != 0) {
|
||||
Finance.DestroySingletonSDK(sdk);
|
||||
Finance.DestroySdk(sdk);
|
||||
throw new WxErrorException("init sdk err ret " + ret);
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ public class WxCpMsgAuditServiceImpl implements WxCpMsgAuditService {
|
||||
ret = Finance.GetChatData(sdk, seq, limit, proxy, passwd, timeout, slice);
|
||||
if (ret != 0) {
|
||||
Finance.FreeSlice(slice);
|
||||
Finance.DestroySingletonSDK(sdk);
|
||||
Finance.DestroySdk(sdk);
|
||||
throw new WxErrorException("getchatdata err ret " + ret);
|
||||
}
|
||||
|
||||
@@ -97,20 +97,21 @@ public class WxCpMsgAuditServiceImpl implements WxCpMsgAuditService {
|
||||
Finance.FreeSlice(slice);
|
||||
WxCpChatDatas chatDatas = WxCpChatDatas.fromJson(content);
|
||||
if (chatDatas.getErrCode().intValue() != 0) {
|
||||
Finance.DestroySingletonSDK(sdk);
|
||||
Finance.DestroySdk(sdk);
|
||||
throw new WxErrorException(chatDatas.toJson());
|
||||
}
|
||||
|
||||
chatDatas.setSdk(sdk);
|
||||
return chatDatas;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxCpChatModel getDecryptData(@NonNull WxCpChatDatas.WxCpChatData chatData, @NonNull Integer pkcs1) throws Exception {
|
||||
String plainText = this.decryptChatData(chatData, pkcs1);
|
||||
public WxCpChatModel getDecryptData(@NonNull long sdk, @NonNull WxCpChatDatas.WxCpChatData chatData, @NonNull Integer pkcs1) throws Exception {
|
||||
String plainText = this.decryptChatData(sdk, chatData, pkcs1);
|
||||
return WxCpChatModel.fromJson(plainText);
|
||||
}
|
||||
|
||||
public String decryptChatData(WxCpChatDatas.WxCpChatData chatData, Integer pkcs1) throws Exception {
|
||||
public String decryptChatData(long sdk, WxCpChatDatas.WxCpChatData chatData, Integer pkcs1) throws Exception {
|
||||
/**
|
||||
* 企业获取的会话内容,使用企业自行配置的消息加密公钥进行加密,企业可用自行保存的私钥解开会话内容数据。
|
||||
* msgAuditPriKey 会话存档私钥不能为空
|
||||
@@ -124,7 +125,6 @@ public class WxCpMsgAuditServiceImpl implements WxCpMsgAuditService {
|
||||
/**
|
||||
* 每次使用DecryptData解密会话存档前需要调用NewSlice获取一个slice,在使用完slice中数据后,还需要调用FreeSlice释放。
|
||||
*/
|
||||
long sdk = Finance.SingletonSDK();
|
||||
long msg = Finance.NewSlice();
|
||||
|
||||
/**
|
||||
@@ -135,7 +135,7 @@ public class WxCpMsgAuditServiceImpl implements WxCpMsgAuditService {
|
||||
int ret = Finance.DecryptData(sdk, decryptByPriKey, chatData.getEncryptChatMsg(), msg);
|
||||
if (ret != 0) {
|
||||
Finance.FreeSlice(msg);
|
||||
Finance.DestroySingletonSDK(sdk);
|
||||
Finance.DestroySdk(sdk);
|
||||
throw new WxErrorException("msg err ret " + ret);
|
||||
}
|
||||
|
||||
@@ -148,12 +148,12 @@ public class WxCpMsgAuditServiceImpl implements WxCpMsgAuditService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getChatPlainText(WxCpChatDatas.@NonNull WxCpChatData chatData, @NonNull Integer pkcs1) throws Exception {
|
||||
return this.decryptChatData(chatData, pkcs1);
|
||||
public String getChatPlainText(@NonNull long sdk, WxCpChatDatas.@NonNull WxCpChatData chatData, @NonNull Integer pkcs1) throws Exception {
|
||||
return this.decryptChatData(sdk, chatData, pkcs1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getMediaFile(@NonNull String sdkfileid, String proxy, String passwd, @NonNull long timeout, @NonNull String targetFilePath) throws WxErrorException {
|
||||
public void getMediaFile(@NonNull long sdk, @NonNull String sdkfileid, String proxy, String passwd, @NonNull long timeout, @NonNull String targetFilePath) throws WxErrorException {
|
||||
/**
|
||||
* 1、媒体文件每次拉取的最大size为512k,因此超过512k的文件需要分片拉取。
|
||||
* 2、若该文件未拉取完整,sdk的IsMediaDataFinish接口会返回0,同时通过GetOutIndexBuf接口返回下次拉取需要传入GetMediaData的indexbuf。
|
||||
@@ -166,13 +166,13 @@ public class WxCpMsgAuditServiceImpl implements WxCpMsgAuditService {
|
||||
|
||||
String indexbuf = "";
|
||||
int ret, data_len = 0;
|
||||
log.debug("正在分片拉取媒体文件 sdkFileId为{}", sdkfileid);
|
||||
while (true) {
|
||||
long mediaData = Finance.NewMediaData();
|
||||
long sdk = Finance.SingletonSDK();
|
||||
ret = Finance.GetMediaData(sdk, indexbuf, sdkfileid, proxy, passwd, timeout, mediaData);
|
||||
if (ret != 0) {
|
||||
Finance.FreeMediaData(mediaData);
|
||||
Finance.DestroySingletonSDK(sdk);
|
||||
Finance.DestroySdk(sdk);
|
||||
throw new WxErrorException("getmediadata err ret " + ret);
|
||||
}
|
||||
|
||||
|
||||
@@ -434,11 +434,20 @@ public class WxCpXmlMessage implements Serializable {
|
||||
* 1. 群发的结果.
|
||||
* 2. 通讯录变更事件
|
||||
* 激活状态:1=已激活 2=已禁用 4=未激活 已激活代表已激活企业微信或已关注微工作台(原企业号).
|
||||
* 3. 直播回调事件
|
||||
* 直播状态 ,0:预约中,1:直播中,2:已结束,4:已取消 (已过期状态目前没有回调)
|
||||
*/
|
||||
@XStreamAlias("Status")
|
||||
@XStreamConverter(value = XStreamCDataConverter.class)
|
||||
private String status;
|
||||
|
||||
/**
|
||||
* 直播ID
|
||||
*/
|
||||
@XStreamAlias("LivingId")
|
||||
@XStreamConverter(value = XStreamCDataConverter.class)
|
||||
private String livingId;
|
||||
|
||||
/**
|
||||
* group_id下粉丝数;或者openid_list中的粉丝数.
|
||||
*/
|
||||
|
||||
@@ -12,7 +12,8 @@ import java.util.List;
|
||||
/**
|
||||
* 聊天记录数据内容.
|
||||
*
|
||||
* @author Wang_Wong
|
||||
* @author <a href="https://github.com/0katekate0">Wang_Wong</a>
|
||||
* @date 2022-01-17
|
||||
*/
|
||||
@Data
|
||||
public class WxCpChatDatas implements Serializable {
|
||||
@@ -24,6 +25,9 @@ public class WxCpChatDatas implements Serializable {
|
||||
@SerializedName("errmsg")
|
||||
private String errMsg;
|
||||
|
||||
@SerializedName("sdk")
|
||||
private long sdk;
|
||||
|
||||
@SerializedName("chatdata")
|
||||
private List<WxCpChatData> chatData;
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package me.chanjar.weixin.cp.util.crypto;
|
||||
|
||||
import com.google.common.io.BaseEncoding;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.crypto.WxCryptUtil;
|
||||
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
|
||||
@@ -61,7 +60,7 @@ public class WxCpCryptUtil extends WxCryptUtil {
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String decryptPriKeyByPKCS8(String encryptRandomKey, String msgAuditPriKey) throws Exception {
|
||||
String privateKey = msgAuditPriKey.replaceAll("\\n", "")
|
||||
String privateKey = msgAuditPriKey.replaceAll("(\r\n|\r|\n|\n\r)", "")
|
||||
.replace("-----BEGIN PRIVATE KEY-----", "")
|
||||
.replace("-----END PRIVATE KEY-----", "")
|
||||
.replaceAll(" ", "");
|
||||
@@ -87,7 +86,7 @@ public class WxCpCryptUtil extends WxCryptUtil {
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String decryptPriKeyByPKCS1(String encryptRandomKey, String msgAuditPriKey) throws Exception {
|
||||
String privateKey = msgAuditPriKey.replaceAll("\\n", "")
|
||||
String privateKey = msgAuditPriKey.replaceAll("(\r\n|\r|\n|\n\r)", "")
|
||||
.replace("-----BEGIN RSA PRIVATE KEY-----", "")
|
||||
.replace("-----END RSA PRIVATE KEY-----", "")
|
||||
.replaceAll(" ", "");
|
||||
|
||||
Reference in New Issue
Block a user