Browse Source

腾讯im发送消息

lizhen_gitee 3 years ago
parent
commit
e54670187a
3 changed files with 1292 additions and 0 deletions
  1. 887 0
      application/index/controller/Tenim.php
  2. 332 0
      extend/getusersig/getusersig.php
  3. 73 0
      extend/tencentim/tencentim.php

+ 887 - 0
application/index/controller/Tenim.php

@@ -0,0 +1,887 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use think\Request;
+use getusersig\getusersig;
+use tencentim\tencentim;
+use Redis;
+use think\Db;
+
+/**
+ * 腾讯im接口
+ */
+class Tenim extends Api
+{
+    protected $noNeedLogin = ["callback",'test','createIMGroup','updateRoomInfo','setImManage','sendMessageToUser','outMemberFromRoom'];
+    protected $noNeedRight = ['*'];
+
+
+    public function test() {
+        $groupinfo = $this->getGroupInfo("112");
+        print_r($groupinfo);
+        exit;
+        // 获取所有派对信息
+        $partyList = \app\common\model\Party::field("id,user_id,party_name")->where(["status"=>1])->order("id","asc")->select();
+        foreach($partyList as $k => $v) {
+            if($k>=140 && $k<180) {
+                $groupinfo = $this->getGroupInfo((string)$v["id"]);
+                print_r($groupinfo);
+//                $groupinfo = json_encode($groupinfo);
+//                Db::table("hx_aaaa")->insert(["party_id"=>$v["id"],"user_id"=>$v["user_id"],"party_name"=>$v["party_name"],"json"=>$groupinfo]);
+            }
+
+        }
+exit;
+//        $this->success("",$partyList);
+    }
+
+    /**
+     * 创建im群组
+     */
+    public function createIMGroup() {
+
+
+
+        $random = rand(10000000,99999999);
+        $usersig = $this->usersig("administrator");
+        // 获取配置信息
+        $config = config("tencent_im");
+        $url = "https://console.tim.qq.com/v4/group_open_http_svc/create_group";
+        $url .= "?sdkappid=".$config["sdkappid"];
+        $url .= "&identifier=administrator";
+        $url .= "&usersig=".$usersig;
+        $url .= "&random==".$random;
+        $url .= "&contenttype=json";
+        $tencentObj = new tencentim($url);
+
+        // 获取群im信息
+        $imInfo = Db::table("hx_aaaa")->where(["party_id"=>112])->select();
+        if($imInfo) foreach($imInfo as $k => $v) {
+//            if($k>=160 && $k<200) {
+                $grouparr = json_decode($v["json"], true);
+
+                $grouparr = $grouparr["GroupInfo"][0];
+                $AppDefinedData = $grouparr["AppDefinedData"];
+                $data = [];
+                $data["Owner_Account"] = (string)$v["user_id"];
+                $data["Type"] = "AVChatRoom";
+                $data["GroupId"] = (string)$v["party_id"];
+                $data["Name"] = $this->substr((string)$v["party_name"],0,7);
+                $AppDefinedDataNew = [
+                    [
+                        "Key" => "roomInfo",
+                        "Value" => "sssssss"
+                    ],
+                    [
+                        "Key" => "seat0",
+                        "Value" => '{"mute":false,"status":0,"user":""}'
+                    ],
+                    [
+                        "Key" => "seat1",
+                        "Value" => $AppDefinedData[2]["Value"]
+                    ],
+                    [
+                        "Key" => "seat2",
+                        "Value" => $AppDefinedData[3]["Value"]
+                    ],
+                    [
+                        "Key" => "seat3",
+                        "Value" => $AppDefinedData[4]["Value"]
+                    ],
+                    [
+                        "Key" => "seat4",
+                        "Value" => $AppDefinedData[5]["Value"]
+                    ],
+                    [
+                        "Key" => "seat5",
+                        "Value" => $AppDefinedData[6]["Value"]
+                    ],
+                    [
+                        "Key" => "seat6",
+                        "Value" => $AppDefinedData[7]["Value"]
+                    ],
+                    [
+                        "Key" => "seat7",
+                        "Value" => $AppDefinedData[8]["Value"]
+                    ],
+                    [
+                        "Key" => "seat8",
+                        "Value" => $AppDefinedData[9]["Value"]
+                    ]
+                ];
+                $data["AppDefinedData"] = json_encode($AppDefinedDataNew);
+
+                $groupInfo = $tencentObj->toSend($data);
+
+                if($groupInfo["ActionStatus"] != 'OK') {
+                    echo $v["party_id"];
+                    break;
+                }
+//            }
+        }
+
+        print_r($groupInfo);exit;
+    }
+
+    public function substr(string $string, int $start, int $length = null): string
+    {
+        return mb_substr($string, $start, $length, 'UTF-8');
+    }
+
+
+    /**
+     * 更新房间信息
+     */
+    public function updateRoomInfo() {
+        $random = rand(10000000, 99999999);
+        $usersig = $this->usersig("administrator");
+        // 获取配置信息
+        $config = config("tencent_im");
+        $url = "https://console.tim.qq.com/v4/group_open_http_svc/modify_group_base_info";
+        $url .= "?sdkappid=" . $config["sdkappid"];
+        $url .= "&identifier=administrator";
+        $url .= "&usersig=" . $usersig;
+        $url .= "&random=" . $random;
+        $url .= "&contenttype=json";
+        $tencentObj = new tencentim($url);
+
+        // 获取群im信息
+        $imInfo = Db::table("hx_aaaa")->select();
+        if($imInfo) foreach($imInfo as $k => $v) {
+            if($k>=120 && $k<180) {
+                $grouparr = json_decode($v["json"], true);
+                $groupsites = $grouparr["GroupInfo"];
+
+                // 循环房间
+                if ($groupsites) foreach ($groupsites as $k => $v) {
+                    // 循环座位
+                    $data = [];
+                    foreach ($v["AppDefinedData"] as $m => $n) {
+                        // 解析字段信息
+                        $siteInfo = json_decode($n["Value"], true);
+                        $data["AppDefinedData"][] = [
+                            "Key" => $n["Key"],
+                            "Value" => $n["Value"],
+                        ];
+
+                    }
+                    if (!empty($data)) {
+                        $data["GroupId"] = $v["GroupId"];
+                        $res = $tencentObj->toSend($data);
+                    } else {
+                        echo "error!";
+                        break;
+                    }
+                }
+            }
+        }
+        print_r($res);
+        exit;
+    }
+
+    /**
+     * 获取群组信息
+     */
+    public function getGroupInfo($party_id) {
+        $random = rand(10000000,99999999);
+        $usersig = $this->usersig("administrator");
+        // 获取配置信息
+        $config = config("tencent_im");
+        $url = "https://console.tim.qq.com/v4/group_open_http_svc/get_group_info";
+        $url .= "?sdkappid=".$config["sdkappid"];
+        $url .= "&identifier=administrator";
+        $url .= "&usersig=".$usersig;
+        $url .= "&random==".$random;
+        $url .= "&contenttype=json";
+        $tencentObj = new tencentim($url);
+        $data = [];
+        $data["GroupIdList"] = [$party_id];
+        $data["ResponseFilter"] = ["AppDefinedDataFilter_Group"=>[]];
+        $groupInfo = $tencentObj->toSend($data);
+        return $groupInfo;
+    }
+
+    /**
+     * 获取用户所加入的群组
+     */
+    public function getJoindGroupList($user_id) {
+        $random = rand(10000000,99999999);
+        $usersig = $this->usersig("administrator");
+        // 获取配置信息
+        $config = config("tencent_im");
+        $url = "https://console.tim.qq.com/v4/group_open_http_svc/get_joined_group_list";
+        $url .= "?sdkappid=".$config["sdkappid"];
+        $url .= "&identifier=administrator";
+        $url .= "&usersig=".$usersig;
+        $url .= "&random=".$random;
+        $url .= "&contenttype=json";
+
+        $tencentObj = new tencentim($url);
+        $data = [];
+        $data["Member_Account"] = $user_id;
+        $data["WithHugeGroups"] = 1;
+        $data["GroupType"] = 'AVChatRoom';
+        $data["ResponseFilter"] = ["GroupBaseInfoFilter" => ['GroupId'] ];
+        $groupInfo = $tencentObj->toSend($data);
+//        \app\common\model\Test::update(["content"=>json_encode($groupInfo)],["id"=>1]);
+        return $groupInfo;
+    }
+//"https:\/\/console.tim.qq.com\/v4\/group_open_http_svc\/get_joined_group_list?sdkappid=1400458032&identifier=administrator&usersig=eJyrVgrxCdYrSy1SslIy0jNQ0gHzM1NS80oy0zLBwqamUNHilOzEgoLMFCUrQxMDAxNTCwNjI4hMakVBZlEqUNzU1NTIwMAAIlqSmQsSMzO0MDY0NDOwhJqSmQ40NNwowsvMzzXKw8wsLNIwRr9S28giPKjMPSkpKLPcLcfFPd3TO7swz9fXOdLTVqkWADrXLzo_&random=73931990&contenttype=json"
+//    /**
+//     * 删除群成员
+//     */
+//    public function delMemberFromGroup($user_id,$party_id) {
+//        $random = rand(10000000,99999999);
+//        $usersig = $this->usersig($user_id);
+//        // 获取配置信息
+//        $config = config("tencent_im");
+//        $url = "https://console.tim.qq.com/v4/group_open_http_svc/delete_group_member";
+//        $url .= "?sdkappid=".$config["sdkappid"];
+//        $url .= "&identifier=administrator";
+//        $url .= "&usersig=".$usersig;
+//        $url .= "&random=".$random;
+//        $url .= "&contenttype=json";
+//        $tencentObj = new tencentim($url);
+//        $data = [];
+//        $data["GroupId"] = $party_id;
+//        $data["Silence"] = 1;
+//        $data["MemberToDel_Account"] = [$user_id];
+//        $res = $tencentObj->toSend($data);
+//        \app\common\model\Test::update(["content"=>json_encode($res)],["id"=>1]);
+//        return $res;
+//    }
+
+    /**
+     * 设置管理员(自定义消息)
+     */
+    public function setImManage($manage_user_id,$party_id,$status=0) {
+        $random = rand(10000000,99999999);
+        $usersig = $this->usersig("administrator");
+        // 获取配置信息
+        $config = config("tencent_im");
+        $url = "https://console.tim.qq.com/v4/group_open_http_svc/modify_group_base_info";
+        $url .= "?sdkappid=".$config["sdkappid"];
+        $url .= "&identifier=administrator";
+        $url .= "&usersig=".$usersig;
+        $url .= "&random=".$random;
+        $url .= "&contenttype=json";
+        $tencentObj = new tencentim($url);
+        // 获取群组信息
+        $groupInfo = $this->getGroupInfo($party_id);
+        if($groupInfo["GroupInfo"][0]["ErrorCode"] > 0) return false;
+        $roomInfoData = $groupInfo["GroupInfo"][0]["AppDefinedData"];
+        $roomInfo = [];
+        if($roomInfoData) foreach($roomInfoData as $k => $v) {
+            if($v["Key"] == "roomInfo") $roomInfo = $v["Value"];
+        }
+        $roomInfo = json_decode($roomInfo,true);
+        // 更新的信息
+        if(isset($roomInfo["managers"])) {
+            if(!$roomInfo["managers"]) $roomInfo["managers"] = [];
+        }
+        if ($status==1){
+            array_push($roomInfo["managers"],$manage_user_id);
+            $roomInfo["managers"] = array_unique($roomInfo["managers"]);
+        }else{
+            foreach ($roomInfo["managers"] as $k => $v){
+                if ($v == $manage_user_id) {
+                    unset($roomInfo["managers"][$k]);
+                    break;
+                }
+            }
+        }
+
+        $data = [];
+        $data["GroupId"] = $party_id;
+        $data["AppDefinedData"][0] = [
+            "Key" => "roomInfo",
+            "Value" => json_encode($roomInfo),
+//            "Value" => json_encode(json_decode('{"bg":"","cover":"","isScreen":0,"managers":["14","29"],"needRequest":0,"onMode":2,"ownerId":"55","ownerName":"过户手续","pwd":"","roomName":"告白气球~满眼是✨✨","seatSize":9,"silencers":[]}',true)),
+        ];
+        if(isset($roomInfo["ownerId"]) && isset($roomInfo["roomName"])) {
+            $tencentObj->toSend($data);
+        }
+
+    }
+    /**
+     * 强制下线
+     */
+    public function downSite($user_id,$groupsites) {
+        $random = rand(10000000,99999999);
+        $usersig = $this->usersig("administrator");
+        // 获取配置信息
+        $config = config("tencent_im");
+        $url = "https://console.tim.qq.com/v4/group_open_http_svc/modify_group_base_info";
+        $url .= "?sdkappid=".$config["sdkappid"];
+        $url .= "&identifier=administrator";
+        $url .= "&usersig=".$usersig;
+        $url .= "&random=".$random;
+        $url .= "&contenttype=json";
+        $tencentObj = new tencentim($url);
+
+        // 先更新下麦时间
+        $update = [];
+        $update["offsite_time"] = time();
+        $update["status"] = 2;
+        \app\common\model\UserOnsiteTime::update($update,["user_id"=>$user_id,"status"=>1]);
+
+        // 循环房间
+        if($groupsites) foreach($groupsites as $k => $v) {
+            // 循环座位
+            $data = [];
+            foreach($v["AppDefinedData"] as $m => $n) {
+                // 解析字段信息
+                $siteInfo = json_decode($n["Value"],true);
+                if($m != 0 && isset($siteInfo["status"]) && $siteInfo["status"] == 1 && isset($siteInfo["status"]) && $siteInfo["user"] == $user_id) {
+                    $data["AppDefinedData"][] = [
+                        "Key" => $n["Key"],
+                        "Value" => '{"mute":false,"status":0,"user":""}',
+                    ];
+                }
+            }
+            if(!empty($data)) {
+                $data["GroupId"] = $v["GroupId"];
+                return $tencentObj->toSend($data);
+            }
+        }
+    }
+
+    /**
+     * 踢出房间内所有用户
+     */
+    public function outMemberFromRoom($party_id) {
+        if($party_id <= 0) return false;
+        $message = [];
+        $message["type"] = "91";
+        $message["content"] = "房间已被管理员关闭!详情请联系客服!";
+
+        $msgData = [];
+        $msgData["version"] = "1.0";
+        $msgData["action"] = 301;
+        $msgData["command"] = "";
+        $msgData["message"] = json_encode($message);
+
+
+        $random = rand(10000000,99999999);
+        $usersig = $this->usersig("administrator");
+        // 获取配置信息
+        $config = config("tencent_im");
+        $url = "https://console.tim.qq.com/v4/group_open_http_svc/send_group_msg";
+        $url .= "?sdkappid=".$config["sdkappid"];
+        $url .= "&identifier=administrator";
+        $url .= "&usersig=".$usersig;
+        $url .= "&random=".$random;
+        $url .= "&contenttype=json";
+        $tencentObj = new tencentim($url);
+
+        $data = [];
+        $data["GroupId"] = $party_id;
+        $data["Random"] = rand(1000000,9999999);
+
+        $data["MsgBody"][] = [
+            "MsgType" => "TIMCustomElem",
+            "MsgContent" => [
+                "Data"=> json_encode($msgData)
+            ],
+        ];
+        $tencentObj->toSend($data);
+    }
+
+    /**
+     * 获取usersig签名
+     */
+    public function getUsersig() {
+        $user_id = $this->auth->id;
+        $this->success("获取成功!",$this->usersig($user_id));
+    }
+
+
+    /**
+     * 获取usersig签名-具体操作
+     */
+    private function usersig($user_id) {
+        // 获取配置信息
+        $config = config("tencent_im");
+        $usersigObj = new getusersig($config["sdkappid"],$config["key"]);
+        $usersig = $usersigObj->genUserSig($user_id);
+        return $usersig;
+    }
+
+
+    /**
+     * 强制下麦
+     */
+    public function updateSiteInfo() {
+        $user_id = $this->request->request("user_id");// 用户ID
+        if(!$user_id) $this->error("参数缺失!");
+
+        // 更新用户在线状态为离线
+        \app\common\model\User::update(["is_online"=>0],["id"=>$user_id]);
+        \app\common\model\User::update(["is_live"=>0],["id"=>$user_id]);
+        \app\common\model\Party::update(["is_online"=>0],["user_id"=>$user_id]);
+
+        // 获取用户所在群信息
+        $groupids = $this->getJoindGroupList($user_id);
+
+        if($groupids["ErrorCode"] == 0) {
+            $grouplist = $groupids["GroupIdList"];
+            $groupsites = [];
+            if($grouplist) foreach($grouplist as $k => $v) {
+                // 获取群组消息
+                $groupInfo = $this->getGroupInfo($v["GroupId"]);
+
+                if($groupInfo["ErrorCode"] == 0) $groupsites[] = $groupInfo["GroupInfo"][0];
+
+                // 踢出用户在线组
+                $redis = new Redis();
+                $redisconfig = config("redis");
+                $redis->connect($redisconfig["host"], $redisconfig["port"]);
+                $redis->HDel("online_".$v["GroupId"],$user_id);
+            }
+            // 强制下麦
+            $this->downSite($user_id,$groupsites);
+        }
+
+        $this->success("状态更新成功!");
+
+    }
+
+    /**
+     * 更新麦位前四
+     */
+    public function updatePosition($user_id,$party_id) {
+        // 获取用户头像
+        $userInfo = \app\common\model\User::field("avatar")->where(["id"=>$user_id])->find();
+        $userAvatar = isset($userInfo["avatar"])?$userInfo["avatar"]:"";
+
+        $redis = new Redis();
+        $redisconfig = config("redis");
+        $redis->connect($redisconfig["host"], $redisconfig["port"]);
+        $redisData = $redis->get("party_".$party_id);
+        if(!$redisData) {
+            return true;
+        }
+        $partyInfo = json_decode($redisData,true);
+
+        // 遍历已有头像
+        $partyuser = isset($partyInfo["party_user"])?$partyInfo["party_user"]:"";
+        if(is_array($partyuser)) foreach($partyuser as $k => $v) if($v === $userAvatar) unset($partyInfo["party_user"][$k]);
+
+        $redis->set("party_".$party_id,json_encode($partyInfo));
+
+    }
+
+
+    /**
+     * 取消排麦
+     */
+    public function cancelLineUp($user_id,$party_id) {
+        if (!$party_id || !$user_id) {
+            return false;
+        }
+        $redis = new Redis();
+        $redisconfig = config("redis");
+        $redis->connect($redisconfig["host"], $redisconfig["port"]);
+        $data=unserialize($redis->hGet("party_lineup",$party_id));
+        // 更改红点排麦数量
+        $num = 0;
+        $send = false;
+        if($data) foreach($data as $k => $v) {
+            if($v["user_id"] == $user_id) {
+                $send = true;
+                unset($data[$k]);
+                break;
+            }
+        }
+        $redis->hSet("party_lineup",$party_id,serialize($data));
+
+        if($send) {
+            is_array($data) &&  $num = count($data);
+
+            $random = rand(10000000,99999999);
+            $usersig = $this->usersig("administrator");
+            // 获取配置信息
+            $config = config("tencent_im");
+            $url = "https://console.tim.qq.com/v4/group_open_http_svc/send_group_msg";
+            $url .= "?sdkappid=".$config["sdkappid"];
+            $url .= "&identifier=administrator";
+            $url .= "&usersig=".$usersig;
+            $url .= "&random=".$random;
+            $url .= "&contenttype=json";
+            $tencentObj = new tencentim($url);
+
+            $data = [];
+            $data["GroupId"] = $party_id;
+            $data["Random"] = rand(1000000,9999999);
+            $message = [
+                "message" => json_encode(["type"=>23,"content"=>$num]),
+                "version" => "1.0",
+                "action" => 301,
+                "command" => ""
+            ];
+            $data["MsgBody"][] = [
+                "MsgType" => "TIMCustomElem",
+                "MsgContent" => [
+                    "Data"=> json_encode($message)
+                ],
+            ];
+            $tencentObj->toSend($data);
+        }
+
+    }
+
+    /**
+     * 回调
+     */
+    public function callback() {
+        $input = file_get_contents("php://input"); // 主题信息
+        $input = json_decode($input,true);
+        $CallbackCommand = $input["CallbackCommand"];
+        switch ($CallbackCommand) {
+            case 'State.StateChange': // 用户在线状态变更
+                $info = $input["Info"];
+                if($info["Action"] == "Login") { // 用户登录
+                    $loginToday = Db::name('user')->where('id',$info["To_Account"])->whereTime('onlinetime', 'today')->find();
+                    if ($loginToday){
+                        \app\common\model\User::update(["is_online"=>1,"onlinetime"=>time()],["id"=>$info["To_Account"]]);
+                    }else{
+                        \app\common\model\User::update(["is_online"=>1,"onlinetime"=>time(),'login_day'=>Db::raw('login_day+1')],["id"=>$info["To_Account"]]);
+                    }
+
+                    $res = [];
+                    $res["ActionStatus"] = "OK";
+                    $res["ErrorCode"] = 0;
+                    $res["ErrorInfo"] = "";
+                    echo json_encode($res);exit;
+                } elseif($info["Action"] == "Logout" || $info["Action"] == "Disconnect") { // 用户退出 不需要管什么原因
+                    // 更新用户在线状态为离线
+                    \app\common\model\User::update(["is_online"=>0],["id"=>$info["To_Account"]]);
+                    \app\common\model\User::update(["is_live"=>0],["id"=>$info["To_Account"]]);
+
+                    // 获取用户在派对直播间情况信息
+                    $redis = new Redis();
+                    $redisconfig = config("redis");
+                    $redis->connect($redisconfig["host"], $redisconfig["port"]);
+                    $livingUserPartyId = $redis->hGet("livingUser", $info["To_Account"]);
+                    if ($livingUserPartyId) {
+                        // 扣除在线用户在房间情况
+                        $redis->hDel("livingUser", $info["To_Account"]);
+                        $redis->HDel("online_" . $livingUserPartyId, $info["To_Account"]);
+                    }
+
+                    // 获取用户所在群信息
+                    $groupids = $this->getJoindGroupList($info["To_Account"]);
+//                    \app\common\model\Test::update(["content"=>json_encode($groupids)],["id"=>1]);
+                    if($groupids["ErrorCode"] == 0) {
+                        $grouplist = $groupids["GroupIdList"];
+                        $groupsites = [];
+                        if($grouplist) foreach($grouplist as $k => $v) {
+                            // 获取群组消息
+                            $groupInfo = $this->getGroupInfo($v["GroupId"]);
+
+                            if($groupInfo["ErrorCode"] == 0) $groupsites[] = $groupInfo["GroupInfo"][0];
+
+                            // 踢出用户在线组
+                            $redis = new Redis();
+                            $redisconfig = config("redis");
+                            $redis->connect($redisconfig["host"], $redisconfig["port"]);
+                            $redis->HDel("online_".$v["GroupId"],$info["To_Account"]);
+
+                            // 更新麦位前四
+                            $this->updatePosition($info["To_Account"],$v["GroupId"]);
+
+                            // 取消排麦
+                            $this->cancelLineup($info["To_Account"],$v["GroupId"]);
+
+                            // 房间状态变更
+                            $partyInfo = $redis->get("party_".$v["GroupId"]);
+                            if($partyInfo) {
+                                $partyInfo = json_decode($partyInfo,true);
+                                $memCount = count($redis->hGetAll("online_".$v["GroupId"]));
+                                if($memCount <= 0) {
+                                    $partyInfo["is_online"] = 0;
+                                    $redis->set("party_".$v["GroupId"],json_encode($partyInfo));
+                                    \app\common\model\Party::update(["is_online"=>0],["id"=>$v["GroupId"]]);
+                                }
+                            }
+
+                            $liveInfo = $redis->get("live_".$v["GroupId"]);
+                            if($liveInfo) {
+                                $liveInfo = json_decode($liveInfo,true);
+                                $memCount = count($redis->hGetAll("online_".$v["GroupId"]));
+                                if($memCount <= 0) {
+                                    $liveInfo["is_online"] = 0;
+                                    $redis->set("live_".$v["GroupId"],json_encode($liveInfo));
+                                    \app\common\model\Party::update(["is_online"=>0],["id"=>$v["GroupId"]]);
+                                }
+                            }
+                        }
+                        // 强制下麦
+                        print_r($this->downSite($info["To_Account"],$groupsites));
+
+                    }
+
+                }
+                break;
+//            case 'Group.CallbackAfterNewMemberJoin': // 新成员入群
+//                $redis = new Redis();
+//                $redisconfig = config("redis");
+//                $redis->connect($redisconfig["host"], $redisconfig["port"]);
+//                $members = $input["NewMemberList"];
+//                if($members) foreach($members as $k => $v) {
+//                    $redis->hSet("online_".$input["GroupId"],$v["Member_Account"],$v["Member_Account"]);
+//                    // 更新房间在线状态
+//                    $party_id = $input["GroupId"];
+//                    $partyInfo = $redis->get("party_".$party_id);
+//                    if($partyInfo) {
+//                        $partyInfo = json_decode($partyInfo,true);
+//                        if($partyInfo["is_online"] != 1) {
+//                            $partyInfo["is_online"] = 1;
+//                            $redis->set("party_".$party_id,json_encode($partyInfo));
+//                            \app\common\model\Party::update(["is_online"=>1],["id"=>$party_id]);
+//                        }
+//                    }
+//                }
+//                $res = [];
+//                $res["ActionStatus"] = "OK";
+//                $res["ErrorCode"] = 0;
+//                $res["ErrorInfo"] = "";
+//                echo json_encode($res);
+//                break;
+//            case 'Group.CallbackAfterMemberExit': // 成员离开
+//                $redis = new Redis();
+//                $redisconfig = config("redis");
+//                $redis->connect($redisconfig["host"], $redisconfig["port"]);
+//                $members = $input["ExitMemberList"];
+//                if($members) foreach($members as $k => $v) {
+//                    $redis->HDel("online_".$input["GroupId"],$v["Member_Account"]);
+//                }
+//                // 更新房间在线状态
+//                $party_id = $input["GroupId"];
+//                $partyInfo = $redis->get("party_".$party_id);
+//                if($partyInfo) {
+//                    $partyInfo = json_decode($partyInfo,true);
+//                    $memCount = count($redis->hGetAll("online_".$party_id));
+//                    if($memCount <= 0) {
+//                        $partyInfo["is_online"] = 0;
+//                        $redis->set("party_".$party_id,json_encode($partyInfo));
+//                        \app\common\model\Party::update(["is_online"=>0],["id"=>$party_id]);
+//                    }
+//                }
+//
+//                $liveInfo = $redis->get("live_".$party_id);
+//                if($liveInfo) {
+//                    $liveInfo = json_decode($liveInfo,true);
+//                    $memCount = count($redis->hGetAll("online_".$party_id));
+//                    if($memCount <= 0) {
+//                        $liveInfo["is_online"] = 0;
+//                        $redis->set("live_".$party_id,json_encode($liveInfo));
+//                        \app\common\model\Party::update(["is_online"=>0],["id"=>$party_id]);
+//                    }
+//                }
+//
+//                $res = [];
+//                $res["ActionStatus"] = "OK";
+//                $res["ErrorCode"] = 0;
+//                $res["ErrorInfo"] = "";
+//                echo json_encode($res);
+//                break;
+            case 'Group.CallbackBeforeSendMsg': // 群内发送消息之前
+
+                $config = config("wxMiniProgram");
+                $access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$config["appid"]."&secret=".$config["secret"];
+                $access_token_arr = $this->getJson($access_token_url);
+                $access_token = isset($access_token_arr["access_token"])?$access_token_arr["access_token"]:"";
+                if(!$access_token) {
+                    $res = [];
+                    $res["ActionStatus"] = "OK";
+                    $res["ErrorInfo"] = "";
+                    $res["ErrorCode"] = 0;
+                    echo json_encode($res);
+                    break;
+                }
+//                \app\common\model\Test::update(["content"=>json_encode($access_token)],["id"=>1]);
+                $infoMessage = $input["MsgBody"];
+                $badMessage = false;
+                if($infoMessage) foreach($infoMessage as $k => $v) {
+                    if(isset($v["MsgContent"]["Text"])) {
+                        $message = json_decode($v["MsgContent"]["Text"],true);
+                        $content = isset($message["content"])?$message["content"]:"";
+                        $message_check_url ='https://api.weixin.qq.com/wxa/msg_sec_check?access_token='.$access_token;
+                        $tencentObj = new tencentim($message_check_url);
+                        $content_json = '{"content": "'.$content.'"}';
+                        $body = $tencentObj->messageCheck($content_json);
+                        if($body["errcode"] == 87014) {
+//                        \app\common\model\Test::update(["content"=>$body["errcode"]],["id"=>1]);
+//                        $res = [];
+//                        $res["ActionStatus"] = "OK";
+//                        $res["ErrorInfo"] = "检测到敏感词汇!";
+//                        $res["ErrorCode"] = 1;
+//                        echo json_encode($res);exit;
+                            $message["content"] = "*******";
+                            $infoMessage[$k]["MsgContent"]["Text"] = json_encode($message);
+                            $badMessage = true;
+                        }
+                    }
+                }
+                if($badMessage) {
+                    $res = [];
+                    $res["ActionStatus"] = "OK";
+                    $res["ErrorInfo"] = "检测到敏感词汇!";
+                    $res["ErrorCode"] = 10102;
+                    $res["MsgBody"] = $infoMessage;
+                } else {
+                    $res = [];
+                    $res["ActionStatus"] = "OK";
+                    $res["ErrorInfo"] = "";
+                    $res["ErrorCode"] = 0;
+                }
+//                \app\common\model\Test::update(["content"=>json_encode($res)],["id"=>1]);
+                echo json_encode($res);exit;
+                break;
+            default:
+                break;
+        }
+        exit;
+    }
+
+    private function getJson($url){
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_URL, $url);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+        $output = curl_exec($ch);
+        curl_close($ch);
+        return json_decode($output, true);
+    }
+
+    /**
+     * 发送消息给某人
+     */
+    public function sendMessageToUser($from_user,$to_user,$message) {
+//        $from_user = 54;
+//        $to_user = 6;
+//        $message = "sdsd";
+        $random = rand(10000000,99999999);
+        $usersig = $this->usersig("administrator");
+        // 获取配置信息
+        $config = config("tencent_im");
+        $url = "https://console.tim.qq.com/v4/openim/sendmsg";
+        $url .= "?sdkappid=".$config["sdkappid"];
+        $url .= "&identifier=administrator";
+        $url .= "&usersig=".$usersig;
+        $url .= "&random=".$random;
+        $url .= "&contenttype=json";
+        $tencentObj = new tencentim($url);
+
+        $data = [];
+        $data["SyncOtherMachine"] = 1;
+        $data["From_Account"] = (string)$from_user;
+        $data["To_Account"] = (string)$to_user;
+        $data["MsgRandom"] = rand(1000000,9999999);
+        $data["MsgTimeStamp"] = time();
+
+        $data["MsgBody"][] = [
+            "MsgType" => "TIMTextElem",
+            "MsgContent" => [
+                "Text"=> $message
+            ],
+        ];
+        $tencentObj->toSend($data);
+    }
+
+    /**
+     * 发送消息给某人-接口调用
+     */
+    public function sendToUser() {
+        $from_user = $this->request->request("from_user");// 发送者
+        $to_user = $this->request->request("to_user");// 接收者
+        $message = $this->request->request("message");// 接收者
+        if(!$from_user || !$to_user || !$message) $this->error("参数缺失!");
+        $this->sendMessageToUser($from_user,$to_user,$message);
+    }
+
+    //请求的基础参数
+    private function baseTencentObj($url)
+    {
+        $random = rand(10000000,99999999);
+        $usersig = $this->usersig("administrator");
+        // 获取配置信息
+        $config = config("tencent_im");
+        $url = "$url";
+        $url .= "?sdkappid=".$config["sdkappid"];
+        $url .= "&identifier=administrator";
+        $url .= "&usersig=".$usersig;
+        $url .= "&random=".$random;
+        $url .= "&contenttype=json";
+        return new tencentim($url);
+    }
+
+    /** 设置管理员(im端的改变)
+     * @param $user_id
+     * @param $party_id
+     * @param int $status 1:yes 0:no
+     * @return array|false|mixed
+     */
+    public function setUpIMAdmin($user_id, $party_id, int $status=0) {
+        $url = "https://console.tim.qq.com/v4/group_open_http_svc/modify_group_member_info";
+        $tencentObj = $this->baseTencentObj($url);
+
+        $groupInfo = $this->getGroupInfo($party_id);
+        if($groupInfo["GroupInfo"][0]["ErrorCode"] > 0) return false;
+        // 更新的信息
+        $data = [];
+        $data["GroupId"] = $party_id;
+        if ($status==1){
+            $data['Role'] = 'Admin';
+        }else{
+            $data['Role'] = 'Member';
+        }
+        $data['Member_Account'] = $user_id;
+        return $tencentObj->toSend($data);
+    }
+
+
+    //获取群成员详细资料(目前没用到)
+    private function getGroupMemberDetails($party_id)
+    {
+        $url = "https://console.tim.qq.com/v4/group_open_http_svc/get_group_member_info";
+        $tencentObj = $this->baseTencentObj($url);
+        // 获取群组信息
+        $groupInfo = $this->getGroupInfo($party_id);
+        if($groupInfo["GroupInfo"][0]["ErrorCode"] > 0) return false;
+
+        // 更新的信息
+        $data = [];
+        $data["GroupId"] = $party_id;
+        return  $tencentObj->toSend($data);
+    }
+
+    //IM管理员维护,同步本地管理员
+    public function imAdminMaintenance()
+    {
+        $partyIds = \app\admin\model\party\Party::column('id');
+        foreach ($partyIds as $party_id){
+            $groupInfo = $this->getGroupInfo($party_id);
+
+            if($groupInfo["GroupInfo"][0]["ErrorCode"] > 0) continue;
+            $roomInfoData = $groupInfo["GroupInfo"][0]["AppDefinedData"];
+            $roomInfo = [];
+            if($roomInfoData) foreach($roomInfoData as $k => $v) {
+                if($v["Key"] == "roomInfo") $roomInfo = $v["Value"];
+            }
+            $roomInfo = json_decode($roomInfo,true);
+
+            // 更新管理员
+            if(isset($roomInfo["managers"]) && !empty($roomInfo['managers'])) {
+               foreach ($roomInfo['managers'] as $v){
+                    $this->setUpIMAdmin($v,$party_id,1);
+               }
+            }
+        }
+        $this->success("成功!");
+    }
+}

+ 332 - 0
extend/getusersig/getusersig.php

@@ -0,0 +1,332 @@
+<?php
+namespace getusersig;
+
+if ( version_compare( PHP_VERSION, '5.1.2' ) < 0 ) {
+    trigger_error( 'need php 5.1.2 or newer', E_USER_ERROR );
+}
+
+class Getusersig {
+
+    private $key = false;
+    private $sdkappid = 0;
+
+    /**
+     *【功能说明】用于签发 TRTC 和 IM 服务中必须要使用的 UserSig 鉴权票据
+     *
+     *【参数说明】
+     * @param string userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
+     * @param string expire - UserSig 票据的过期时间,单位是秒,比如 86400 代表生成的 UserSig 票据在一天后就无法再使用了。
+     * @return string 签名字符串
+     * @throws \Exception
+     */
+
+    public function genUserSig( $userid, $expire = 15552000 ) {
+        return $this->__genSig( $userid, $expire, '', false );
+    }
+
+    /**
+     *【功能说明】
+     * 用于签发 TRTC 进房参数中可选的 PrivateMapKey 权限票据。
+     * PrivateMapKey 需要跟 UserSig 一起使用,但 PrivateMapKey 比 UserSig 有更强的权限控制能力:
+     *  - UserSig 只能控制某个 UserID 有无使用 TRTC 服务的权限,只要 UserSig 正确,其对应的 UserID 可以进出任意房间。
+     *  - PrivateMapKey 则是将 UserID 的权限控制的更加严格,包括能不能进入某个房间,能不能在该房间里上行音视频等等。
+     * 如果要开启 PrivateMapKey 严格权限位校验,需要在【实时音视频控制台】=>【应用管理】=>【应用信息】中打开“启动权限密钥”开关。
+     *
+     *【参数说明】
+     * @param userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
+     * @param expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
+     * @param roomid - 房间号,用于指定该 userid 可以进入的房间号
+     * @param privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关:
+     *  - 第 1 位:0000 0001 = 1,创建房间的权限
+     *  - 第 2 位:0000 0010 = 2,加入房间的权限
+     *  - 第 3 位:0000 0100 = 4,发送语音的权限
+     *  - 第 4 位:0000 1000 = 8,接收语音的权限
+     *  - 第 5 位:0001 0000 = 16,发送视频的权限
+     *  - 第 6 位:0010 0000 = 32,接收视频的权限
+     *  - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限
+     *  - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限
+     *  - privilegeMap == 1111 1111 == 255 代表该 userid 在该 roomid 房间内的所有功能权限。
+     *  - privilegeMap == 0010 1010 == 42  代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
+     */
+
+    public function genPrivateMapKey( $userid, $expire, $roomid, $privilegeMap ) {
+        $userbuf = $this->__genUserBuf( $userid, $roomid, $expire, $privilegeMap, 0, '' );
+        return $this->__genSig( $userid, $expire, $userbuf, true );
+    }
+    /**
+     *【功能说明】
+     * 用于签发 TRTC 进房参数中可选的 PrivateMapKey 权限票据。
+     * PrivateMapKey 需要跟 UserSig 一起使用,但 PrivateMapKey 比 UserSig 有更强的权限控制能力:
+     *  - UserSig 只能控制某个 UserID 有无使用 TRTC 服务的权限,只要 UserSig 正确,其对应的 UserID 可以进出任意房间。
+     *  - PrivateMapKey 则是将 UserID 的权限控制的更加严格,包括能不能进入某个房间,能不能在该房间里上行音视频等等。
+     * 如果要开启 PrivateMapKey 严格权限位校验,需要在【实时音视频控制台】=>【应用管理】=>【应用信息】中打开“启动权限密钥”开关。
+     *
+     *【参数说明】
+     * @param userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
+     * @param expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
+     * @param roomstr - 房间号,用于指定该 userid 可以进入的房间号
+     * @param privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关:
+     *  - 第 1 位:0000 0001 = 1,创建房间的权限
+     *  - 第 2 位:0000 0010 = 2,加入房间的权限
+     *  - 第 3 位:0000 0100 = 4,发送语音的权限
+     *  - 第 4 位:0000 1000 = 8,接收语音的权限
+     *  - 第 5 位:0001 0000 = 16,发送视频的权限
+     *  - 第 6 位:0010 0000 = 32,接收视频的权限
+     *  - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限
+     *  - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限
+     *  - privilegeMap == 1111 1111 == 255 代表该 userid 在该 roomid 房间内的所有功能权限。
+     *  - privilegeMap == 0010 1010 == 42  代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
+     */
+
+    public function genPrivateMapKeyWithStringRoomID( $userid, $expire, $roomstr, $privilegeMap ) {
+        $userbuf = $this->__genUserBuf( $userid, 0, $expire, $privilegeMap, 0, $roomstr );
+        return $this->__genSig( $userid, $expire, $userbuf, true );
+    }
+
+    public function __construct( $sdkappid, $key ) {
+        $this->sdkappid = $sdkappid;
+        $this->key = $key;
+    }
+
+    /**
+     * 用于 url 的 base64 encode
+     * '+' => '*', '/' => '-', '=' => '_'
+     * @param string $string 需要编码的数据
+     * @return string 编码后的base64串,失败返回false
+     * @throws \Exception
+     */
+
+    private function base64_url_encode( $string ) {
+        static $replace = Array( '+' => '*', '/' => '-', '=' => '_' );
+        $base64 = base64_encode( $string );
+        if ( $base64 === false ) {
+            throw new \Exception( 'base64_encode error' );
+        }
+        return str_replace( array_keys( $replace ), array_values( $replace ), $base64 );
+    }
+
+    /**
+     * 用于 url 的 base64 decode
+     * '+' => '*', '/' => '-', '=' => '_'
+     * @param string $base64 需要解码的base64串
+     * @return string 解码后的数据,失败返回false
+     * @throws \Exception
+     */
+
+    private function base64_url_decode( $base64 ) {
+        static $replace = Array( '+' => '*', '/' => '-', '=' => '_' );
+        $string = str_replace( array_values( $replace ), array_keys( $replace ), $base64 );
+        $result = base64_decode( $string );
+        if ( $result == false ) {
+            throw new \Exception( 'base64_url_decode error' );
+        }
+        return $result;
+    }
+    /**
+     * TRTC业务进房权限加密串使用用户定义的userbuf
+     * @brief 生成 userbuf
+     * @param account 用户名
+     * @param dwSdkappid sdkappid
+     * @param dwAuthID  数字房间号
+     * @param dwExpTime 过期时间:该权限加密串的过期时间. 过期时间 = now+dwExpTime
+     * @param dwPrivilegeMap 用户权限,255表示所有权限
+     * @param dwAccountType 用户类型, 默认为0
+     * @param roomStr 字符串房间号
+     * @return userbuf string  返回的userbuf
+     */
+
+    private function __genUserBuf( $account, $dwAuthID, $dwExpTime, $dwPrivilegeMap, $dwAccountType,$roomStr ) {
+
+        //cVer  unsigned char/1 版本号,填0
+        if($roomStr == '')
+            $userbuf = pack( 'C1', '0' );
+        else
+            $userbuf = pack( 'C1', '1' );
+
+        $userbuf .= pack( 'n', strlen( $account ) );
+        //wAccountLen   unsigned short /2   第三方自己的帐号长度
+        $userbuf .= pack( 'a'.strlen( $account ), $account );
+        //buffAccount   wAccountLen 第三方自己的帐号字符
+        $userbuf .= pack( 'N', $this->sdkappid );
+        //dwSdkAppid    unsigned int/4  sdkappid
+        $userbuf .= pack( 'N', $dwAuthID );
+        //dwAuthId  unsigned int/4  群组号码/音视频房间号
+        $expire = $dwExpTime + time();
+        $userbuf .= pack( 'N', $expire );
+        //dwExpTime unsigned int/4  过期时间 (当前时间 + 有效期(单位:秒,建议300秒))
+        $userbuf .= pack( 'N', $dwPrivilegeMap );
+        //dwPrivilegeMap unsigned int/4  权限位
+        $userbuf .= pack( 'N', $dwAccountType );
+        //dwAccountType  unsigned int/4
+        if($roomStr != '')
+        {
+            $userbuf .= pack( 'n', strlen( $roomStr ) );
+            //roomStrLen   unsigned short /2   字符串房间号长度
+            $userbuf .= pack( 'a'.strlen( $roomStr ), $roomStr );
+            //roomStr   roomStrLen 字符串房间号
+        }
+        return $userbuf;
+    }
+    /**
+     * 使用 hmac sha256 生成 sig 字段内容,经过 base64 编码
+     * @param $identifier 用户名,utf-8 编码
+     * @param $curr_time 当前生成 sig 的 unix 时间戳
+     * @param $expire 有效期,单位秒
+     * @param $base64_userbuf base64 编码后的 userbuf
+     * @param $userbuf_enabled 是否开启 userbuf
+     * @return string base64 后的 sig
+     */
+
+    private function hmacsha256( $identifier, $curr_time, $expire, $base64_userbuf, $userbuf_enabled ) {
+        $content_to_be_signed = 'TLS.identifier:' . $identifier . "\n"
+            . 'TLS.sdkappid:' . $this->sdkappid . "\n"
+            . 'TLS.time:' . $curr_time . "\n"
+            . 'TLS.expire:' . $expire . "\n";
+        if ( true == $userbuf_enabled ) {
+            $content_to_be_signed .= 'TLS.userbuf:' . $base64_userbuf . "\n";
+        }
+        return base64_encode( hash_hmac( 'sha256', $content_to_be_signed, $this->key, true ) );
+    }
+
+    /**
+     * 生成签名。
+     *
+     * @param $identifier 用户账号
+     * @param int $expire 过期时间,单位秒,默认 180 天
+     * @param $userbuf base64 编码后的 userbuf
+     * @param $userbuf_enabled 是否开启 userbuf
+     * @return string 签名字符串
+     * @throws \Exception
+     */
+
+    private function __genSig( $identifier, $expire, $userbuf, $userbuf_enabled ) {
+        $curr_time = time();
+        $sig_array = Array(
+            'TLS.ver' => '2.0',
+            'TLS.identifier' => strval( $identifier ),
+            'TLS.sdkappid' => intval( $this->sdkappid ),
+            'TLS.expire' => intval( $expire ),
+            'TLS.time' => intval( $curr_time )
+        );
+
+        $base64_userbuf = '';
+        if ( true == $userbuf_enabled ) {
+            $base64_userbuf = base64_encode( $userbuf );
+            $sig_array['TLS.userbuf'] = strval( $base64_userbuf );
+        }
+
+        $sig_array['TLS.sig'] = $this->hmacsha256( $identifier, $curr_time, $expire, $base64_userbuf, $userbuf_enabled );
+        if ( $sig_array['TLS.sig'] === false ) {
+            throw new \Exception( 'base64_encode error' );
+        }
+        $json_str_sig = json_encode( $sig_array );
+        if ( $json_str_sig === false ) {
+            throw new \Exception( 'json_encode error' );
+        }
+        $compressed = gzcompress( $json_str_sig );
+        if ( $compressed === false ) {
+            throw new \Exception( 'gzcompress error' );
+        }
+        return $this->base64_url_encode( $compressed );
+    }
+
+    /**
+     * 验证签名。
+     *
+     * @param string $sig 签名内容
+     * @param string $identifier 需要验证用户名,utf-8 编码
+     * @param int $init_time 返回的生成时间,unix 时间戳
+     * @param int $expire_time 返回的有效期,单位秒
+     * @param string $userbuf 返回的用户数据
+     * @param string $error_msg 失败时的错误信息
+     * @return boolean 验证是否成功
+     * @throws \Exception
+     */
+
+    private function __verifySig( $sig, $identifier, &$init_time, &$expire_time, &$userbuf, &$error_msg ) {
+        try {
+            $error_msg = '';
+            $compressed_sig = $this->base64_url_decode( $sig );
+            $pre_level = error_reporting( E_ERROR );
+            $uncompressed_sig = gzuncompress( $compressed_sig );
+            error_reporting( $pre_level );
+            if ( $uncompressed_sig === false ) {
+                throw new \Exception( 'gzuncompress error' );
+            }
+            $sig_doc = json_decode( $uncompressed_sig );
+            if ( $sig_doc == false ) {
+                throw new \Exception( 'json_decode error' );
+            }
+            $sig_doc = ( array )$sig_doc;
+            if ( $sig_doc['TLS.identifier'] !== $identifier ) {
+                throw new \Exception( "identifier dosen't match" );
+            }
+            if ( $sig_doc['TLS.sdkappid'] != $this->sdkappid ) {
+                throw new \Exception( "sdkappid dosen't match" );
+            }
+            $sig = $sig_doc['TLS.sig'];
+            if ( $sig == false ) {
+                throw new \Exception( 'sig field is missing' );
+            }
+
+            $init_time = $sig_doc['TLS.time'];
+            $expire_time = $sig_doc['TLS.expire'];
+
+            $curr_time = time();
+            if ( $curr_time > $init_time+$expire_time ) {
+                throw new \Exception( 'sig expired' );
+            }
+
+            $userbuf_enabled = false;
+            $base64_userbuf = '';
+            if ( isset( $sig_doc['TLS.userbuf'] ) ) {
+                $base64_userbuf = $sig_doc['TLS.userbuf'];
+                $userbuf = base64_decode( $base64_userbuf );
+                $userbuf_enabled = true;
+            }
+            $sigCalculated = $this->hmacsha256( $identifier, $init_time, $expire_time, $base64_userbuf, $userbuf_enabled );
+
+            if ( $sig != $sigCalculated ) {
+                throw new \Exception( 'verify failed' );
+            }
+
+            return true;
+        } catch ( \Exception $ex ) {
+            $error_msg = $ex->getMessage();
+            return false;
+        }
+    }
+
+    /**
+     * 带 userbuf 验证签名。
+     *
+     * @param string $sig 签名内容
+     * @param string $identifier 需要验证用户名,utf-8 编码
+     * @param int $init_time 返回的生成时间,unix 时间戳
+     * @param int $expire_time 返回的有效期,单位秒
+     * @param string $error_msg 失败时的错误信息
+     * @return boolean 验证是否成功
+     * @throws \Exception
+     */
+
+    public function verifySig( $sig, $identifier, &$init_time, &$expire_time, &$error_msg ) {
+        $userbuf = '';
+        return $this->__verifySig( $sig, $identifier, $init_time, $expire_time, $userbuf, $error_msg );
+    }
+
+    /**
+     * 验证签名
+     * @param string $sig 签名内容
+     * @param string $identifier 需要验证用户名,utf-8 编码
+     * @param int $init_time 返回的生成时间,unix 时间戳
+     * @param int $expire_time 返回的有效期,单位秒
+     * @param string $userbuf 返回的用户数据
+     * @param string $error_msg 失败时的错误信息
+     * @return boolean 验证是否成功
+     * @throws \Exception
+     */
+
+    public function verifySigWithUserBuf( $sig, $identifier, &$init_time, &$expire_time, &$userbuf, &$error_msg ) {
+        return $this->__verifySig( $sig, $identifier, $init_time, $expire_time, $userbuf, $error_msg );
+    }
+}

+ 73 - 0
extend/tencentim/tencentim.php

@@ -0,0 +1,73 @@
+<?php
+namespace tencentim;
+
+class Tencentim
+{
+    private $api_url;
+
+    /**
+     * 构造函数
+     */
+    public function __construct($api_url)
+    {
+        $this->api_url = $api_url;
+    }
+
+
+    /**
+     * $params 请求参数
+     */
+    function toSend($receiptdata) {
+        // 构造请求参数
+        $params = $receiptdata;
+        // random int
+        $params = json_encode($params);
+
+        $result = $this->http_post_json($this->api_url,$params);
+//        \app\common\model\Test::update(["content"=>json_encode($result)],["id"=>1]);
+        if ($result === FALSE) {
+            return array("code" => 500, "msg" => "file_get_contents failed.");
+        } else {
+            return json_decode($result[1], true);
+        }
+    }
+
+
+    /**
+     * @param $url
+     * @param $jsonStr
+     * @return array
+     */
+    function http_post_json($url, $jsonStr)
+    {
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_POST, 1);
+        curl_setopt($ch, CURLOPT_URL, $url);
+        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
+                'Content-Type: application/json; charset=utf-8',
+                'Content-Length: ' . strlen($jsonStr)
+            )
+        );
+        $response = curl_exec($ch);
+        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+        curl_close($ch);
+        return array($httpCode, $response);
+    }
+
+    /**
+     * $params 请求参数
+     */
+    function messageCheck($params) {
+
+        $result = $this->http_post_json($this->api_url,$params);
+        if ($result === FALSE) {
+            return array("code" => 500, "msg" => "file_get_contents failed.");
+        } else {
+            return json_decode($result[1], true);
+        }
+    }
+
+}
+