腾讯云 即时通讯IM 接口

该博客展示了使用PHP实现腾讯云即时通讯IM接口的代码。包含导入、删除、查询帐号,失效登录态,查询在线状态,发送单聊和批量单聊消息等功能,还涉及资料、关系链、群组、全局禁言及运营管理等接口实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php

namespace app\miniapi\controller;

use app\miniapi\service\TenImService;
use think\Request;

/**
 * 腾讯云 即时通讯IM 接口
 * Class Im
 * @package app\miniapi\controller
 */
class Im extends Root
{
    protected $tenIm;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->tenIm = new TenImService();
    }


    /**
     * 导入单个帐号
     * [
     * Identifier,管理员帐号
     * Nick,昵称
     * FaceUrl,头像
     * ]
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function accountImport()
    {
        $params = [
            'Identifier' => $this->tenIm->config['identifier'],
            'Nick' => 'hankin',
            'FaceUrl' => '头像地址',
        ];
        $serverName = 'im_open_login_svc';
        $command = 'account_import';

        return $this->tenIm->account($params, $serverName, $command);
    }

    /**
     * 导入多个帐号
     * [
     * Accounts 用户名,单个用户名长度不超过32字节,单次最多导入100个用户名
     * ]
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function multiAccountImport()
    {
        $params = [
            'Accounts' => ["test1", "test2", "test3", "test4", "test5"],
        ];
        $serverName = 'im_open_login_svc';
        $command = 'multiaccount_import';

        return $this->tenIm->account($params, $serverName, $command);
    }

    /**
     * 删除帐号
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function accountDelete()
    {
        $params = [
            "DeleteItem" => [
                ["UserID" => "test1"],
                ["UserID" => "test2"],
            ]
        ];
        $serverName = 'im_open_login_svc';
        $command = 'account_delete';

        return $this->tenIm->account($params, $serverName, $command);
    }

    /**
     * 查询帐号
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function accountCheck()
    {
        $params = [
            "CheckItem" => [
                ["UserID" => "test1"],
                ["UserID" => "test2"],
            ]
        ];
        $serverName = 'im_open_login_svc';
        $command = 'account_check';

        return $this->tenIm->account($params, $serverName, $command);
    }

    /**
     * 失效帐号登录态
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function kick()
    {
        $params = [
            "Identifier" => $this->tenIm->config['identifier']
        ];
        $serverName = 'im_open_login_svc';
        $command = 'kick';

        return $this->tenIm->account($params, $serverName, $command);
    }

    /**
     * 查询帐号在线状态
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function queryState()
    {
        $params = [
            "IsNeedDetail" => 1,
            "To_Account" => ["test1", "test2", "test3"]
        ];
        $serverName = 'openim';
        $command = 'querystate';

        return $this->tenIm->account($params, $serverName, $command);
    }

    /**
     * 单发单聊消息
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function sendMsg()
    {
        $MsgBogy = [["MsgType" => "TIMTextElem", "MsgContent" => ["Text" => "hello world"]]];
        $params = [
            "SyncOtherMachine" => 1,
            "From_Account" => "test1",
            "To_Account" => "test2",
            "MsgLifeTime" => 0,
            "MsgRandom" => mt_rand(0, 4294967295),
            "MsgTimeStamp" => time(),
            "MsgBody" => $MsgBogy
        ];


        $serverName = 'openim';
        $command = 'sendmsg';

        return $this->tenIm->openIm($params, $serverName, $command);
    }


    /**
     * 批量发单聊消息
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function batchSendMsg()
    {

        $MsgBogy = [["MsgType" => "TIMTextElem", "MsgContent" => ["Text" => "hello world"]]];
        $params = [
            "SyncOtherMachine" => 1,
            "From_Account" => "test1",
            "To_Account" => ["test1", "test2"],
            "MsgLifeTime" => 0,
            "MsgRandom" => mt_rand(0, 4294967295),
            "MsgTimeStamp" => time(),
            "MsgBody" => $MsgBogy
        ];
        $serverName = 'openim';
        $command = 'batchsendmsg';

        return $this->tenIm->openIm($params, $serverName, $command);
    }

    /**
     * 导入单聊消息
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function importMsg()
    {

        $MsgBogy = [["MsgType" => "TIMTextElem", "MsgContent" => ["Text" => "hello world"]]];
        $params = [
            "SyncFromOldSystem" => 1,
            "From_Account" => "test1",
            "To_Account" => "test2",
            "MsgLifeTime" => 0,
            "MsgRandom" => mt_rand(0, 4294967295),
            "MsgTimeStamp" => time(),
            "MsgBody" => $MsgBogy
        ];
        $serverName = 'openim';
        $command = 'importmsg';

        return $this->tenIm->openIm($params, $serverName, $command);
    }


    /**
     * 查询单聊消息
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function adminGetroamMsg()
    {

        $params = [
            "From_Account" => "test1",
            "To_Account" => "test2",
            "MaxCnt" => 1,
            "MinTime" => time() - 3600,
            "MaxTime" => time(),
            "MsgKey" => "4229323874_4229323874_1593343367"
        ];
        $serverName = 'openim';
        $command = 'admin_getroammsg';

        return $this->tenIm->openIm($params, $serverName, $command);
    }


    /**
     * 查询单聊消息
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function adminMsgWithDraw()
    {

        $params = [
            "From_Account" => "test1",
            "To_Account" => "test2",
            "MsgKey" => "4229323874_4229323874_1593343367"
        ];
        $serverName = 'openim';
        $command = 'admin_msgwithdraw';

        return $this->tenIm->openIm($params, $serverName, $command);
    }


    /**
     * 设置资料
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function portraitSet()
    {

        $params = [
            "From_Account" => "test1",
            "ProfileItem" => [["Tag" => "Tag_Profile_IM_Nick", "Value" => "My nick"]]
        ];
        $serverName = 'profile';
        $command = 'portrait_set';

        return $this->tenIm->profile($params, $serverName, $command);
    }

    /**
     * 拉取资料
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function portraitGet()
    {

        $params = [
            "To_Account" => ["test1", "test2"],
            "TagList" => ["Tag_Profile_IM_Nick", "Tag_Profile_IM_AllowType", "Tag_Profile_IM_SelfSignature"]
        ];
        $serverName = 'profile';
        $command = 'portrait_get';

        return $this->tenIm->profile($params, $serverName, $command);
    }


    /**
     * 添加好友
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function friendAdd()
    {

        $AddFriendItem = [["To_Account" => "test2", "Remark" => "好基友", "GroupName" => "同事", "AddSource" => "AddSource_Type_Wx", "AddWording" => "您好"]];
        $params = [
            "From_Account" => "test1",
            "AddFriendItem" => $AddFriendItem,
            "AddType" => "Add_Type_Single",
            "ForceAddFlags" => 1
        ];

        $serverName = 'sns';
        $command = 'friend_add';

        return $this->tenIm->sns($params, $serverName, $command);
    }

    /**
     * 导入好友
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function friendImport()
    {

        $addFriendItem = [["To_Account" => "test2", "Remark" => "好基友", "GroupName" => ["同事"], "AddSource" => "AddSource_Type_Wx", "AddWording" => "您好", "AddTime" => time()]];
        $params = [
            "From_Account" => "test1",
            "AddFriendItem" => $addFriendItem
        ];

        $serverName = 'sns';
        $command = 'friend_import';

        return $this->tenIm->sns($params, $serverName, $command);
    }

    /**
     * 更新好友
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function friendUpdate()
    {

        $updateItem = [

            [
                "To_Account" => "test2",
                "SnsItem" => [
                    ["Tag" => "Tag_SNS_IM_Remark", "Value" => "老哥"],
                    ["Tag" => "Tag_SNS_IM_Group", "Value" => ["同学", "朋友"]],
                ]
            ]
        ];
        $params = [
            "From_Account" => "test1",
            "UpdateItem" => $updateItem
        ];

        $serverName = 'sns';
        $command = 'friend_update';

        return $this->tenIm->sns($params, $serverName, $command);
    }

    /**
     * 删除好友
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function friendDelete()
    {

        $params = [
            "From_Account" => "test2",
            "To_Account" => ["test1", "test2"],
            "DeleteType" => "Delete_Type_Single"
        ];


        $serverName = 'sns';
        $command = 'friend_delete';

        return $this->tenIm->sns($params, $serverName, $command);
    }


    /**
     * 删除所有好友
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function friendDeleteAll()
    {

        $params = [
            "From_Account" => "test1",
        ];


        $serverName = 'sns';
        $command = 'friend_delete_all';

        return $this->tenIm->sns($params, $serverName, $command);
    }


    /**
     * 校验好友
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function friendCheck()
    {

        $params = [
            "From_Account" => "test1",
            "To_Account" => ["test1", "test2"],
            "CheckType" => "CheckResult_Type_Both"
        ];


        $serverName = 'sns';
        $command = 'friend_check';

        return $this->tenIm->sns($params, $serverName, $command);
    }

    /**
     * 拉取好友
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function friendGet()
    {

        $params = [
            "From_Account" => "test1",
            "StartIndex" => 0,
            "StandardSequence" => 0,
            "CustomSequence" => 0
        ];


        $serverName = 'sns';
        $command = 'friend_get';

        return $this->tenIm->sns($params, $serverName, $command);
    }

    /**
     * 拉取指定好友
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function friendGetList()
    {

        $params = [
            "From_Account" => "test1",
            "To_Account" => ["test2"],
            "TagList" => ["Tag_Profile_IM_Image", "Tag_Profile_IM_Nick"]
        ];

        $serverName = 'sns';
        $command = 'friend_get_list';

        return $this->tenIm->sns($params, $serverName, $command);
    }

    /**
     * 添加黑名单
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function blackListAdd()
    {

        $params = [
            "From_Account" => "test1",
            "To_Account" => ["test2"]
        ];

        $serverName = 'sns';
        $command = 'black_list_add';

        return $this->tenIm->sns($params, $serverName, $command);
    }


    /**
     * 删除黑名单
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function blackListDelete()
    {

        $params = [
            "From_Account" => "test1",
            "To_Account" => ["test2"]
        ];

        $serverName = 'sns';
        $command = 'black_list_delete';

        return $this->tenIm->sns($params, $serverName, $command);
    }

    /**
     * 拉取黑名单
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function blackListGet()
    {

        $params = [
            "From_Account" => "test1",
            "StartIndex" => 0,
            "MaxLimited" => 30,
            "LastSequence" => 0
        ];

        $serverName = 'sns';
        $command = 'black_list_get';

        return $this->tenIm->sns($params, $serverName, $command);
    }

    /**
     * 校验黑名单
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function blackListCheck()
    {

        $params = [
            "From_Account" => "test1",
            "To_Account" => ["test2"],
            "CheckType" => "BlackCheckResult_Type_Both"
        ];

        $serverName = 'sns';
        $command = 'black_list_check';

        return $this->tenIm->sns($params, $serverName, $command);
    }


    /**
     * 添加分组
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function groupAdd()
    {

        $params = [
            "From_Account" => "test1",
            "GroupName" => ["亲人"]
        ];

        $serverName = 'sns';
        $command = 'group_add';

        return $this->tenIm->sns($params, $serverName, $command);
    }


    /**
     * 删除分组
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function groupDelete()
    {

        $params = [
            "From_Account" => "test1",
            "GroupName" => ["亲人"]
        ];

        $serverName = 'sns';
        $command = 'group_delete';

        return $this->tenIm->sns($params, $serverName, $command);
    }


    /**
     * 创建群组
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function createGroup()
    {

        $params = [
            "Owner_Account" => "test1",
            "Type" => "Public",
            "Name" => "TestGroup"
        ];

        $serverName = 'group_open_http_svc';
        $command = 'create_group';

        return $this->tenIm->group($params, $serverName, $command);
    }

    /**
     * 获取群详细资料
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function getGroupInfo()
    {

        $params = [
            "GroupIdList" => ["@TGS#24LP7FSGG"]
        ];

        $serverName = 'group_open_http_svc';
        $command = 'get_group_info';

        return $this->tenIm->group($params, $serverName, $command);
    }

    /**
     * 获取群详细资料
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function getGroupMemberInfo()
    {

        $params = [
            "GroupId" => "@TGS#24LP7FSGG"
        ];

        $serverName = 'group_open_http_svc';
        $command = 'get_group_member_info';

        return $this->tenIm->group($params, $serverName, $command);
    }

    /**
     * 修改群基础资料
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function modifyGroupBaseInfo()
    {

        $params = [
            "GroupId" => "@TGS#24LP7FSGG",
            "Name" => "hello"
        ];

        $serverName = 'group_open_http_svc';
        $command = 'modify_group_base_info';

        return $this->tenIm->group($params, $serverName, $command);
    }


    /**
     * 增加群成员
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function addGroupMember()
    {

        $params = [
            "GroupId" => "@TGS#24LP7FSGG",
            "Silence" => 1,
            "MemberList" => [["Member_Account" => "test1"], ["Member_Account" => "test2"]]
        ];

        $serverName = 'group_open_http_svc';
        $command = 'add_group_member';

        return $this->tenIm->group($params, $serverName, $command);
    }


    /**
     * 删除群成员
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function deleteGroupMember()
    {

        $params = [
            "GroupId" => "@TGS#2IDHSESGH",
            "Silence" => 1,
            "Reason" => "禁言",
            "MemberToDel_Account" => ["test2"]
        ];

        $serverName = 'group_open_http_svc';
        $command = 'delete_group_member';

        return $this->tenIm->group($params, $serverName, $command);
    }

    /**
     * 修改群成员资料
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function modifyGroupMemberInfo()
    {

        $params = [
            "GroupId" => "@TGS#24LP7FSGG",
            "Member_Account" => "test1",
            "MsgFlag" => "AcceptAndNotify"
        ];

        $serverName = 'group_open_http_svc';
        $command = 'modify_group_member_info';

        return $this->tenIm->group($params, $serverName, $command);
    }


    /**
     * 解散群组
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function destroyGroup()
    {

        $params = [
            "GroupId" => "@TGS#24LP7FSGG"
        ];

        $serverName = 'group_open_http_svc';
        $command = 'destroy_group';

        return $this->tenIm->group($params, $serverName, $command);
    }

    /**
     * 获取用户所加入的群组
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function getJoinedGroupList()
    {

        $params = [
            "Member_Account" => "test1"
        ];

        $serverName = 'group_open_http_svc';
        $command = 'get_joined_group_list';

        return $this->tenIm->group($params, $serverName, $command);
    }


    /**
     * 查询用户在群组中的身份
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function getRoleInGroup()
    {

        $params = [
            "GroupId" => "@TGS#24LP7FSGG",
            "User_Account" => ["test1", "test2"]
        ];

        $serverName = 'group_open_http_svc';
        $command = 'get_role_in_group';

        return $this->tenIm->group($params, $serverName, $command);
    }


    /**
     * 批量禁言和取消禁言
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function forbidSendMsg()
    {

        $params = [
            "GroupId" => "@TGS#24LP7FSGG",
            "Members_Account" => ["test1", "test2"],
            "ShutUpTime" => 3600

        ];

        $serverName = 'group_open_http_svc';
        $command = 'forbid_send_msg';

        return $this->tenIm->group($params, $serverName, $command);
    }

    /**
     * 获取被禁言群成员列表
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function getGroupShuttedUin()
    {

        $params = [
            "GroupId" => "@TGS#24LP7FSGG"
        ];

        $serverName = 'group_open_http_svc';
        $command = 'get_group_shutted_uin';

        return $this->tenIm->group($params, $serverName, $command);
    }

    /**
     * 在群组中发送普通消息
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function sendGroupMsg()
    {

        $text=input('text') ;//消息内容
        $GroupId=input('GroupId');

        $MsgBody = [["MsgType" => "TIMTextElem", "MsgContent" => ["Text" => $text]]];

        $params = [
            "GroupId" => $GroupId,
            "Random" => getRandom(32),
            "MsgBody" => $MsgBody,

        ];

        $serverName = 'group_open_http_svc';
        $command = 'send_group_msg';

        return $this->tenIm->group($params, $serverName, $command);
    }

    /**
     * 在群组中发送系统通知
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function sendGroupSystemNotification()
    {

        $params = [
            "GroupId" => "@TGS#24LP7FSGG",
            "ToMembers_Account" => [],
            "Content" => "Hello World"
        ];

        $serverName = 'group_open_http_svc';
        $command = 'send_group_system_notification';

        return $this->tenIm->group($params, $serverName, $command);
    }

    /**
     * 撤回群消息
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function groupMsgRecall()
    {

        $params = [
            "GroupId" => "@TGS#24LP7FSGG",
            "MsgSeqList" => [["MsgSeq" => 1], ["MsgSeq" => 2]],
            "MsgSeq" => "Hello World"
        ];

        $serverName = 'group_open_http_svc';
        $command = 'group_msg_recall';

        return $this->tenIm->group($params, $serverName, $command);
    }


    /**
     * 转让群主
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function changeGroupOwner()
    {

        $params = [
            "GroupId" => "@TGS#24LP7FSGG",
            "NewOwner_Account" => "test2"
        ];

        $serverName = 'group_open_http_svc';
        $command = 'change_group_owner';

        return $this->tenIm->group($params, $serverName, $command);
    }


    /**
     * 导入群基础资料
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function importGroup()
    {

        $params = [
            "Owner_Account" => "test1",
            "Type" => "Public",
            "GroupId" => "MyFirstGroup",
            "NewOwner_Account" => "test2",
            "Name" => "TestGroup"
        ];

        $serverName = 'group_open_http_svc';
        $command = 'import_group';

        return $this->tenIm->group($params, $serverName, $command);
    }

    /**
     * 导入群消息
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function importGroupMsg()
    {

        $MsgBogy = [["MsgType" => "TIMTextElem", "MsgContent" => ["Text" => "hello world"]]];
        $Msglist = [["From_Account" => "test2", "SendTime" => time(), "Random" => getRandom(32), "MsgBody" => $MsgBogy]];

        $params = [
            "GroupId" => "@TGS#24LP7FSGG",
            "MsgList" => $Msglist
        ];

        $serverName = 'group_open_http_svc';
        $command = 'import_group_msg';

        return $this->tenIm->group($params, $serverName, $command);
    }

    /**
     * 导入群成员
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function importGroupMember()
    {

        $params = [
            "GroupId" => "@TGS#24LP7FSGG",
            "MemberList" => [["Member_Account" => "test2"]]
        ];

        $serverName = 'group_open_http_svc';
        $command = 'import_group_member';

        return $this->tenIm->group($params, $serverName, $command);
    }

    /**
     * 设置成员未读消息计数
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function setUnreadMsgNum()
    {

        $params = [
            "GroupId" => "@TGS#24LP7FSGG",
            "Member_Account" => "test1",
            "UnreadMsgNum" => 5
        ];

        $serverName = 'group_open_http_svc';
        $command = 'set_unread_msg_num';

        return $this->tenIm->group($params, $serverName, $command);
    }

    /**
     * 删除指定用户发送的消息
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function deleteGroupMsgBySender()
    {

        $params = [
            "GroupId" => "@TGS#24LP7FSGG",
            "Sender_Account" => "test1"
        ];

        $serverName = 'group_open_http_svc';
        $command = 'delete_group_msg_by_sender';

        return $this->tenIm->group($params, $serverName, $command);
    }

    /**
     * 拉取群历史消息
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function groupMsgGetSimple()
    {

        $params = [
            "GroupId" => "@TGS#24LP7FSGG",
            "ReqMsgNumber" => "2"
        ];

        $serverName = 'group_open_http_svc';
        $command = 'group_msg_get_simple';

        return $this->tenIm->group($params, $serverName, $command);
    }


    /**
     * 设置全局禁言
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function setNospeaking()
    {

        $params = [
            "Set_Account" => "test2",
            "C2CmsgNospeakingTime" => 3600,
            "GroupmsgNospeakingTime" => 3600
        ];

        $serverName = 'openconfigsvr';
        $command = 'setnospeaking';

        return $this->tenIm->group($params, $serverName, $command);
    }

    /**
     * 查询全局禁言
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function getNospeaking()
    {

        $params = ["Get_Account" => "test2"];

        $serverName = 'openconfigsvr';
        $command = 'getnospeaking';

        return $this->tenIm->openConfigSvr($params, $serverName, $command);
    }

    /**
     * 拉取运营数据
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function getAppinfo()
    {

        $params = ["RequestField" => [
            "ChainIncrease",
            "ChainDecrease"
        ]];

        $serverName = 'openconfigsvr';
        $command = 'getappinfo';

        return $this->tenIm->openConfigSvr($params, $serverName, $command);
    }


    /**
     * 下载最近消息记录
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function getHistory()
    {

        $params = ["ChatType" => "C2C", "MsgTime" => "2020063015"];

        $serverName = 'open_msg_svc';
        $command = 'get_history';

        return $this->tenIm->openConfigSvr($params, $serverName, $command);
    }

}

<?php
/**
 * PhpStorm file
 *
 * @author hankin <hanhanjun@aiotube.com>
 *
 * @link http://www.aiotube.com
 *
 * @copyright Copyright &copy; 2020/6/20 14:08 HangZhou yiqibo Technology Co., Ltd
 */

namespace app\miniapi\service;


use Hedeqiang\TenIM\IM;

/**
 * 腾讯云 即时通讯IM服务
 * @doc  https://cloud.tencent.com/document/product/269/1520
 * Class TenImService
 * @package app\miniapi\service
 */
class TenImService
{
    protected $im;

    public function __construct()
    {
        $config=Config('IM');
        $this->im = new IM($config);
    }
    /**
     * 账号管理
     * @param $params
     * @param $serverName
     * @param $command
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function account($params, $serverName, $command)
    {
        return $this->im->send($serverName, $command, $params);
    }

    /**
     * 单聊消息
     * @param $params
     * @param $serverName
     * @param $command
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function openIm($params, $serverName, $command)
    {
        return $this->im->send($serverName, $command, $params);
    }

    /**
     * 资料管理
     * @param $params
     * @param $serverName
     * @param $command
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function profile($params, $serverName, $command)
    {
        return $this->im->send($serverName, $command, $params);
    }

    /**
     * 关系链管理
     * @param $params
     * @param $serverName
     * @param $command
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function sns($params, $serverName, $command)
    {
        return $this->im->send($serverName, $command, $params);
    }

    /**
     * 群组管理
     * @param $params
     * @param $serverName
     * @param $command
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function group($params, $serverName, $command)
    {
        return $this->im->send($serverName, $command, $params);
    }

    /**
     * 全局禁言管理
     * @param $params
     * @param $serverName
     * @param $command
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function openConfigSvr($params, $serverName, $command)
    {
        return $this->im->send($serverName, $command, $params);
    }

    /**
     * 运营管理
     * @param $params
     * @param $serverName
     * @param $command
     * @return array
     * @throws \Hedeqiang\TenIM\Exceptions\Exception
     * @throws \Hedeqiang\TenIM\Exceptions\HttpException
     */
    public function operation($params, $serverName, $command)
    {
        return $this->im->send($serverName, $command, $params);
    }

}

<?php

/*
 * This file is part of the hedeqiang/ten-im.
 *
 * (c) hedeqiang<laravel_code@163.com>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Hedeqiang\TenIM;

use app\common\model\LogMain;
use Hedeqiang\TenIM\Exceptions\Exception;
use Hedeqiang\TenIM\Exceptions\HttpException;
use Hedeqiang\TenIM\Traits\HasHttpRequest;
use Tencent\TLSSigAPIv2;

class IM
{
    use HasHttpRequest;

    const ENDPOINT_TEMPLATE = 'https://console.tim.qq.com/%s/%s/%s?%s';

    const ENDPOINT_VERSION = 'v4';

    const ENDPOINT_FORMAT = 'json';

    protected $config;

    public function __construct(array $config)
    {
        $this->config = new Config($config);
    }

    /**
     * @param string $servername
     * @param string $command
     * @param array  $params
     *
     * @return array
     *
     * @throws Exception
     * @throws HttpException
     */
    public function send($servername, $command, array $params = [])
    {
        try {
            LogMain::add(['name'=>'Im','content'=>$this->buildEndpoint($servername, $command)]);
            $result = $this->postJson($this->buildEndpoint($servername, $command), $params);
        } catch (\Exception $e) {
            throw new HttpException($e->getMessage(), $e->getCode(), $e);
        }


        if(isset($result['ActionStatus'])){
            if (0 === $result['ErrorCode'] && 'OK' === $result['ActionStatus']) {
                return $result;
            }
        }else{
            if (0 === $result['ErrorCode'] ) {
                return $result;
            }
        }

        throw new Exception('Tim REST API error: '.json_encode($result));
    }

    /**
     * Build endpoint url.
     *
     * @param string $servername
     * @param string $command
     *
     * @return string
     *
     * @throws \Exception
     */
    protected function buildEndpoint(string $servername, string $command): string
    {
        $query = http_build_query([
            'sdkappid' => $this->config->get('sdk_app_id'),
            'identifier' => $this->config->get('identifier'),
            'usersig' => $this->generateSign($this->config->get('identifier')),
            'random' => mt_rand(0, 4294967295),
            'contenttype' => self::ENDPOINT_FORMAT,
        ]);
        LogMain::add(['name'=>'Im','content'=>$query]);
        return \sprintf(self::ENDPOINT_TEMPLATE, self::ENDPOINT_VERSION, $servername, $command, $query);
    }

    /**
     * Generate Sign.
     *
     * @param string $identifier
     * @param int    $expires
     *
     * @return string
     *
     * @throws \Exception
     */
    protected function generateSign(string $identifier, int $expires = 15552000): string
    {
        $api = new TLSSigAPIv2($this->config->get('sdk_app_id'), $this->config->get('secret_key'));

        return  $api->genSig($identifier, $expires);
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值