Skip to content

Commit c545ef3

Browse files
committed
[Bug修复](master): 修复会话存档密钥不匹配导致重复获取消息问题
1 parent 210ebac commit c545ef3

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

src/Provider/AbstractProvider.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @contact group@mo.chat
99
* @license https://github.com/mochat-cloud/mochat/blob/master/LICENSE
1010
*/
11+
1112
namespace MoChat\WeWorkFinanceSDK\Provider;
1213

1314
use 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());

src/Provider/FFIProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function decryptData(string $randomKey, string $encryptStr): string
112112
*/
113113
public function getMediaData(string $sdkFileId, string $ext): \SplFileInfo
114114
{
115-
$path = '/tmp/' . md5((string) time());
115+
$path = sys_get_temp_dir() . '/' . md5($sdkFileId);
116116
$ext && $path .= '.' . $ext;
117117
try {
118118
$this->downloadMediaData($sdkFileId, $path);

src/Provider/PHPExtProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function decryptData(string $randomKey, string $encryptStr): string
6666
*/
6767
public function getMediaData(string $sdkFileId, string $ext): \SplFileInfo
6868
{
69-
$path = '/tmp/' . md5((string) time());
69+
$path = sys_get_temp_dir() . '/' . md5($sdkFileId);
7070
$ext && $path .= '.' . $ext;
7171
try {
7272
$this->financeSdk->downloadMedia($sdkFileId, $path);

0 commit comments

Comments
 (0)