Index.php
10.3 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\index\controller;
use app\common\controller\Frontend;
use RongCloud\Lib\Utils;
use RongCloud\RongCloud;
use think\Cache;
use think\Db;
use think\Log;
class Index extends Frontend
{
protected $noNeedLogin = '*';
protected $noNeedRight = '*';
protected $layout = '';
public function index()
{
return $this->view->fetch();
}
public function news()
{
$newslist = [];
return jsonp(['newslist' => $newslist, 'new' => count($newslist), 'url' => 'https://www.fastadmin.net?ref=news']);
}
// 融云创建用户、群组
public function test() {
$user_ids = '3,4';
$config = config('rongyun.config'); // 应用配置参数
$RongSDK = new RongCloud($config['appkey'],$config['appsecret']);
$users = Db::name('user')->whereIn('id',$user_ids)->column('id,nickname,avatar,rongyun_id,is_create'); // 获取用户信息
if($users) {
$user_rongyun_ids = [];
$User = $RongSDK->getUser();
foreach ($users as $k=>$v) {
$user_rongyun_ids[] = [
'id' => $v['rongyun_id']
];
if($v['is_create'] == 0) {
// 创建新用户
$portrait = $v['avatar']; // 头像
$user_params = [
'id'=> $v['rongyun_id'],//用户id
'name'=> $v['nickname'],//用户名称
'portrait'=> $portrait //用户头像
];
$user_return = $User->register($user_params);
if($user_return['code'] == 200) {
Db::name('user')->where('id',$v['id'])->update(['is_create'=>1,'rongyun_token'=>$user_return['token']]); // 融云账号状态更新为已创建
}
Log::write(date('Y-m-d H:i') . '用户注册成功,返回结果:' . json_encode($user_return, JSON_UNESCAPED_UNICODE), 'rongyun_log');
}
}
// 创建群组
$chat_id = '';
$chat_name = '';
$Group = $RongSDK->getGroup();
$group_params = [
'id'=> $chat_id,//群组 id
'name'=> $chat_name,//群组名称
'members'=> $user_rongyun_ids //群成员 列表
];
$group_return = $Group->create($group_params);
Log::write(date('Y-m-d H:i') . '创建群组成功,返回结果:' . json_encode($group_return, JSON_UNESCAPED_UNICODE), 'rongyun_log');
return [
'chat_id' => $chat_id,
'chat_name' => $chat_name
];
}
return [];
}
// 创建群组
public function group() {
// 应用配置参数
$config = config('rongyun.config');
$RongSDK = new RongCloud($config['appkey'],$config['appsecret']);
$Group = $RongSDK->getGroup();
$params = [
'id'=> 'xiaowei_group1',//群组 id
'name'=> 'xiaowei_group1',//群组名称
'members'=>[ //群成员 列表
['id'=> 'xiaowei_master']
]
];
Utils::dump("创建群组成功",$Group->create($params));
}
// 群组用户禁言
public function group_dump_add() {
// 应用配置参数
$config = config('rongyun.config');
$RongSDK = new RongCloud($config['appkey'],$config['appsecret']);
$Group = $RongSDK->getGroup()->Gag();
$params = [
'id'=> 'xiaowei_group1',//群组 id
'members'=>[ //禁言人员列表
['id'=> 'xiaowei_master']
],
'minute'=>0 // 禁言时长
];
Utils::dump("添加群组禁言成功",$Group->add($params));
}
// 解除群组用户禁言
public function group_dump_remove() {
// 应用配置参数
$config = config('rongyun.config');
$RongSDK = new RongCloud($config['appkey'],$config['appsecret']);
$Group = $RongSDK->getGroup()->Gag();
$params = [
'id'=> 'xiaowei_group1',//群组 id
'members'=>[ //禁言人员列表
['id'=> 'xiaowei_master']
]
];
Utils::dump("解除禁言成功",$Group->remove($params));
}
// 指定群组用户全部禁言
public function group_mute() {
// 应用配置参数
$config = config('rongyun.config');
$RongSDK = new RongCloud($config['appkey'],$config['appsecret']);
$time = time();
$orders = Db::name('order')->whereBetween('expirationtime',[$time-60,$time])->select(); // 查询已经过期的群聊
$Group = $RongSDK->getGroup()->MuteAllMembers();
foreach ($orders as $k=>$v) {
$params = [
'id'=> $v['chat_id'],//群组 id
];
$return = $Group->add($params);
Log::write(date('Y-m-d H:i') . '添加指定群组全部禁言成功,返回结果:' . json_encode($return, JSON_UNESCAPED_UNICODE), 'rongyun_log');
}
}
public function test_c() {
// $appSecret = config('rongyun.config')['appsecret']; // 开发者平台分配的 App Secret。
// $nonce = $param['nonce']; // 获取随机数。
// $timestamp = $param['signTimestamp']; // 获取时间戳。
// $signature = $param['signature']; // 获取数据签名。
// $local_signature = sha1($appSecret.$nonce.$timestamp); // 生成本地签名。
// if(strcmp($signature, $local_signature)===0){
// echo'OK';
// } else {
// echo 'ERROR';
// }
// $content = str_replace('"','"',$param['content']);
// $content = json_decode($content,true);
// var_dump($content);
}
// 修改已到期订单未过期,并禁言用户
public function over_order() {
$end_time = time();
$start_time = time()-60;
$where = [
'status' => 2,
'expirationtime' => ['between',[$start_time,$end_time]],
'finish_status' => 1,
'is_chargeback' => 2
];
$order_list = Db::name('order')->where($where)->select(); // 查询符合要求的到期订单
foreach ($order_list as $order) {
$update = [
'id' => $order['id'],
'finish_status' => 2
];
Db::name('order')->update($update);
// 群组禁言
$return = rongyun_ban($order);
Log::write(date('Y-m-d H:i') . '添加指定群组全部禁言成功,返回结果:' . $order['chat_id'] . json_encode($return, JSON_UNESCAPED_UNICODE), 'rongyun_log');
}
}
// 修改已到期订单过期14天后
public function xian() {
$where['status'] = ['eq',2];
$where['expirationtime'] = ['<',time()];
$where['finish_status'] = ['in',[2,3,4]];
// 查出所有的过期订单
$order_list = Db::name('order')
->where($where)
->select();
foreach ($order_list as &$v) {
if($v['expirationtime'] + 14*24*60*60 < time()){
Db::name('order')->where('id',$v['id'])->update(['xian'=>2]);
}else{
Db::name('order')->where('id',$v['id'])->update(['xian'=>3]);
}
}
}
// 融云服务端实时消息回调
public function callback() {
$param = $this->request->param();
$appSecret = config('rongyun.config')['appsecret']; // 开发者平台分配的 App Secret。
$nonce = $param['nonce']; // 获取随机数。
$timestamp = $param['signTimestamp']; // 获取时间戳。
$signature = $param['signature']; // 获取数据签名。
$local_signature = sha1($appSecret.$nonce.$timestamp); // 生成本地签名。
if(strcmp($signature, $local_signature)===0){
$order = Db::name('order')->where('chat_id',$param['toUserId'])->find(); // 查询房间是否存在
if($order) {
$content = str_replace('"','"',$param['content']);
Log::write(date('Y-m-d H:i') . '消息记录成功,返回结果:' . json_encode($content, JSON_UNESCAPED_UNICODE), 'rongyun_msg');
$content = json_decode($content,true);
// 处理消息类型
$text = $image = $file = $voice = '';
switch ($param['objectName']) {
case 'RC:TxtMsg':
$type = 1;
$text = $content['content'];
break;
case 'RC:ImgMsg':
$type = 2;
$image = str_replace("&","&",$content['imageUri']);
break;
case 'RC:ImgTextMsg':
$type = 3;
$file = $content['content'];
break;
case 'RC:VcMsg':
$type = 4;
$voice = str_replace("&","&",$content['content']);
break;
default:
$type = 1;
}
// 消息记录
$send_id = Db::name('user')->where(['rongyun_id'=>$param['fromUserId']])->value('id');
$order_chat = [
'order_id' => $order['id'],
'chat_id' => $param['toUserId'],
'send_id' => $send_id,
'receive_id' => $order['teacher_id'],
'type' => $type,
'text' => $text,
'image' => $image,
'file' => $file,
'voice' => $voice,
'more' => json_encode($param,JSON_UNESCAPED_UNICODE),
'createtime' => time(),
'updatetime' => time()
];
$result = Db::name('order_chat')->insertGetId($order_chat);
}
Log::write(date('Y-m-d H:i') . '签名验证成功,返回结果:' . json_encode($param, JSON_UNESCAPED_UNICODE), 'rongyun_msg');
} else {
Log::write(date('Y-m-d H:i') . '签名验证失败,返回结果:' . json_encode($param, JSON_UNESCAPED_UNICODE), 'rongyun_msg');
}
echo json_encode(['code'=>200,'msg'=>'成功']);exit;
}
}