IMHelper.php
8.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
namespace App\Helpers;
use GuzzleHttp\Promise\PromiseInterface;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Tencent\TLSSigAPIv2;
class IMHelper
{
private string $host;
private string $key;
private string $secret;
private string $identifier;
public const SYSTEM_USER = 'system';
public function __construct()
{
$this->host = config('services.im.domain');
$this->key = config('services.im.key');
$this->secret = config('services.im.secret');
$this->identifier = config('services.im.identifier');
}
/**
* @throws \Exception
*/
private function createSignature(string $identifier): string
{
return (new TLSSigAPIv2($this->key, $this->secret))->genUserSig($identifier);
}
/**
* @throws \Exception
*/
private function getSystemSignature(): string
{
return $this->createSignature($this->identifier);
}
/**
* @throws \Exception
*/
public static function getUserSignature(int|string $userId): string
{
return (new static())->createSignature(self::userKeyToAccount($userId));
}
/**
* @throws \Exception
*/
public function getUrl(string $uri): string
{
return $this->host . '/' . $uri . '?' . Arr::query(['sdkappid' => $this->key, 'identifier' => $this->identifier, 'usersig' => $this->getSystemSignature(), 'random' => random_int(0, 4294967295), 'contenttype' => 'json']);
}
/**
* @param string $uri
* @param array $data
* @return \GuzzleHttp\Promise\PromiseInterface|\Illuminate\Http\Client\Response
* @throws \Exception
*/
public static function send(string $uri, array $data): PromiseInterface|Response
{
$host = (new static())->getUrl($uri);
$result = Http::asJson()->post($host, $data);
Log::channel('im')->info('IM ' . $uri . ' status[' . $result->json('ActionStatus') . ']:', [
'uri' => $uri, 'data' => $data, 'result' => $result->json()
]);
return $result;
}
/**
* @param int|string $userId
* @return string
*/
public static function userKeyToAccount(int|string $userId): string
{
return config('services.im.prefix') . $userId;
}
/**
* @param string $account
* @return string
*/
public static function accountToUserKey(string $account): string
{
return Str::replace(config('services.im.prefix'), '', $account);
}
/**
* @param string $title
* @param string $desc
* @param string|array $ext
* @return array
* @throws \JsonException
*/
public static function createOfflineMessage(string $title, string $desc = '', string|array $ext = ''): array
{
return [
"PushFlag" => 0,
"Title" => $title,
"Desc" => $desc,
"Ext" => is_array($ext) ? json_encode($ext, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE) : $ext,
"AndroidInfo" => ["Sound" => "android.mp3"],
"ApnsInfo" => ["Sound" => "apns.mp3", "BadgeMode" => 1]
];
}
/**
* @param string $type
* @param array $content
* @return array
*/
public static function createOnlineMessage(string $type, array $content): array
{
return [
'MsgType' => $type,
'MsgContent' => [
...Arr::map($content, static fn($item) => is_array($item) ? json_encode($item, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE) : $item),
'Sound' => 'dingdong.aiff'
]
];
}
/**
* @param array $content
* @param array $ext
* @return array
*/
public static function createCustomMessage(array $content, array $ext = []): array
{
return self::createOnlineMessage('TIMCustomElem', [...$content, "Ext" => $ext,]);
}
/**
* @param string $form
* @param string $to
* @param array $payload
* @return \GuzzleHttp\Promise\PromiseInterface|\Illuminate\Http\Client\Response
* @throws \Exception
*/
public static function sendSingleChatMessage(string $form, string $to, array $payload): PromiseInterface|Response
{
return self::send('v4/openim/sendmsg', [
"SyncOtherMachine" => 2,
'IsNeedReadReceipt' => 1,
"MsgRandom" => random_int(0, 4294967295),
"From_Account" => $form,
"To_Account" => self::userKeyToAccount($to)
] + $payload);
}
/**
* @param string $form
* @param array $to
* @param array $payload
* @return \GuzzleHttp\Promise\PromiseInterface|\Illuminate\Http\Client\Response
* @throws \Exception
*/
public static function sendBatchChatMessage(string $form, array $to, array $payload): PromiseInterface|Response
{
return self::send('v4/openim/batchsendmsg', [
"SyncOtherMachine" => 2,
'IsNeedReadReceipt' => 1,
'MsgRandom' => random_int(0, 4294967295),
"From_Account" => is_numeric($form) ? self::userKeyToAccount($form) : $form,
"To_Account" => Arr::map($to, static fn($item) => self::userKeyToAccount($item)),
] + $payload);
}
/**
* @param string $form
* @param string $to
* @param string $msgKey
* @return \GuzzleHttp\Promise\PromiseInterface|\Illuminate\Http\Client\Response
* @throws \Exception
*/
public static function rollbackChatMessage(string $form, string $to, string $msgKey): PromiseInterface|Response
{
info('IM_ROLLBACK', [$form, $to, $msgKey]);
return self::send('v4/openim/admin_msgwithdraw', ["From_Account" => $form, "To_Account" => is_numeric($to) ? self::userKeyToAccount($to) : $to, "MsgKey" => $msgKey]);
}
/**
* @throws \Exception
*/
public static function checkAccount(array $account): PromiseInterface|Response
{
return self::send('v4/im_open_login_svc/account_check ', [
'CheckItem' => Arr::map($account, static fn($item) => ['UserID' => self::userKeyToAccount($item)])
]);
}
/**
* @throws \JsonException
* @throws \Exception
*/
public static function importSingleAccount(string $userId, string|array $name, string $avatar = ''): PromiseInterface|Response
{
return self::send('v4/im_open_login_svc/account_import', [
'UserID' => self::userKeyToAccount($userId),
'Nick' => is_array($name) ? json_encode($name, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE) : $name,
'FaceUrl' => $avatar
]);
}
/**
* @throws \Exception
*/
public static function updateSingleAccountInfo(string $userId, array $profileItem): PromiseInterface|Response
{
return self::send('v4/profile/portrait_set', ['From_Account' => self::userKeyToAccount($userId), 'ProfileItem' => $profileItem]);
}
/**
* @throws \Exception
*/
public static function createGroup(string $userId, array $data = []): PromiseInterface|Response
{
return self::send('v4/group_open_http_svc/create_group', [
'Type' => 'Public',
'GroupId' => config('services.im.prefix') . now()->format('YmdHisu'),
'Owner_Account' => self::userKeyToAccount($userId),
...$data
]);
}
/**
* @throws \Random\RandomException
* @throws \Exception
*/
public static function sendGroupMessage(string $groupId, array $data): PromiseInterface|Response
{
return self::send('v4/group_open_http_svc/send_group_msg', ["GroupId" => $groupId, "Random" => random_int(0, 4294967295),] + $data);
}
/**
* @param string $groupId
* @return \GuzzleHttp\Promise\PromiseInterface|\Illuminate\Http\Client\Response
* @throws \Exception
*/
public static function deleteGroup(string $groupId): PromiseInterface|Response
{
return self::send('v4/group_open_http_svc/destroy_group', ['GroupId' => $groupId]);
}
}