SystemController.php
3.57 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
<?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);
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]);
}
}