Index.php 10.3 KB
<?php

namespace app\index\controller;

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

    public function test_c() {
//        $appSecret = config('rongyun.config')['appsecret']; // 开发者平台分配的 App Secret。
//        $nonce = $param['nonce']; // 获取随机数。
//        $timestamp = $param['signTimestamp']; // 获取时间戳。
//        $signature = $param['signature']; // 获取数据签名。
//        $local_signature = sha1($appSecret.$nonce.$timestamp); // 生成本地签名。
//        if(strcmp($signature, $local_signature)===0){
//            echo'OK';
//        } else {
//            echo 'ERROR';
//        }
//        $content = str_replace('&quot;','"',$param['content']);
//        $content = json_decode($content,true);
//        var_dump($content);
    }

    // 修改已到期订单未过期,并禁言用户
    public function over_order() {
        $end_time = time();
        $start_time = time()-60;
        $where = [
            'status' => 2,
            'expirationtime' => ['between',[$start_time,$end_time]],
            'finish_status' => 1,
            'is_chargeback' => 2
        ];
        $order_list = Db::name('order')->where($where)->select(); // 查询符合要求的到期订单
        foreach ($order_list as $order) {
            $update = [
                'id' => $order['id'],
                'finish_status' => 2
            ];
            Db::name('order')->update($update);
            // 群组禁言
            $return = rongyun_ban($order);
            Log::write(date('Y-m-d H:i') . '添加指定群组全部禁言成功,返回结果:' . $order['chat_id'] . json_encode($return, JSON_UNESCAPED_UNICODE), 'rongyun_log');
        }
    }

    // 修改已到期订单过期14天后
    public function xian() {
        $where['status'] = ['eq',2];
        $where['expirationtime'] = ['<',time()];
        $where['finish_status'] = ['in',[2,3,4]];
        // 查出所有的过期订单
        $order_list = Db::name('order')
            ->where($where)
            ->select();
        foreach ($order_list as &$v) {
            if($v['expirationtime'] + 14*24*60*60 < time()){
                Db::name('order')->where('id',$v['id'])->update(['xian'=>2]);
            }else{
                Db::name('order')->where('id',$v['id'])->update(['xian'=>3]);
            }
        }
    }



    // 融云服务端实时消息回调
    public function callback() {
        $param = $this->request->param();
        $appSecret = config('rongyun.config')['appsecret']; // 开发者平台分配的 App Secret。
        $nonce = $param['nonce']; // 获取随机数。
        $timestamp = $param['signTimestamp']; // 获取时间戳。
        $signature = $param['signature']; // 获取数据签名。
        $local_signature = sha1($appSecret.$nonce.$timestamp); // 生成本地签名。
        if(strcmp($signature, $local_signature)===0){
            $order = Db::name('order')->where('chat_id',$param['toUserId'])->find(); // 查询房间是否存在
            if($order) {
                $content = str_replace('&quot;','"',$param['content']);
                Log::write(date('Y-m-d H:i') . '消息记录成功,返回结果:' . json_encode($content, JSON_UNESCAPED_UNICODE), 'rongyun_msg');
                $content = json_decode($content,true);
                // 处理消息类型
                $text = $image = $file = $voice = '';
                switch ($param['objectName']) {
                    case 'RC:TxtMsg':
                        $type = 1;
                        $text = $content['content'];
                        break;
                    case 'RC:ImgMsg':
                        $type = 2;
                        $image = str_replace("&amp;","&",$content['imageUri']);
                        break;
                    case 'RC:ImgTextMsg':
                        $type = 3;
                        $file = $content['content'];
                        break;
                    case 'RC:VcMsg':
                        $type = 4;
                        $voice = str_replace("&amp;","&",$content['content']);
                        break;
                    default:
                        $type = 1;
                }
                // 消息记录
                $send_id = Db::name('user')->where(['rongyun_id'=>$param['fromUserId']])->value('id');
                $order_chat = [
                    'order_id' => $order['id'],
                    'chat_id' => $param['toUserId'],
                    'send_id' => $send_id,
                    'receive_id' => $order['teacher_id'],
                    'type' => $type,
                    'text' => $text,
                    'image' => $image,
                    'file' => $file,
                    'voice' => $voice,
                    'more' => json_encode($param,JSON_UNESCAPED_UNICODE),
                    'createtime' => time(),
                    'updatetime' => time()
                ];
                $result = Db::name('order_chat')->insertGetId($order_chat);
            }
            Log::write(date('Y-m-d H:i') . '签名验证成功,返回结果:' . json_encode($param, JSON_UNESCAPED_UNICODE), 'rongyun_msg');
        } else {
            Log::write(date('Y-m-d H:i') . '签名验证失败,返回结果:' . json_encode($param, JSON_UNESCAPED_UNICODE), 'rongyun_msg');
        }
        echo json_encode(['code'=>200,'msg'=>'成功']);exit;
    }
}