Index.php 5.0 KB
<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use RongCloud\Lib\Utils;
use RongCloud\RongCloud;
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');
        }
    }
}