12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\utils\RedisUtil;
- use think\Request;
- use getusersig\getusersig;
- use tencentim\tencentim;
- use think\Db;
- /**
- * 腾讯im接口
- */
- class Tenim extends Api
- {
- protected $noNeedLogin = ['trtc_callback','callback','test','createIMGroup','updateRoomInfo','setImManage','sendMessageToUser','outMemberFromRoom','selectstatus','loginoutim', 'tencentcall'];
- 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);
- }
- public function loginoutim($uid){
- /*
- * https://console.tim.qq.com/v4/im_open_login_svc/kick?sdkappid=88888888&identifier=admin&usersig=xxx&random=99999999&contenttype=json
- */
- $random = rand(10000000, 99999999);
- $usersig = $this->usersig("administrator");
- // 获取配置信息
- $config = config("tencent_im");
- $url = "https://console.tim.qq.com/v4/im_open_login_svc/kick";
- $url .= "?sdkappid=" . $config["sdkappid"];
- $url .= "&identifier=administrator";
- $url .= "&usersig=" . $usersig;
- $url .= "&random=" . $random;
- $url .= "&contenttype=json";
- $data = [
- 'Identifier' => "".$uid."",
- ];
- $rs = curl_post($url,json_encode($data));
- //dump($rs);
- // exit;
- }
- public function selectstatus(){
- /*
- * https://console.tim.qq.com/v4/openim/query_online_status?sdkappid=88888888&identifier=admin&usersig=xxx&random=99999999&contenttype=json
- */
- $random = rand(10000000, 99999999);
- $usersig = $this->usersig("administrator");
- // 获取配置信息
- $config = config("tencent_im");
- $url = "https://console.tim.qq.com/v4/openim/query_online_status";
- $url .= "?sdkappid=" . $config["sdkappid"];
- $url .= "&identifier=administrator";
- $url .= "&usersig=" . $usersig;
- $url .= "&random=" . $random;
- $url .= "&contenttype=json";
- $data = [
- 'To_Account' => ["34"],
- ];
- $rs = curl_post($url,json_encode($data));
- //$rs = curl_post($url,'{"IsNeedDetail":1,"To_Account":["32"]}');
- dump($rs);
- exit;
- }
- /**
- * 创建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)
- {
- 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];
- // 踢出用户在线组
- RedisUtil::getInstance('online',$v["GroupId"])->HDel($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"]:"";
- $redisData = RedisUtil::getInstance('party',$party_id)->get();
- 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]);
- RedisUtil::getInstance('party',$party_id)->set(json_encode($partyInfo));
- }
- /**
- * 取消排麦
- */
- public function cancelLineUp($user_id,$party_id) {
- if (!$party_id || !$user_id) {
- return false;
- }
- $data=unserialize(RedisUtil::getInstance('party_lineup')->hGet($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;
- }
- }
- RedisUtil::getInstance('party_lineup')->hSet($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);
- }
- }
- //trtc_callback,直播callback
- //https://cloud.tencent.com/document/product/647/51586#eventId
- /*
- 2021-12-24 15:34:22 start2:{
- "EventGroupId": 1,
- "EventType": 103,
- "CallbackTs": 1640331262241,
- "EventInfo": {
- "RoomId": 1604,
- "EventTs": 1640331262,
- "EventMsTs": 1640331262238,
- "UserId": "26",
- "Role": 20,
- "TerminalType": 2,
- "UserType": 3,
- "Reason": 1
- }
- }
- 2021-12-24 15:34:22 start2:{
- "EventGroupId": 1,
- "EventType": 101,
- "CallbackTs": 1640331262247,
- "EventInfo": {
- "RoomId": 1604,
- "EventTs": 1640331262,
- "EventMsTs": 1640331262238,
- "UserId": "26"
- }
- }
- 2021-12-24 15:34:55 start2:{
- "EventGroupId": 1,
- "EventType": 102,
- "CallbackTs": 1640331295244,
- "EventInfo": {
- "RoomId": 1604,
- "EventTs": 1640331295,
- "EventMsTs": 1640331295239
- }
- }
- 2021-12-24 15:34:55 start2:{
- "EventGroupId": 1,
- "EventType": 104,
- "CallbackTs": 1640331295244,
- "EventInfo": {
- "RoomId": 1604,
- "EventTs": 1640331295,
- "EventMsTs": 1640331295239,
- "UserId": "26",
- "Role": 20,
- "Reason": 1
- }
- }
- */
- public function trtc_callback(){
- $input = file_get_contents("php://input");
- filePut('start:'.$input,'callback.txt');
- /*$input = '{
- "EventGroupId": 1,
- "EventType": 104,
- "CallbackTs": 1640331295244,
- "EventInfo": {
- "RoomId": 1604,
- "EventTs": 1640331295,
- "EventMsTs": 1640331295239,
- "UserId": "26",
- "Role": 20,
- "Reason": 1
- }
- }';*/
- $input = json_decode($input,true);
- $roomId = $input['EventInfo']['RoomId'];
- //房间事件组
- if($input['EventGroupId'] == 1){
- if($input['EventType'] == 101){
- //创建房间,第一个进来的
- }elseif($input['EventType'] == 102){
- //解散房间,最后一个走的
- //没有UserId
- }elseif($input['EventType'] == 103){
- //进入房间,主播role=20 观众role=21
- $userId = $input['EventInfo']['UserId'];
- RedisUtil::getInstance('online',$roomId)->hSet($userId, $userId);
- // 记录在线用户在房间情况
- RedisUtil::getInstance('livingUser')->hSet($userId,$roomId);
- // 更新房间在线状态
- $partyInfo = $redis->get("party_" . $roomId);
- if ($partyInfo) {
- $partyInfo = json_decode($partyInfo, true);
- if ($partyInfo["is_online"] != 1) {
- $partyInfo["is_online"] = 1;
- $redis->set("party_" . $roomId, json_encode($partyInfo));
- \app\common\model\Party::update(["is_online" => 1], ["id" => $roomId]);
- }
- }
- // 更新房间在线状态
- $liveInfo = $redis->get("livebc_" . $roomId);
- if ($liveInfo) {
- $liveInfo = json_decode($liveInfo, true);
- if ($input['EventInfo']['Role'] == 20) {
- $liveInfo["is_online"] = 1;
- $redis->set("livebc_" . $roomId, json_encode($liveInfo));
- \app\common\model\Party::update(["is_online" => 1], ["id" => $roomId]);
- $rs_user = Db::name('user')->where('id',$userId)->update(['is_livebc'=>1]);
- }
- }
- }elseif($input['EventType'] == 104){
- //退出房间,主播role=20 观众role=21
- $userId = $input['EventInfo']['UserId'];
- RedisUtil::getInstance('online',$roomId)->HDel($userId);
- // 扣除在线用户在房间情况
- RedisUtil::getInstance('livingUser')->hDel($userId);
- // 更新房间在线状态
- $partyInfo = RedisUtil::getInstance('party',$roomId)->get();
- if ($partyInfo) {
- $partyInfo = json_decode($partyInfo, true);
- $memCount = count(RedisUtil::getInstance('online',$roomId)->hGetAll());
- if ($memCount <= 0) {
- $partyInfo["is_online"] = 0;
- RedisUtil::getInstance('party',$roomId)->set(json_encode($partyInfo));
- \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]);
- }
- }
- //
- $liveInfo = RedisUtil::getInstance('livebc',$roomId)->get();
- if ($liveInfo) {
- $liveInfo = json_decode($liveInfo, true);
- if($input['EventInfo']['Role'] == 20){
- $liveInfo["is_online"] = 0;
- RedisUtil::getInstance('livebc',$roomId)->set(json_encode($liveInfo));
- \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]);
- $rs_user = Db::name('user')->where('id',$userId)->update(['is_livebc'=>0]);
- }
- }
- }
- }elseif($input['EventGroupId'] == 2){
- }
- //结束
- echo json_encode(['code' => 0]);exit;
- }
- /**
- * 回调
- */
- public function callback() {
- $input = file_get_contents("php://input"); // 主题信息
- if(!$input) {
- echo '无数据';exit;
- }
- $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()],["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") { // 用户退出 不需要管什么原因
- // 更新用户在线状态为离线
- $updateData['is_online'] = 0;
- $user = Db::name('user')->where('id',$info["To_Account"])->find();
- if (!empty($user)) {
- // 客户觉得实时判断导致在线用户数量过少,此处,不再根据IM处理用户在线状态,已在API父类中做接口访问频率处理用户在线状态
- // if ($user['gender'] == 1) {
- // $updateData['is_active'] = 0;
- // }
- }
- \app\common\model\User::update($updateData,["id"=>$info["To_Account"]]);
- //\app\common\model\User::update(["is_live"=>0],["id"=>$info["To_Account"]]);
- // 获取用户在派对直播间情况信息
- $livingUserPartyId = RedisUtil::getInstance('livingUser')->hGet($info["To_Account"]);
- if ($livingUserPartyId) {
- // 扣除在线用户在房间情况
- RedisUtil::getInstance('livingUser')->hDel( $info["To_Account"]);
- RedisUtil::getInstance('online',$livingUserPartyId)->HDel($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];
- // 踢出用户在线组
- RedisUtil::getInstance('online',$v["GroupId"])->HDel($info["To_Account"]);
- // 更新麦位前四
- $this->updatePosition($info["To_Account"],$v["GroupId"]);
- // 取消排麦
- $this->cancelLineup($info["To_Account"],$v["GroupId"]);
- // 房间状态变更
- $partyInfo = RedisUtil::getInstance('party',$v["GroupId"])->get();
- if($partyInfo) {
- $partyInfo = json_decode($partyInfo,true);
- $memCount = count(RedisUtil::getInstance('online',$v["GroupId"])->hGetAll());
- if($memCount <= 0) {
- $partyInfo["is_online"] = 0;
- RedisUtil::getInstance('party',$v["GroupId"])->set(json_encode($partyInfo));
- \app\common\model\Party::update(["is_online"=>0],["id"=>$v["GroupId"]]);
- }
- }
- $liveInfo = RedisUtil::getInstance('live',$v["GroupId"])->get();
- if($liveInfo) {
- $liveInfo = json_decode($liveInfo,true);
- $memCount = count(RedisUtil::getInstance('online',$v["GroupId"])->hGetAll());
- if($memCount <= 0) {
- $liveInfo["is_online"] = 0;
- RedisUtil::getInstance('live',$v["GroupId"])->set(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': // 新成员入群
- // $members = $input["NewMemberList"];
- // if($members) foreach($members as $k => $v) {
- // RedisUtil::getInstance('online',$input["GroupId"])->hSet($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': // 成员离开
- // $members = $input["ExitMemberList"];
- // if($members) foreach($members as $k => $v) {
- // RedisUtil::getInstance('online',$input["GroupId"])->HDel($v["Member_Account"]);
- // }
- // // 更新房间在线状态
- // $party_id = $input["GroupId"];
- // $partyInfo = RedisUtil::getInstance('party',$party_id)->get();
- // if($partyInfo) {
- // $partyInfo = json_decode($partyInfo,true);
- // $memCount = count(RedisUtil::getInstance('online',$party_id)->hGetAll());
- // if($memCount <= 0) {
- // $partyInfo["is_online"] = 0;
- // RedisUtil::getInstance('party',$party_id)->set(json_encode($partyInfo));
- // \app\common\model\Party::update(["is_online"=>0],["id"=>$party_id]);
- // }
- // }
- //
- // $liveInfo = RedisUtil::getInstance('live',$party_id)->get();
- // if($liveInfo) {
- // $liveInfo = json_decode($liveInfo,true);
- // $memCount = count(RedisUtil::getInstance('online',$party_id)->hGetAll());
- // if($memCount <= 0) {
- // $liveInfo["is_online"] = 0;
- // RedisUtil::getInstance('live',$party_id)->set(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);
- }
- /**
- * 发送消息给某人
- * type:0普通消息 1自定义消息
- */
- public function sendMessageToUser($from_user,$to_user,$message, $type = 0) {
- // $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();
- if ($type == 1) { //自定义消息
- $body_message = '{"businessID":"custom_tips_message","receiverShow":true,"senderId":"'.$from_user.'","text":"'.$message.'","tipType":257,"version":0}';
- $data["MsgBody"][] = [
- "MsgType" => "TIMCustomElem",
- "MsgContent" => [
- "Data" => $body_message,
- "Desc" => $body_message,
- "Ext" => 'custom_tips_message',
- "Sound" => ''
- ],
- ];
- } else {
- $data["MsgBody"][] = [
- "MsgType" => "TIMTextElem",
- "MsgContent" => [
- "Text" => $message
- ],
- ];
- }
- $rs = $tencentObj->toSend($data);//p($rs);
- }
- /**
- * 发送消息给某人-接口调用
- */
- 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, 1);
- }
- //请求的基础参数
- 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("成功!");
- }
- //拉黑
- public function black_one($from_user = '',$to_user = '') {
- $from_user = (string)$from_user;
- $to_user = (string)$to_user;
- $random = rand(10000000,99999999);
- $usersig = $this->usersig("administrator");
- // 获取配置信息
- $config = config("tencent_im");
- $url = "https://console.tim.qq.com/v4/sns/black_list_add";
- $url .= "?sdkappid=".$config["sdkappid"];
- $url .= "&identifier=administrator";
- $url .= "&usersig=".$usersig;
- $url .= "&random=".$random;
- $url .= "&contenttype=json";
- $tencentObj = new tencentim($url);
- $data = [];
- $data["From_Account"] = $from_user;
- $data["To_Account"] = [$to_user];
- $rs = $tencentObj->toSend($data);
- return $rs;
- }
- //取消拉黑
- public function un_black_one($from_user = '',$to_user = '') {
- $from_user = (string)$from_user;
- $to_user = (string)$to_user;
- $random = rand(10000000,99999999);
- $usersig = $this->usersig("administrator");
- // 获取配置信息
- $config = config("tencent_im");
- $url = "https://console.tim.qq.com/v4/sns/black_list_delete";
- $url .= "?sdkappid=".$config["sdkappid"];
- $url .= "&identifier=administrator";
- $url .= "&usersig=".$usersig;
- $url .= "&random=".$random;
- $url .= "&contenttype=json";
- $tencentObj = new tencentim($url);
- $data = [];
- $data["From_Account"] = $from_user;
- $data["To_Account"] = [$to_user];
- $rs = $tencentObj->toSend($data);
- return $rs;
- }
- //撤回单聊消息
- public function withdraw_message($from_user = '', $to_user = '',$msgkey = '') {
- // $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/admin_msgwithdraw";
- $url .= "?sdkappid=".$config["sdkappid"];
- $url .= "&identifier=administrator";
- $url .= "&usersig=".$usersig;
- $url .= "&random=".$random;
- $url .= "&contenttype=json";
- $tencentObj = new tencentim($url);
- $data = [];
- $data["From_Account"] = (string)$from_user;
- $data["To_Account"] = (string)$to_user;
- $data["MsgKey"] = (string)$msgkey;
- $rs = $tencentObj->toSend($data);//p($rs);
- return $rs;
- }
- }
|