SystemController.php
5.65 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
<?php
namespace backend\controllers;
use Yii;
use app\models\SysConfig;
use app\models\Vest;
/**
* Site controller
*/
class SystemController extends BaseController
{
public function actionServers()
{
$key = 'servers';
if (Yii::$app->request->isPost && trim(Yii::$app->request->post('content'))) {
/*$content = Yii::$app->request->post('content');
$ip_arr = explode(",",trim($content));
$ip_arr = array_map(function($v){return trim($v);},$ip_arr);
$model = SysConfig::findOne(['conf_key'=>$key]);
$model->conf_value = json_encode($ip_arr);
$model->update_time = time();
if($model->save()){
Yii::$app->session->setFlash('success', '保存成功。');
}else{
Yii::$app->session->setFlash('error', '保存失败。');
}
$this->redirect('/system/servers');*/
}else{
/*$model = SysConfig::find()->where(['conf_key' => $key])->one();
$ips = json_decode($model->conf_value, true);*/
$cache_prefix = Yii::$app->params['cache_prefix'];
$ips = Yii::$app->redisIm->smembers($cache_prefix . "server_list");
return $this->render('servers', ['ips' => $ips]);
}
}
public function actionServAddr()
{
$key = 'serv_addr';
if( Yii::$app->request->isPost ){
$save_arr = [];
$content_tcp = trim(Yii::$app->request->post('content_tcp'));
$content_ws = trim(Yii::$app->request->post('content_ws'));
$val_arr_tcp = explode(",",trim($content_tcp));
$val_arr_tcp = array_map(function($v){return trim($v);},$val_arr_tcp);
$val_arr_ws = explode(",",trim($content_ws));
$val_arr_ws = array_map(function($v){return trim($v);},$val_arr_ws);
$save_arr = [
'tcp'=>$val_arr_tcp ? :'',
'websocket'=>$val_arr_ws ? :''
];
$model = SysConfig::findOne(['conf_key'=>$key]);
$model->conf_value = json_encode($save_arr);
$model->update_time = time();
if($model->save()){
Yii::$app->session->setFlash('success', '保存成功。');
}else{
Yii::$app->session->setFlash('error', '保存失败。');
}
$this->redirect('/system/serv-addr');
}else{
$model = SysConfig::find()->where(['conf_key'=>$key])->one();
$data = json_decode($model->conf_value,true);
return $this->render('serv-addr',['data'=>$data]);
}
}
public function actionVest()
{
$cache_prefix = Yii::$app->params['cache_prefix'];
$imVestNums = Yii::$app->redisIm->llen($cache_prefix.'vest_users');
if(Yii::$app->request->isAjax){
set_time_limit(0);
$offset = Yii::$app->request->get('offset');
$limit = Yii::$app->request->get('limit');
$list = Vest::find()
->offset($offset)->limit($limit)
->orderBy(['id'=>SORT_ASC])
->asArray()
->all();
shuffle($list);//打乱数组
foreach($list as $row){
$data = [
'name'=>$row['name'],
'avatar'=>$row['avatar'],
];
$i = Yii::$app->redisIm->lpush($cache_prefix.'vest_users',json_encode($data));
unset($data);
}
$data = [
'code'=>'200',
'msg'=>'',
'data'=>$i-$imVestNums
];
echo json_encode($data);exit;
}
return $this->render('vest',['imVestNums'=>$imVestNums]);
}
public function actionGetaudnums()
{
$cache_prefix = Yii::$app->params['cache_prefix'];
$room_id = trim(Yii::$app->request->post('room_id'));
$ret = [
'audience'=>0,
'like'=>0,
'addup'=>0,
];
if($room_id) {
$keys = [
'audience'=>$cache_prefix.'audience_nums_room_'.$room_id,
'like'=>$cache_prefix.'like_numbers_room_'.$room_id,
'addup'=>$cache_prefix.'addup_num_'.$room_id,
];
$index = array_keys($keys);
$values = $keys ? call_user_func_array(array(Yii::$app->redisIm,'mget'), $keys) : [];
foreach ($values as $k=>$v) {
$ret[$index[$k]] = $v ? :0;
}
}
$data = [
'code'=>'200',
'msg'=>'',
'data'=>$ret
];
echo json_encode($data);exit;
}
public function actionModifycache()
{
$cache_prefix = Yii::$app->params['cache_prefix'];
$room_id = trim(Yii::$app->request->post('room_id'));
$cache_key = trim(Yii::$app->request->post('cache_key'));
$cache_val = trim(Yii::$app->request->post('cache_val'));
if($room_id && $cache_key && $cache_val){
$expire = 86400;
if($cache_key == 'addup'){
$res = Yii::$app->redisIm->setex($cache_prefix.'addup_num_'.$room_id,$expire,$cache_val);
}
if($cache_key == 'like'){
$res = Yii::$app->redisIm->setex($cache_prefix.'like_numbers_room_'.$room_id,$expire,$cache_val);
}
$data = [
'code'=>'200',
'msg'=>'修改成功',
'data'=>$res
];
}else{
$data = [
'code'=>'400',
'msg'=>'参数不能为空',
'data'=>''
];
}
echo json_encode($data);exit;
}
}