SystemController.php 5.65 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);*/
            $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;
    }
}