Openim.php
8.7 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
<?php
/**
* Created by PhpStorm.
* User: ty01
* Date: 2022/3/22
* Time: 11:51
*/
namespace app\api\controller\tencentim;
use think\Db;
use think\Request;
class Openim extends Base
{
public $StatusList;
public function __construct(Request $request = null)
{
parent::__construct($request);
$this->StatusList = ["Online" => "在班", "PushOnline" => "下班", "Offline" => "下班"];
}
/**
* 1. 获取腾讯云App管理员帐号的签名
*/
public function getUserSig()
{
return $this->userSig;
}
/**
* 2. 查询帐号在线状态
* 单次限制500条
*/
public function query_online_status($param)
{
// $param = $this->request->param();//前端传json
if (empty($param)) {
return array_callback(false, "参数异常");
}
$res = $this->send("v4/openim/query_online_status", $param);
// file_put_contents("kevinsss.log",date("Ymd H:i:s").json_encode($param,JSON_UNESCAPED_UNICODE).PHP_EOL,FILE_APPEND);
// file_put_contents("kevinsss.log",date("Ymd H:i:s").json_encode($res,JSON_UNESCAPED_UNICODE).PHP_EOL.PHP_EOL,FILE_APPEND);
$result = json_decode($res, true);
if (empty($result) || $result['ActionStatus'] == "FAIL") {
return array_callback(false, "即时通讯接口异常" . $result['ErrorCode'] . $result['ErrorInfo']);
}
$return_data = [];
$QueryResult = $result['QueryResult'];//返回的用户在线状态结构化信息
if (!empty($QueryResult)) {
foreach ($QueryResult as $k => $v) {
$return_data[$v['To_Account']] = ["To_Account" => $v['To_Account'], "Status" => $v['Status'], "StatusCn" => $this->StatusList[$v['Status']]];
}
}
$ErrorList = $result['ErrorList'];//状态查询失败的帐号列表,在此列表中的目标帐号,状态查询失败或目标帐号不存在。若状态全部查询成功,则 ErrorList 为空
if (!empty($ErrorList)) {
foreach ($ErrorList as $kk => $vv) {
$return_data[$vv['To_Account']] = ["To_Account" => $vv['To_Account'], "Status" => "Offline", "StatusCn" => $this->StatusList["Offline"]];
}
}
return array_callback(true, "成功", $return_data);
}
/**
* 2. 查询帐号在线状态
* 单次限制500条
*/
public function api_query_online_status()
{
$param = $this->request->param();
if (empty($param)) {
$this->error("参数异常");
}
$res = $this->send("v4/openim/query_online_status", $param);
$result = json_decode($res, true);
if (empty($result) || $result['ActionStatus'] == "FAIL") {
$this->error("即时通讯接口异常" . $result['ErrorCode'] . $result['ErrorInfo']);
}
$return_data = [];
$QueryResult = $result['QueryResult'];//返回的用户在线状态结构化信息
if (!empty($QueryResult)) {
foreach ($QueryResult as $k => $v) {
$return_data[$v['To_Account']] = ["To_Account" => $v['To_Account'], "Status" => $v['Status'], "StatusCn" => $this->StatusList[$v['Status']]];
}
}
$ErrorList = $result['ErrorList'];//状态查询失败的帐号列表,在此列表中的目标帐号,状态查询失败或目标帐号不存在。若状态全部查询成功,则 ErrorList 为空
if (!empty($ErrorList)) {
foreach ($ErrorList as $kk => $vv) {
$return_data[$vv['To_Account']] = ["To_Account" => $vv['To_Account'], "Status" => "Offline", "StatusCn" => $this->StatusList["Offline"]];
}
}
$this->success("成功", $return_data);
}
public function api_account_import()
{
$param = [
"UserID" => "15587067522"
];
$res = $this->send("v4/im_open_login_svc/kick", $param);
$result = json_decode($res, true);
if (empty($result) || $result['ActionStatus'] == "FAIL") {
$this->error("即时通讯接口异常" . $result['ErrorCode'] . $result['ErrorInfo']);
}
$this->success("成功", $result);
}
/**
* 3. 删除帐号
*/
public function api_account_delete()
{
$param = $this->request->param();
if (empty($param)) {
$this->error("参数异常");
}
$res = $this->send("v4/im_open_login_svc/account_delete", $param);
$result = json_decode($res, true);
if (empty($result) || $result['ActionStatus'] == "FAIL") {
$this->error("即时通讯接口异常" . $result['ErrorCode'] . $result['ErrorInfo']);
}
$this->success("成功", $result);
}
/**
* 批量替换用户
*/
public function update_im_userinfo(){
$data = Db::name("inspection_staff")->alias("a")->join("user b","b.id=a.user_id","LEFT")
->field("a.user_id,a.staff_name as name,a.mobile,b.avatar")->select();
if(!empty($data)){
foreach ($data as $k=>$v){
if(empty($v['avatar'])){
Db::name("user")->where("id",$v['user_id'])->update(["avatar"=>"/headlogo.png"]);
}
$v['avatar'] =$v['avatar']?full_image_kevin($v['avatar']):full_image_kevin("/headlogo.png");
$param_data = [];
$param_data = [
"From_Account" => "yhwk_".$v['mobile'],
"ProfileItem" =>
[
[
"Tag" => "Tag_Profile_IM_Image",
"Value" => $v['avatar']
],
[
"Tag"=>"Tag_Profile_IM_Nick",
"Value" => $v['name']
]
]
];
$res = $this->send("v4/profile/portrait_set", $param_data);
$result = json_decode($res, true);
dump($v['mobile']);
dump($result);
dump("----------------------");
}
}
}
/**
* 4. 设置用户头像
*/
public function portrait_set($param)
{
if (empty($param)) {
return array_callback(false, "参数异常");
}
$param_data = [
"From_Account" => $param['mobile'],
"ProfileItem" =>
[
[
"Tag" => "Tag_Profile_IM_Image",
"Value" => $param['avatar']
],
[
"Tag"=>"Tag_Profile_IM_Nick",
"Value" => $param['name']
]
]
];
// file_put_contents("kevin_head.log", json_encode($param_data) . PHP_EOL . PHP_EOL, FILE_APPEND);
$res = $this->send("v4/profile/portrait_set", $param_data);
$result = json_decode($res, true);
if (empty($result) || $result['ActionStatus'] == "FAIL") {
return array_callback(false, "即时通讯接口异常" . $result['ErrorCode'] . $result['ErrorInfo']);
}
return array_callback(true, "头像设置成功", $result);
}
/**
* 5. 批量发单聊消息
*/
public function batchsendmsg($param)
{
// $param = $this->request->param();
if (empty($param)) {
// $this->error("参数异常");
return array_callback(false, "参数异常");
}
$param_data = [
"SyncOtherMachine" => 2, // 消息不同步至发送方
// "MsgLifeTime"=>0,//只发给在线用户
"To_Account" => $param['to_user_list'],
// "MsgSeq" => 1, // 消息序列号
"MsgRandom" => mt_rand(10000000, 99999999), // 消息随机数
"MsgBody" => [ // 消息
[
"MsgType" => "TIMTextElem", // 消息类型,TIMTextElem为文本消息
"MsgContent" => [
"Text" => $param['text'] // 消息文本
]
]
],
"CloudCustomData" => "您有新的信息待查看"
];
$res = $this->send("v4/openim/batchsendmsg", $param_data);
$result = json_decode($res, true);
if (empty($result) || $result['ActionStatus'] == "FAIL") {
// $this->error("IM接口异常" . $result['ErrorCode'] . $result['ErrorInfo']);
return array_callback(false, "IM接口异常" . $result['ErrorCode'] . $result['ErrorInfo']);
}
// $this->success("IM发送消息成功", $result);
return array_callback(true, "IM发送消息成功", $result);
}
}