Tim.php
11.2 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
namespace app\common\controller;
use think\Controller;
use GuzzleHttp\Client;
class Tim extends Controller
{
protected $sdkappid;
protected $key;
protected $api;
protected $identifier = 'administrator';
// 错误信息
protected $_error = '';
public function __construct(){
vendor('autoload');
$this->sdkappid = config('tim.sdkappid');
$this->key = config('tim.key');
$this->api = new \Tencent\TLSSigAPIv2($this->sdkappid, $this->key);
}
/**
* 获取签名
*/
public function getSig($identifier){
return $this->api->genUserSig((string)$identifier);
}
/**
* 导入单个账号
*/
public function accountImport($user_id,$nickname,$head_photo='http://www.qq.com'){
$usersig = $this->getSig($this->identifier);
$url = "https://console.tim.qq.com/v4/im_open_login_svc/account_import?sdkappid={$this->sdkappid}&identifier={$this->identifier}&usersig={$usersig}&random=99999999&contenttype=json";
$data = [
'Identifier' => (string)$user_id,
'Nick' => $nickname,
'FaceUrl' => $head_photo,
];
$client = new Client();
$response = $client->request('POST',$url,['json'=>$data]);
return json_decode($response->getBody()->getContents(),true);
}
/**
* 导入用户账号
*/
public function importUser($user){
//导入腾讯im账号(先查询是否存在,不存在就导入)
$portrait_get_res = $this->portraitGet('user_'.$user['id']);//查询
if(!isset($portrait_get_res['ErrorCode'])){
$this->setError('拉取资料接口报错');
return false;
}
if($portrait_get_res['ErrorCode'] != 0 && $portrait_get_res['ErrorCode'] != 40003){
$this->setError($portrait_get_res['ErrorInfo']);
return false;
}
if($portrait_get_res['UserProfileItem'][0]['ResultCode'] == 40003){
$import_res = $this->accountImport('user_'.$user['id'],$user['nickname'],$user['avatar']);//导入
if(!isset($import_res['ErrorCode'])){
$this->setError('导入账号失败');
return false;
}
if($import_res['ErrorCode'] != 0){
$this->setError($import_res['ErrorInfo']);
return false;
}
}
return true;
}
/**
* 导入医生账号
*/
public function importDoctor($doctor){
//导入腾讯im账号(先查询是否存在,不存在就导入)
$portrait_get_res = $this->portraitGet('doctor_'.$doctor['id']);//查询
if(!isset($portrait_get_res['ErrorCode'])){
$this->setError('拉取资料接口报错');
return false;
}
if($portrait_get_res['ErrorCode'] != 0 && $portrait_get_res['ErrorCode'] != 40003){
$this->setError($portrait_get_res['ErrorInfo']);
return false;
}
if($portrait_get_res['UserProfileItem'][0]['ResultCode'] == 40003){
$import_res = $this->accountImport('doctor_'.$doctor['id'],$doctor['nickname'],$doctor['avatar']);//导入
if(!isset($import_res['ErrorCode'])){
$this->setError('导入账号失败');
return false;
}
if($import_res['ErrorCode'] != 0){
$this->setError($import_res['ErrorInfo']);
return false;
}
}
return true;
}
/**
* 帐号删除
*/
public function accountDelete($user_id1){
$usersig = $this->getSig($this->identifier);
$url = "https://console.tim.qq.com/v4/im_open_login_svc/account_delete?sdkappid={$this->sdkappid}&identifier={$this->identifier}&usersig={$usersig}&random=99999999&contenttype=json";
$data = [
'DeleteItem' => [
["UserID"=>(string)$user_id1],
]
];
$client = new Client();
$response = $client->request('POST',$url,['json'=>$data]);
$res = json_decode($response->getBody()->getContents(),true);
if(!isset($res['ErrorCode'])){
$this->setError('帐号删除失败');
return false;
}
if($res['ErrorCode'] != 0){
$this->setError($res['ErrorInfo']);
return false;
}
return true;
}
/**
* 拉取资料
*/
public function portraitGet($user_id){
$usersig = $this->getSig($this->identifier);
$url = "https://console.tim.qq.com/v4/profile/portrait_get?sdkappid={$this->sdkappid}&identifier={$this->identifier}&usersig={$usersig}&random=99999999&contenttype=json";
$data = [
'To_Account' => [(string)$user_id],
'TagList' => ['Tag_Profile_IM_Nick'],
];
$client = new Client();
$response = $client->request('POST',$url,['json'=>$data]);
return json_decode($response->getBody()->getContents(),true);
}
/**
* 设置资料
*/
public function portraitSet($user_id,$profile_item){
$usersig = $this->getSig($this->identifier);
$url = "https://console.tim.qq.com/v4/profile/portrait_set?sdkappid={$this->sdkappid}&identifier={$this->identifier}&usersig={$usersig}&random=99999999&contenttype=json";
$data = [
"From_Account" => (string)$user_id,
"ProfileItem" => $profile_item
];
$client = new Client();
$response = $client->request('POST',$url,['json'=>$data]);
$res = json_decode($response->getBody()->getContents(),true);
if(!isset($res['ErrorCode'])){
$this->setError('设置资料失败');
return false;
}
if($res['ErrorCode'] != 0){
$this->setError($res['ErrorInfo']);
return false;
}
return true;
}
/**
* 加好友
*/
public function friendAdd($user_id1,$add_friend_item){
$usersig = $this->getSig($this->identifier);
$url = "https://console.tim.qq.com/v4/sns/friend_add?sdkappid={$this->sdkappid}&identifier={$this->identifier}&usersig={$usersig}&random=99999999&contenttype=json";
$data = [
"From_Account" => (string)$user_id1,
"AddFriendItem" => $add_friend_item
];
$client = new Client();
$response = $client->request('POST',$url,['json'=>$data]);
$res = json_decode($response->getBody()->getContents(),true);
if(!isset($res['ErrorCode'])){
$this->setError('加好友失败');
return false;
}
if($res['ErrorCode'] != 0){
$this->setError($res['ErrorInfo']);
return false;
}
return true;
}
/**
* 拉取好友
*/
public function friendGet($user_id1){
$usersig = $this->getSig($this->identifier);
$url = "https://console.tim.qq.com/v4/sns/friend_get?sdkappid={$this->sdkappid}&identifier={$this->identifier}&usersig={$usersig}&random=99999999&contenttype=json";
$data = [
'From_Account' => (string)$user_id1,
'StartIndex' => 0,
'StandardSequence' => 0,
'CustomSequence' => 0
];
$client = new Client();
$response = $client->request('POST',$url,['json'=>$data]);
return json_decode($response->getBody()->getContents(),true);
}
/**
* 删除好友
*/
public function friendDelete($user_id1,$user_id2){
$usersig = $this->getSig($this->identifier);
$url = "https://console.tim.qq.com/v4/sns/friend_delete?sdkappid={$this->sdkappid}&identifier={$this->identifier}&usersig={$usersig}&random=99999999&contenttype=json";
$data = [
'From_Account' => (string)$user_id1,
'To_Account' => [(string)$user_id2],
'DeleteType' => 'Delete_Type_Both',//双向删除好友
];
$client = new Client();
$response = $client->request('POST',$url,['json'=>$data]);
$res = json_decode($response->getBody()->getContents(),true);
if(!isset($res['ErrorCode'])){
$this->setError('删除好友失败');
return false;
}
if($res['ErrorCode'] != 0){
$this->setError($res['ErrorInfo']);
return false;
}
return true;
}
/**
* 发送信息
*/
public function sendMsg($user_id1,$user_id2,$msg){
$usersig = $this->getSig($this->identifier);
$url = "https://console.tim.qq.com/v4/openim/sendmsg?sdkappid={$this->sdkappid}&identifier={$this->identifier}&usersig={$usersig}&random=99999999&contenttype=json";
$data = [
'SyncOtherMachine' => 1,
'From_Account' => (string)$user_id1,
'To_Account' => (string)$user_id2,
'MsgLifeTime' => 60,
'MsgRandom' => 1287657,
'MsgTimeStamp' => 1557387418,
'MsgBody' => [
[
"MsgType" => "TIMTextElem",
"MsgContent" => [
"Text" => (string)$msg
]
]
]
];
$client = new Client();
$response = $client->request('POST',$url,['json'=>$data]);
$res = json_decode($response->getBody()->getContents(),true);
if(!isset($res['ErrorCode'])){
$this->setError('发送信息失败');
return false;
}
if($res['ErrorCode'] != 0){
$this->setError($res['ErrorInfo']);
return false;
}
return true;
}
/**
* 查询单聊消息
*/
public function getRoamMsg($user_id1,$user_id2,$min_time,$max_time,$max_cnt=100){
$usersig = $this->getSig($this->identifier);
$url = "https://console.tim.qq.com/v4/openim/admin_getroammsg?sdkappid={$this->sdkappid}&identifier={$this->identifier}&usersig={$usersig}&random=99999999&contenttype=json";
$data = [
"From_Account" => (string)$user_id1,
"To_Account" => (string)$user_id2,
"MaxCnt" => $max_cnt,
"MinTime" => $min_time,
"MaxTime" => $max_time
];
$client = new Client();
$response = $client->request('POST',$url,['json'=>$data]);
return json_decode($response->getBody()->getContents(),true);
}
/**
* 查询单聊未读消息计数
*/
public function getNoreadMsgNum($user_id1,$user_id2){
$usersig = $this->getSig($this->identifier);
$url = "https://console.tim.qq.com/v4/openim/get_c2c_unread_msg_num?sdkappid={$this->sdkappid}&identifier={$this->identifier}&usersig={$usersig}&random=99999999&contenttype=json";
$data = [
"To_Account" => (string)$user_id1,
"Peer_Account" => [
(string)$user_id2,
]
];
$client = new Client();
$response = $client->request('POST',$url,['json'=>$data]);
return json_decode($response->getBody()->getContents(),true);
}
/**
* 校验好友
*/
public function friendCheck($user_id1,$friend_id_arr){
$usersig = $this->getSig($this->identifier);
$url = "https://console.tim.qq.com/v4/sns/friend_check?sdkappid={$this->sdkappid}&identifier={$this->identifier}&usersig={$usersig}&random=99999999&contenttype=json";
$data = [
"From_Account" => (string)$user_id1,
"To_Account" => $friend_id_arr,
"CheckType" => "CheckResult_Type_Both"
];
$client = new Client();
$response = $client->request('POST',$url,['json'=>$data]);
return json_decode($response->getBody()->getContents(),true);
}
/**
* 设置错误信息
*/
public function setError($error)
{
$this->_error = $error;
return $this;
}
/**
* 获取错误信息
*/
public function getError()
{
return $this->_error ? __($this->_error) : '';
}
}