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


}