Issues (14)

src/Provider/AbstractProvider.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
namespace tinymeng\WeWorkFinanceSDK\Provider;
5
6
use tinymeng\WeWorkFinanceSDK\Contract\ProviderInterface;
7
use tinymeng\WeWorkFinanceSDK\Exception\FinanceSDKException;
8
use tinymeng\WeWorkFinanceSDK\Exception\InvalidArgumentException;
9
10
/**
11
 * AbstractProvider
12
 * @author: TinyMeng <[email protected]>
13
 */
14
abstract class AbstractProvider implements ProviderInterface
15
{
16
    /**
17
     * 获取会话解密记录数据.
18
     * @param int $seq 起始位置
19
     * @param int $limit 限制条数
20
     * @param int $retry 重试次数
21
     * @return array ...
22
     * @throws InvalidArgumentException
23
     * @throws FinanceSDKException
24
     */
25
    public function getDecryptChatData(int $seq, int $limit, int $retry = 0): array
26
    {
27
        $config = $this->getConfig();
28
        if (!isset($config['private_keys'])) {
29
            throw new InvalidArgumentException('缺少配置:private_keys[{"version":"private_key"}]');
30
        }
31
        $privateKeys = $config['private_keys'];
32
33
        try {
34
            $chatData = json_decode($this->getChatData($seq, $limit), true)['chatdata'];
35
            $newChatData = [];
36
            $lastSeq = 0;
37
            foreach ($chatData as $i => $item) {
38
                $lastSeq = $item['seq'];
39
                if (!isset($privateKeys[$item['publickey_ver']])) {
40
                    continue;
41
                }
42
43
                $decryptRandKey = null;
44
                openssl_private_decrypt(
45
                    base64_decode($item['encrypt_random_key']),
46
                    $decryptRandKey,
47
                    $privateKeys[$item['publickey_ver']],
48
                    OPENSSL_PKCS1_PADDING
49
                );
50
51
                // TODO 无法解密,一般为秘钥不匹配
52
                // 临时补丁方案,需要改为支持多版本key
53
                if ($decryptRandKey === null) {
54
                    continue;
55
                }
56
57
                $newChatData[$i] = json_decode($this->decryptData($decryptRandKey, $item['encrypt_chat_msg']), true);
58
                $newChatData[$i]['seq'] = $item['seq'];
59
            }
60
61
            if (!empty($chatData) && empty($chatData) && $retry && $retry < 10) {
62
                return $this->getDecryptChatData($lastSeq, $limit, ++$retry);
63
            }
64
65
            return $newChatData;
66
        } catch (\Exception $e) {
67
            throw new FinanceSDKException($e->getMessage(), $e->getCode());
68
        }
69
    }
70
71
    /**
72
     * 下载媒体资源
73
     * @param $object
74
     * @param $msgType
75
     * @return Object SplFileInfo
76
     * @throws FinanceSDKException
77
     */
78
    public function getDownloadMediaData($object,$msgType)
79
    {
80
        try {
81
            $filePath = $this->getFilePath($object,$msgType);
82
            return $this->getMediaData($object['sdkfileid'],$filePath);
83
        } catch (\Exception $e) {
84
            throw new FinanceSDKException($e->getMessage(), $e->getCode());
85
        }
86
    }
87
88
    /**
89
     * 是否是媒体资源
90
     * @param $msgType
91
     * @return bool
92
     */
93
    public function isMedia($msgType): bool
94
    {
95
        if(in_array($msgType,['image','voice','video','file','emotion'])){
96
            return true;
97
        }else{
98
            return false;
99
        }
100
    }
101
102
    /**
103
     * 获取文件存储路径
104
     * @param $object
105
     * @param $megType
106
     * @return string
107
     */
108
    protected function getFilePath($object,$megType)
109
    {
110
        $config = $this->getConfig();
111
        if(empty($config['path'])){
112
            $config['path'] = sys_get_temp_dir() . '/' ;
113
        }
114
        $fileName = '';
115
        switch ($megType){
116
            case 'image':
117
                $fileName = $object['md5sum']. ".jpg";
118
                break;
119
            case 'voice':
120
                $fileName = $object['md5sum']. ".mp3";
121
                break;
122
            case 'video':
123
                $fileName = $object['md5sum']. ".mp4";
124
                break;
125
            case 'emotion':
126
                // 表情类型,png或者gif.1表示gif 2表示png。Uint32类型
127
                if($object['type'] == 1){
128
                    $fileName = $object['md5sum']. ".gif";
129
                }else{
130
                    $fileName = $object['md5sum']. ".png";
131
                }
132
                break;
133
            case 'file':
134
                $fileName = $object['filename'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
135
            default:break;
136
        }
137
        return $config['path'].$fileName;
138
    }
139
140
}
141