1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: lenovo |
5
|
|
|
* Date: 6/14/2018 |
6
|
|
|
* Time: 8:08 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace TimSDK\Core; |
10
|
|
|
|
11
|
|
|
use TimSDK\Support\Str; |
12
|
|
|
use TimSDK\Support\Log; |
13
|
|
|
use TimSDK\Support\Arr; |
14
|
|
|
use TimSDK\Support\Collection; |
15
|
|
|
use TimSDK\Core\Exceptions\HttpException; |
16
|
|
|
use TimSDK\Core\Exceptions\MissingArgumentsException; |
17
|
|
|
|
18
|
|
|
class IMCloud extends BaseIMCloud |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Collection |
22
|
|
|
*/ |
23
|
|
|
protected $query; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
protected $needRefresh = false; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Set on refresh swith |
32
|
|
|
*/ |
33
|
|
|
public function needRefresh() |
34
|
|
|
{ |
35
|
|
|
$this->needRefresh = true; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
|
|
public function isNeedRefresh() |
42
|
|
|
{ |
43
|
|
|
return (bool) $this->needRefresh; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Init |
48
|
|
|
* |
49
|
|
|
* @throws MissingArgumentsException |
50
|
|
|
* @throws \TimSDK\Core\Exceptions\UserSigException |
51
|
|
|
*/ |
52
|
|
|
public function initialize() |
53
|
|
|
{ |
54
|
|
|
$this->getRefreshedQueryStringCollection(true); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Request api |
59
|
|
|
* |
60
|
|
|
* @param $uri |
61
|
|
|
* @param array $data |
62
|
|
|
* @param array $options |
63
|
|
|
* @return \TimSDK\Foundation\ResponseBag |
64
|
|
|
* @throws \TimSDK\Core\Exceptions\JsonParseException |
65
|
|
|
* @throws \TimSDK\Core\Exceptions\UserSigException |
66
|
|
|
* @throws \TimSDK\Core\Exceptions\HttpException |
67
|
|
|
* @throws \TimSDK\Core\Exceptions\MissingArgumentsException |
68
|
|
|
*/ |
69
|
|
|
public function handle($uri, $data = [], $options = []) |
70
|
|
|
{ |
71
|
|
|
if (empty($data)) { |
72
|
|
|
$data = '{}'; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$response = $this->httpPostJson($uri, $data, array_merge($options, [ |
|
|
|
|
76
|
|
|
'query' => $this->getRefreshedQueryStringArray() |
77
|
|
|
])); |
78
|
|
|
|
79
|
|
|
$this->checkAndThrow($response->getContents()); |
80
|
|
|
|
81
|
|
|
return $response; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Generate sig |
86
|
|
|
* |
87
|
|
|
* @param $identifier |
88
|
|
|
* @return string |
89
|
|
|
* @throws \TimSDK\Core\Exceptions\UserSigException |
90
|
|
|
*/ |
91
|
|
|
public function generateSig($identifier) |
92
|
|
|
{ |
93
|
|
|
return $this->app['TLSSig']->genSig($identifier); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Refresh query string |
98
|
|
|
* |
99
|
|
|
* @param bool $force |
100
|
|
|
* @return Collection |
101
|
|
|
* @throws Exceptions\UserSigException |
102
|
|
|
* @throws MissingArgumentsException |
103
|
|
|
*/ |
104
|
|
|
public function getRefreshedQueryStringCollection($force = false) |
105
|
|
|
{ |
106
|
|
|
if ($this->needRefresh || $force) { |
107
|
|
|
$this->needRefresh = false; |
108
|
|
|
$this->query = $this->getQueryStringCollection(); |
109
|
|
|
$this->query->setAll($this->getLatestQueryStringArray()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this->query; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get the refreshed query string |
117
|
|
|
* |
118
|
|
|
* @return array |
119
|
|
|
* @throws Exceptions\UserSigException |
120
|
|
|
* @throws MissingArgumentsException |
121
|
|
|
*/ |
122
|
|
|
public function getRefreshedQueryStringArray() |
123
|
|
|
{ |
124
|
|
|
return $this->getRefreshedQueryStringCollection()->toArray(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Query Getter |
129
|
|
|
* |
130
|
|
|
* @return Collection |
131
|
|
|
*/ |
132
|
|
|
public function getQueryStringCollection() |
133
|
|
|
{ |
134
|
|
|
if (!$this->query instanceof Collection) { |
|
|
|
|
135
|
|
|
$this->query = new Collection(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $this->query; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get the latest query string array |
143
|
|
|
* |
144
|
|
|
* @return array |
145
|
|
|
* @throws Exceptions\UserSigException |
146
|
|
|
* @throws MissingArgumentsException |
147
|
|
|
*/ |
148
|
|
|
public function getLatestQueryStringArray() |
149
|
|
|
{ |
150
|
|
|
$data = Arr::only($this->app['config']->all(), [ |
151
|
|
|
'sdkappid', |
152
|
|
|
'identifier', |
153
|
|
|
'prikey', |
154
|
|
|
'pubkey', |
155
|
|
|
'random', |
156
|
|
|
'contenttype', |
157
|
|
|
]); |
158
|
|
|
|
159
|
|
|
if (!isset($data['random'])) { |
160
|
|
|
$data['random'] = time(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if (!isset($data['contenttype'])) { |
164
|
|
|
$data['contenttype'] = 'json'; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
foreach (['sdkappid', 'identifier', 'prikey', 'pubkey'] as $item) { |
168
|
|
|
if (!isset($data[$item])) { |
169
|
|
|
Log::debug('IMCloud Query: ', $data); |
170
|
|
|
throw new MissingArgumentsException('Missing ' . $item); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$data['usersig'] = $this->generateSig($data['identifier']); |
175
|
|
|
|
176
|
|
|
return $data; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get a full url |
181
|
|
|
* |
182
|
|
|
* @param $uri |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
protected function getFullApiUrl($uri) |
186
|
|
|
{ |
187
|
|
|
return Str::startsWith($uri, ['http', 'https']) ? $uri : API::BASE_URL . $uri; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Check the array data errors, and Throw exception when the contents contains error. |
192
|
|
|
* |
193
|
|
|
* @param array|Collection $contents |
194
|
|
|
* |
195
|
|
|
* @throws HttpException |
196
|
|
|
*/ |
197
|
|
|
protected function checkAndThrow($contents) |
198
|
|
|
{ |
199
|
|
|
if ($contents instanceof Collection) { |
200
|
|
|
$contents = $contents->toArray(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
if (isset($contents['ErrorCode']) && 0 !== $contents['ErrorCode']) { |
204
|
|
|
if (empty($contents['ErrorInfo'])) { |
205
|
|
|
$contents['ErrorInfo'] = 'Unknown'; |
206
|
|
|
} |
207
|
|
|
throw new HttpException($contents['ErrorInfo'], $contents['ErrorCode']); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|