88 * @contact group@mo.chat
99 * @license https://github.com/mochat-cloud/mochat/blob/master/LICENSE
1010 */
11+
1112namespace MoChat \WeWorkFinanceSDK \Provider ;
1213
1314use MoChat \WeWorkFinanceSDK \Contract \ProviderInterface ;
@@ -20,23 +21,26 @@ abstract class AbstractProvider implements ProviderInterface
2021 * 获取会话解密记录数据.
2122 * @param int $seq 起始位置
2223 * @param int $limit 限制条数
23- * @throws FinanceSDKException
24- * @throws InvalidArgumentException
24+ * @param int $retry 重试次数
2525 * @return array ...
26+ * @throws InvalidArgumentException
27+ * @throws FinanceSDKException
2628 */
27- public function getDecryptChatData (int $ seq , int $ limit ): array
29+ public function getDecryptChatData (int $ seq , int $ limit, int $ retry = 0 ): array
2830 {
2931 $ config = $ this ->getConfig ();
30- if (! isset ($ config ['private_keys ' ])) {
32+ if (!isset ($ config ['private_keys ' ])) {
3133 throw new InvalidArgumentException ('缺少配置:private_keys[{"version":"private_key"}] ' );
3234 }
3335 $ privateKeys = $ config ['private_keys ' ];
3436
3537 try {
36- $ chatData = json_decode ($ this ->getChatData ($ seq , $ limit ), true )['chatdata ' ];
38+ $ chatData = json_decode ($ this ->getChatData ($ seq , $ limit ), true )['chatdata ' ];
3739 $ newChatData = [];
40+ $ lastSeq = 0 ;
3841 foreach ($ chatData as $ i => $ item ) {
39- if (! isset ($ privateKeys [$ item ['publickey_ver ' ]])) {
42+ $ lastSeq = $ item ['seq ' ];
43+ if (!isset ($ privateKeys [$ item ['publickey_ver ' ]])) {
4044 continue ;
4145 }
4246
@@ -47,10 +51,21 @@ public function getDecryptChatData(int $seq, int $limit): array
4751 $ privateKeys [$ item ['publickey_ver ' ]],
4852 OPENSSL_PKCS1_PADDING
4953 );
50- $ newChatData [$ i ] = json_decode ($ this ->decryptData ($ decryptRandKey , $ item ['encrypt_chat_msg ' ]), true );
54+
55+ // TODO 无法解密,一般为秘钥不匹配
56+ // 临时补丁方案,需要改为支持多版本key
57+ if ($ decryptRandKey === null ) {
58+ continue ;
59+ }
60+
61+ $ newChatData [$ i ] = json_decode ($ this ->decryptData ($ decryptRandKey , $ item ['encrypt_chat_msg ' ]), true );
5162 $ newChatData [$ i ]['seq ' ] = $ item ['seq ' ];
5263 }
5364
65+ if (!empty ($ chatData ) && empty ($ chatData ) && $ retry && $ retry < 10 ) {
66+ return $ this ->getDecryptChatData ($ lastSeq , $ limit , ++$ retry );
67+ }
68+
5469 return $ newChatData ;
5570 } catch (\Exception $ e ) {
5671 throw new FinanceSDKException ($ e ->getMessage (), $ e ->getCode ());
0 commit comments