Gobelieve即时通讯服务端接口

张茂勋
2023-12-01

    //公屏聊天
    public function liveChat($params, &$msg = '')
    {
        $user = User::where('id', $params['user_id'])->first();
        if ($user['chat_token'] != null) {
            $gobelieve_token = $user['chat_token'];
        } else {
            $url = 'https://api.gobelieve.io/auth/grant';
            $data = json_encode(array('uid' => (int)$params['user_id'], 'user_name' => $user['nickname']));
            $result = $this->IM($url, $data);
            $json = json_decode($result, true);
            $gobelieve_token = $json['data']['token'];
        }
        return [
            'token' => $gobelieve_token,
            'uid' => (int)$params['user_id']
        ];
    }

    //创建群组
    public function liveChatGroups($params, &$msg = '')
    {
        $url = 'https://api.gobelieve.io/groups';
        $data = json_encode(array('master' => (int)$params['user_id'], 'name' => 'xxxx', 'members' => [(int)$params['user_id']]));
        $result = $this->IM($url, $data);
        $json = json_decode($result, true);
        $group_id = $json['data']['group_id'];
        Broadcast::where('user_id', $params['user_id'])->update(['group_id' => $group_id]);
        return [
            'group_id' => $group_id,
        ];
    }

    //加入群组
    public function joinGroup($params, &$msg = '')
    {
        $url = 'https://api.gobelieve.io/groups/' . $params['group_id'] . '/members';
        $data = json_encode(array('uid' => (int)$params['user_id']));
        $result = $this->IM($url, $data);
        $json = json_decode($result, true);
        if (isset($json['success'])) {
            return true;
        } else {
            $msg = $json['meta']['message'];
            return false;
        }
    }

    //发送消息
    public function groupMessage($params, &$msg = '')
    {
        $url = 'https://api.gobelieve.io/messages/groups';
        $data = json_encode(array('sender' => (int)$params['user_id'], 'receiver' => (int)$params['group_id'], 'content' => $params['content']));
        $this->IM($url, $data);
        return true;
    }

    //离开群组
    public function leaveGroup($params, &$msg = '')
    {
        $url = 'https://api.gobelieve.io/groups/' . $params['group_id'] . '/members/' . $params['user_id'];


        return true;
    }

    protected function IM($url, $data = [])
    {
        $app_id = env('G_APP_ID');
        $app_secret = env('G_APP_SECRET');
        $auth = base64_encode(sprintf('%s:%s', $app_id, md5($app_secret)));
        $header = array("Content-type: application/json", 'Authorization: Basic ' . $auth);
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }
 类似资料: