Tenim.php 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Request;
  5. use getusersig\getusersig;
  6. use tencentim\tencentim;
  7. use Redis;
  8. use think\Db;
  9. /**
  10. * 腾讯im接口
  11. */
  12. class Tenim extends Api
  13. {
  14. protected $noNeedLogin = ['trtc_callback','callback','test','createIMGroup','updateRoomInfo','setImManage','sendMessageToUser','outMemberFromRoom','selectstatus','loginoutim'];
  15. protected $noNeedRight = ['*'];
  16. public function test() {
  17. $groupinfo = $this->getGroupInfo("112");
  18. print_r($groupinfo);
  19. exit;
  20. // 获取所有派对信息
  21. $partyList = \app\common\model\Party::field("id,user_id,party_name")->where(["status"=>1])->order("id","asc")->select();
  22. foreach($partyList as $k => $v) {
  23. if($k>=140 && $k<180) {
  24. $groupinfo = $this->getGroupInfo((string)$v["id"]);
  25. print_r($groupinfo);
  26. // $groupinfo = json_encode($groupinfo);
  27. // Db::table("hx_aaaa")->insert(["party_id"=>$v["id"],"user_id"=>$v["user_id"],"party_name"=>$v["party_name"],"json"=>$groupinfo]);
  28. }
  29. }
  30. exit;
  31. // $this->success("",$partyList);
  32. }
  33. public function loginoutim($uid){
  34. /*
  35. * https://console.tim.qq.com/v4/im_open_login_svc/kick?sdkappid=88888888&identifier=admin&usersig=xxx&random=99999999&contenttype=json
  36. */
  37. $random = rand(10000000, 99999999);
  38. $usersig = $this->usersig("administrator");
  39. // 获取配置信息
  40. $config = config("tencent_im");
  41. $url = "https://console.tim.qq.com/v4/im_open_login_svc/kick";
  42. $url .= "?sdkappid=" . $config["sdkappid"];
  43. $url .= "&identifier=administrator";
  44. $url .= "&usersig=" . $usersig;
  45. $url .= "&random=" . $random;
  46. $url .= "&contenttype=json";
  47. $data = [
  48. 'Identifier' => "".$uid."",
  49. ];
  50. $rs = curl_post($url,json_encode($data));
  51. //dump($rs);
  52. // exit;
  53. }
  54. public function selectstatus(){
  55. /*
  56. * https://console.tim.qq.com/v4/openim/query_online_status?sdkappid=88888888&identifier=admin&usersig=xxx&random=99999999&contenttype=json
  57. */
  58. $random = rand(10000000, 99999999);
  59. $usersig = $this->usersig("administrator");
  60. // 获取配置信息
  61. $config = config("tencent_im");
  62. $url = "https://console.tim.qq.com/v4/openim/query_online_status";
  63. $url .= "?sdkappid=" . $config["sdkappid"];
  64. $url .= "&identifier=administrator";
  65. $url .= "&usersig=" . $usersig;
  66. $url .= "&random=" . $random;
  67. $url .= "&contenttype=json";
  68. $data = [
  69. 'To_Account' => ["34"],
  70. ];
  71. $rs = curl_post($url,json_encode($data));
  72. //$rs = curl_post($url,'{"IsNeedDetail":1,"To_Account":["32"]}');
  73. dump($rs);
  74. exit;
  75. }
  76. /**
  77. * 创建im群组
  78. */
  79. public function createIMGroup() {
  80. $random = rand(10000000,99999999);
  81. $usersig = $this->usersig("administrator");
  82. // 获取配置信息
  83. $config = config("tencent_im");
  84. $url = "https://console.tim.qq.com/v4/group_open_http_svc/create_group";
  85. $url .= "?sdkappid=".$config["sdkappid"];
  86. $url .= "&identifier=administrator";
  87. $url .= "&usersig=".$usersig;
  88. $url .= "&random==".$random;
  89. $url .= "&contenttype=json";
  90. $tencentObj = new tencentim($url);
  91. // 获取群im信息
  92. $imInfo = Db::table("hx_aaaa")->where(["party_id"=>112])->select();
  93. if($imInfo) foreach($imInfo as $k => $v) {
  94. // if($k>=160 && $k<200) {
  95. $grouparr = json_decode($v["json"], true);
  96. $grouparr = $grouparr["GroupInfo"][0];
  97. $AppDefinedData = $grouparr["AppDefinedData"];
  98. $data = [];
  99. $data["Owner_Account"] = (string)$v["user_id"];
  100. $data["Type"] = "AVChatRoom";
  101. $data["GroupId"] = (string)$v["party_id"];
  102. $data["Name"] = $this->substr((string)$v["party_name"],0,7);
  103. $AppDefinedDataNew = [
  104. [
  105. "Key" => "roomInfo",
  106. "Value" => "sssssss"
  107. ],
  108. [
  109. "Key" => "seat0",
  110. "Value" => '{"mute":false,"status":0,"user":""}'
  111. ],
  112. [
  113. "Key" => "seat1",
  114. "Value" => $AppDefinedData[2]["Value"]
  115. ],
  116. [
  117. "Key" => "seat2",
  118. "Value" => $AppDefinedData[3]["Value"]
  119. ],
  120. [
  121. "Key" => "seat3",
  122. "Value" => $AppDefinedData[4]["Value"]
  123. ],
  124. [
  125. "Key" => "seat4",
  126. "Value" => $AppDefinedData[5]["Value"]
  127. ],
  128. [
  129. "Key" => "seat5",
  130. "Value" => $AppDefinedData[6]["Value"]
  131. ],
  132. [
  133. "Key" => "seat6",
  134. "Value" => $AppDefinedData[7]["Value"]
  135. ],
  136. [
  137. "Key" => "seat7",
  138. "Value" => $AppDefinedData[8]["Value"]
  139. ],
  140. [
  141. "Key" => "seat8",
  142. "Value" => $AppDefinedData[9]["Value"]
  143. ]
  144. ];
  145. $data["AppDefinedData"] = json_encode($AppDefinedDataNew);
  146. $groupInfo = $tencentObj->toSend($data);
  147. if($groupInfo["ActionStatus"] != 'OK') {
  148. echo $v["party_id"];
  149. break;
  150. }
  151. // }
  152. }
  153. print_r($groupInfo);exit;
  154. }
  155. public function substr(string $string, int $start, int $length = null)
  156. {
  157. return mb_substr($string, $start, $length, 'UTF-8');
  158. }
  159. /**
  160. * 更新房间信息
  161. */
  162. public function updateRoomInfo() {
  163. $random = rand(10000000, 99999999);
  164. $usersig = $this->usersig("administrator");
  165. // 获取配置信息
  166. $config = config("tencent_im");
  167. $url = "https://console.tim.qq.com/v4/group_open_http_svc/modify_group_base_info";
  168. $url .= "?sdkappid=" . $config["sdkappid"];
  169. $url .= "&identifier=administrator";
  170. $url .= "&usersig=" . $usersig;
  171. $url .= "&random=" . $random;
  172. $url .= "&contenttype=json";
  173. $tencentObj = new tencentim($url);
  174. // 获取群im信息
  175. $imInfo = Db::table("hx_aaaa")->select();
  176. if($imInfo) foreach($imInfo as $k => $v) {
  177. if($k>=120 && $k<180) {
  178. $grouparr = json_decode($v["json"], true);
  179. $groupsites = $grouparr["GroupInfo"];
  180. // 循环房间
  181. if ($groupsites) foreach ($groupsites as $k => $v) {
  182. // 循环座位
  183. $data = [];
  184. foreach ($v["AppDefinedData"] as $m => $n) {
  185. // 解析字段信息
  186. $siteInfo = json_decode($n["Value"], true);
  187. $data["AppDefinedData"][] = [
  188. "Key" => $n["Key"],
  189. "Value" => $n["Value"],
  190. ];
  191. }
  192. if (!empty($data)) {
  193. $data["GroupId"] = $v["GroupId"];
  194. $res = $tencentObj->toSend($data);
  195. } else {
  196. echo "error!";
  197. break;
  198. }
  199. }
  200. }
  201. }
  202. print_r($res);
  203. exit;
  204. }
  205. /**
  206. * 获取群组信息
  207. */
  208. public function getGroupInfo($party_id) {
  209. $random = rand(10000000,99999999);
  210. $usersig = $this->usersig("administrator");
  211. // 获取配置信息
  212. $config = config("tencent_im");
  213. $url = "https://console.tim.qq.com/v4/group_open_http_svc/get_group_info";
  214. $url .= "?sdkappid=".$config["sdkappid"];
  215. $url .= "&identifier=administrator";
  216. $url .= "&usersig=".$usersig;
  217. $url .= "&random==".$random;
  218. $url .= "&contenttype=json";
  219. $tencentObj = new tencentim($url);
  220. $data = [];
  221. $data["GroupIdList"] = [$party_id];
  222. $data["ResponseFilter"] = ["AppDefinedDataFilter_Group"=>[]];
  223. $groupInfo = $tencentObj->toSend($data);
  224. return $groupInfo;
  225. }
  226. /**
  227. * 获取用户所加入的群组
  228. */
  229. public function getJoindGroupList($user_id) {
  230. $random = rand(10000000,99999999);
  231. $usersig = $this->usersig("administrator");
  232. // 获取配置信息
  233. $config = config("tencent_im");
  234. $url = "https://console.tim.qq.com/v4/group_open_http_svc/get_joined_group_list";
  235. $url .= "?sdkappid=".$config["sdkappid"];
  236. $url .= "&identifier=administrator";
  237. $url .= "&usersig=".$usersig;
  238. $url .= "&random=".$random;
  239. $url .= "&contenttype=json";
  240. $tencentObj = new tencentim($url);
  241. $data = [];
  242. $data["Member_Account"] = $user_id;
  243. $data["WithHugeGroups"] = 1;
  244. $data["GroupType"] = 'AVChatRoom';
  245. $data["ResponseFilter"] = ["GroupBaseInfoFilter" => ['GroupId'] ];
  246. $groupInfo = $tencentObj->toSend($data);
  247. // \app\common\model\Test::update(["content"=>json_encode($groupInfo)],["id"=>1]);
  248. return $groupInfo;
  249. }
  250. //"https:\/\/console.tim.qq.com\/v4\/group_open_http_svc\/get_joined_group_list?sdkappid=1400458032&identifier=administrator&usersig=eJyrVgrxCdYrSy1SslIy0jNQ0gHzM1NS80oy0zLBwqamUNHilOzEgoLMFCUrQxMDAxNTCwNjI4hMakVBZlEqUNzU1NTIwMAAIlqSmQsSMzO0MDY0NDOwhJqSmQ40NNwowsvMzzXKw8wsLNIwRr9S28giPKjMPSkpKLPcLcfFPd3TO7swz9fXOdLTVqkWADrXLzo_&random=73931990&contenttype=json"
  251. // /**
  252. // * 删除群成员
  253. // */
  254. // public function delMemberFromGroup($user_id,$party_id) {
  255. // $random = rand(10000000,99999999);
  256. // $usersig = $this->usersig($user_id);
  257. // // 获取配置信息
  258. // $config = config("tencent_im");
  259. // $url = "https://console.tim.qq.com/v4/group_open_http_svc/delete_group_member";
  260. // $url .= "?sdkappid=".$config["sdkappid"];
  261. // $url .= "&identifier=administrator";
  262. // $url .= "&usersig=".$usersig;
  263. // $url .= "&random=".$random;
  264. // $url .= "&contenttype=json";
  265. // $tencentObj = new tencentim($url);
  266. // $data = [];
  267. // $data["GroupId"] = $party_id;
  268. // $data["Silence"] = 1;
  269. // $data["MemberToDel_Account"] = [$user_id];
  270. // $res = $tencentObj->toSend($data);
  271. // \app\common\model\Test::update(["content"=>json_encode($res)],["id"=>1]);
  272. // return $res;
  273. // }
  274. /**
  275. * 设置管理员(自定义消息)
  276. */
  277. public function setImManage($manage_user_id,$party_id,$status=0) {
  278. $random = rand(10000000,99999999);
  279. $usersig = $this->usersig("administrator");
  280. // 获取配置信息
  281. $config = config("tencent_im");
  282. $url = "https://console.tim.qq.com/v4/group_open_http_svc/modify_group_base_info";
  283. $url .= "?sdkappid=".$config["sdkappid"];
  284. $url .= "&identifier=administrator";
  285. $url .= "&usersig=".$usersig;
  286. $url .= "&random=".$random;
  287. $url .= "&contenttype=json";
  288. $tencentObj = new tencentim($url);
  289. // 获取群组信息
  290. $groupInfo = $this->getGroupInfo($party_id);
  291. if($groupInfo["GroupInfo"][0]["ErrorCode"] > 0) return false;
  292. $roomInfoData = $groupInfo["GroupInfo"][0]["AppDefinedData"];
  293. $roomInfo = [];
  294. if($roomInfoData) foreach($roomInfoData as $k => $v) {
  295. if($v["Key"] == "roomInfo") $roomInfo = $v["Value"];
  296. }
  297. $roomInfo = json_decode($roomInfo,true);
  298. // 更新的信息
  299. if(isset($roomInfo["managers"])) {
  300. if(!$roomInfo["managers"]) $roomInfo["managers"] = [];
  301. }
  302. if ($status==1){
  303. array_push($roomInfo["managers"],$manage_user_id);
  304. $roomInfo["managers"] = array_unique($roomInfo["managers"]);
  305. }else{
  306. foreach ($roomInfo["managers"] as $k => $v){
  307. if ($v == $manage_user_id) {
  308. unset($roomInfo["managers"][$k]);
  309. break;
  310. }
  311. }
  312. }
  313. $data = [];
  314. $data["GroupId"] = $party_id;
  315. $data["AppDefinedData"][0] = [
  316. "Key" => "roomInfo",
  317. "Value" => json_encode($roomInfo),
  318. // "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)),
  319. ];
  320. if(isset($roomInfo["ownerId"]) && isset($roomInfo["roomName"])) {
  321. $tencentObj->toSend($data);
  322. }
  323. }
  324. /**
  325. * 强制下线
  326. */
  327. public function downSite($user_id,$groupsites) {
  328. $random = rand(10000000,99999999);
  329. $usersig = $this->usersig("administrator");
  330. // 获取配置信息
  331. $config = config("tencent_im");
  332. $url = "https://console.tim.qq.com/v4/group_open_http_svc/modify_group_base_info";
  333. $url .= "?sdkappid=".$config["sdkappid"];
  334. $url .= "&identifier=administrator";
  335. $url .= "&usersig=".$usersig;
  336. $url .= "&random=".$random;
  337. $url .= "&contenttype=json";
  338. $tencentObj = new tencentim($url);
  339. // 先更新下麦时间
  340. $update = [];
  341. $update["offsite_time"] = time();
  342. $update["status"] = 2;
  343. \app\common\model\UserOnsiteTime::update($update,["user_id"=>$user_id,"status"=>1]);
  344. // 循环房间
  345. if($groupsites) foreach($groupsites as $k => $v) {
  346. // 循环座位
  347. $data = [];
  348. foreach($v["AppDefinedData"] as $m => $n) {
  349. // 解析字段信息
  350. $siteInfo = json_decode($n["Value"],true);
  351. if($m != 0 && isset($siteInfo["status"]) && $siteInfo["status"] == 1 && isset($siteInfo["status"]) && $siteInfo["user"] == $user_id) {
  352. $data["AppDefinedData"][] = [
  353. "Key" => $n["Key"],
  354. "Value" => '{"mute":false,"status":0,"user":""}',
  355. ];
  356. }
  357. }
  358. if(!empty($data)) {
  359. $data["GroupId"] = $v["GroupId"];
  360. return $tencentObj->toSend($data);
  361. }
  362. }
  363. }
  364. /**
  365. * 踢出房间内所有用户
  366. */
  367. public function outMemberFromRoom($party_id) {
  368. if($party_id <= 0) return false;
  369. $message = [];
  370. $message["type"] = "91";
  371. $message["content"] = "房间已被管理员关闭!详情请联系客服!";
  372. $msgData = [];
  373. $msgData["version"] = "1.0";
  374. $msgData["action"] = 301;
  375. $msgData["command"] = "";
  376. $msgData["message"] = json_encode($message);
  377. $random = rand(10000000,99999999);
  378. $usersig = $this->usersig("administrator");
  379. // 获取配置信息
  380. $config = config("tencent_im");
  381. $url = "https://console.tim.qq.com/v4/group_open_http_svc/send_group_msg";
  382. $url .= "?sdkappid=".$config["sdkappid"];
  383. $url .= "&identifier=administrator";
  384. $url .= "&usersig=".$usersig;
  385. $url .= "&random=".$random;
  386. $url .= "&contenttype=json";
  387. $tencentObj = new tencentim($url);
  388. $data = [];
  389. $data["GroupId"] = $party_id;
  390. $data["Random"] = rand(1000000,9999999);
  391. $data["MsgBody"][] = [
  392. "MsgType" => "TIMCustomElem",
  393. "MsgContent" => [
  394. "Data"=> json_encode($msgData)
  395. ],
  396. ];
  397. $tencentObj->toSend($data);
  398. }
  399. /**
  400. * 获取usersig签名
  401. */
  402. public function getUsersig() {
  403. $user_id = $this->auth->id;
  404. $this->success("获取成功!",$this->usersig($user_id));
  405. }
  406. /**
  407. * 获取usersig签名-具体操作
  408. */
  409. private function usersig($user_id) {
  410. // 获取配置信息
  411. $config = config("tencent_im");
  412. $usersigObj = new getusersig($config["sdkappid"],$config["key"]);
  413. $usersig = $usersigObj->genUserSig($user_id);
  414. return $usersig;
  415. }
  416. /**
  417. * 强制下麦
  418. */
  419. public function updateSiteInfo() {
  420. $user_id = $this->request->request("user_id");// 用户ID
  421. if(!$user_id) $this->error("参数缺失!");
  422. // 更新用户在线状态为离线
  423. \app\common\model\User::update(["is_online"=>0],["id"=>$user_id]);
  424. // \app\common\model\User::update(["is_live"=>0],["id"=>$user_id]);
  425. \app\common\model\Party::update(["is_online"=>0],["user_id"=>$user_id]);
  426. // 获取用户所在群信息
  427. $groupids = $this->getJoindGroupList($user_id);
  428. if($groupids["ErrorCode"] == 0) {
  429. $grouplist = $groupids["GroupIdList"];
  430. $groupsites = [];
  431. if($grouplist) foreach($grouplist as $k => $v) {
  432. // 获取群组消息
  433. $groupInfo = $this->getGroupInfo($v["GroupId"]);
  434. if($groupInfo["ErrorCode"] == 0) $groupsites[] = $groupInfo["GroupInfo"][0];
  435. // 踢出用户在线组
  436. $redis = new Redis();
  437. $redisconfig = config("redis");
  438. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  439. $redis->HDel("online_".$v["GroupId"],$user_id);
  440. }
  441. // 强制下麦
  442. $this->downSite($user_id,$groupsites);
  443. }
  444. $this->success("状态更新成功!");
  445. }
  446. /**
  447. * 更新麦位前四
  448. */
  449. public function updatePosition($user_id,$party_id) {
  450. // 获取用户头像
  451. $userInfo = \app\common\model\User::field("avatar")->where(["id"=>$user_id])->find();
  452. $userAvatar = isset($userInfo["avatar"])?$userInfo["avatar"]:"";
  453. $redis = new Redis();
  454. $redisconfig = config("redis");
  455. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  456. $redisData = $redis->get("party_".$party_id);
  457. if(!$redisData) {
  458. return true;
  459. }
  460. $partyInfo = json_decode($redisData,true);
  461. // 遍历已有头像
  462. $partyuser = isset($partyInfo["party_user"])?$partyInfo["party_user"]:"";
  463. if(is_array($partyuser)) foreach($partyuser as $k => $v) if($v === $userAvatar) unset($partyInfo["party_user"][$k]);
  464. $redis->set("party_".$party_id,json_encode($partyInfo));
  465. }
  466. /**
  467. * 取消排麦
  468. */
  469. public function cancelLineUp($user_id,$party_id) {
  470. if (!$party_id || !$user_id) {
  471. return false;
  472. }
  473. $redis = new Redis();
  474. $redisconfig = config("redis");
  475. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  476. $data=unserialize($redis->hGet("party_lineup",$party_id));
  477. // 更改红点排麦数量
  478. $num = 0;
  479. $send = false;
  480. if($data) foreach($data as $k => $v) {
  481. if($v["user_id"] == $user_id) {
  482. $send = true;
  483. unset($data[$k]);
  484. break;
  485. }
  486. }
  487. $redis->hSet("party_lineup",$party_id,serialize($data));
  488. if($send) {
  489. is_array($data) && $num = count($data);
  490. $random = rand(10000000,99999999);
  491. $usersig = $this->usersig("administrator");
  492. // 获取配置信息
  493. $config = config("tencent_im");
  494. $url = "https://console.tim.qq.com/v4/group_open_http_svc/send_group_msg";
  495. $url .= "?sdkappid=".$config["sdkappid"];
  496. $url .= "&identifier=administrator";
  497. $url .= "&usersig=".$usersig;
  498. $url .= "&random=".$random;
  499. $url .= "&contenttype=json";
  500. $tencentObj = new tencentim($url);
  501. $data = [];
  502. $data["GroupId"] = $party_id;
  503. $data["Random"] = rand(1000000,9999999);
  504. $message = [
  505. "message" => json_encode(["type"=>23,"content"=>$num]),
  506. "version" => "1.0",
  507. "action" => 301,
  508. "command" => ""
  509. ];
  510. $data["MsgBody"][] = [
  511. "MsgType" => "TIMCustomElem",
  512. "MsgContent" => [
  513. "Data"=> json_encode($message)
  514. ],
  515. ];
  516. $tencentObj->toSend($data);
  517. }
  518. }
  519. //trtc_callback,直播callback
  520. //https://cloud.tencent.com/document/product/647/51586#eventId
  521. /*
  522. 2021-12-24 15:34:22 start2:{
  523. "EventGroupId": 1,
  524. "EventType": 103,
  525. "CallbackTs": 1640331262241,
  526. "EventInfo": {
  527. "RoomId": 1604,
  528. "EventTs": 1640331262,
  529. "EventMsTs": 1640331262238,
  530. "UserId": "26",
  531. "Role": 20,
  532. "TerminalType": 2,
  533. "UserType": 3,
  534. "Reason": 1
  535. }
  536. }
  537. 2021-12-24 15:34:22 start2:{
  538. "EventGroupId": 1,
  539. "EventType": 101,
  540. "CallbackTs": 1640331262247,
  541. "EventInfo": {
  542. "RoomId": 1604,
  543. "EventTs": 1640331262,
  544. "EventMsTs": 1640331262238,
  545. "UserId": "26"
  546. }
  547. }
  548. 2021-12-24 15:34:55 start2:{
  549. "EventGroupId": 1,
  550. "EventType": 102,
  551. "CallbackTs": 1640331295244,
  552. "EventInfo": {
  553. "RoomId": 1604,
  554. "EventTs": 1640331295,
  555. "EventMsTs": 1640331295239
  556. }
  557. }
  558. 2021-12-24 15:34:55 start2:{
  559. "EventGroupId": 1,
  560. "EventType": 104,
  561. "CallbackTs": 1640331295244,
  562. "EventInfo": {
  563. "RoomId": 1604,
  564. "EventTs": 1640331295,
  565. "EventMsTs": 1640331295239,
  566. "UserId": "26",
  567. "Role": 20,
  568. "Reason": 1
  569. }
  570. }
  571. */
  572. public function trtc_callback(){
  573. $input = file_get_contents("php://input");
  574. filePut('start:'.$input,'callback.txt');
  575. /*$input = '{
  576. "EventGroupId": 1,
  577. "EventType": 104,
  578. "CallbackTs": 1640331295244,
  579. "EventInfo": {
  580. "RoomId": 1604,
  581. "EventTs": 1640331295,
  582. "EventMsTs": 1640331295239,
  583. "UserId": "26",
  584. "Role": 20,
  585. "Reason": 1
  586. }
  587. }';*/
  588. $input = json_decode($input,true);
  589. //房间事件组
  590. if($input['EventGroupId'] == 1){
  591. if($input['EventType'] == 101){
  592. //创建房间,第一个进来的
  593. }elseif($input['EventType'] == 102){
  594. //解散房间,最后一个走的
  595. //没有UserId
  596. }elseif($input['EventType'] == 103){
  597. //进入房间,主播role=20 观众role=21
  598. }elseif($input['EventType'] == 104){
  599. //退出房间,主播role=20 观众role=21
  600. if($input['EventInfo']['Role'] == 20){
  601. $live_bc = Db::name('livebc')->where('user_id',$input['EventInfo']['UserId'])->update(['is_online'=>0]);
  602. $rs_user = Db::name('user')->where('id',$input['EventInfo']['UserId'])->update(['is_livebc'=>0]);
  603. }
  604. }
  605. }elseif($input['EventGroupId'] == 2){
  606. }
  607. //结束
  608. echo json_encode(['code' => 0]);exit;
  609. }
  610. /**
  611. * 回调
  612. */
  613. public function callback() {
  614. $input = file_get_contents("php://input"); // 主题信息
  615. $input = json_decode($input,true);
  616. $CallbackCommand = $input["CallbackCommand"];
  617. switch ($CallbackCommand) {
  618. case 'State.StateChange': // 用户在线状态变更
  619. $info = $input["Info"];
  620. if($info["Action"] == "Login") { // 用户登录
  621. $loginToday = Db::name('user')->where('id',$info["To_Account"])->whereTime('onlinetime', 'today')->find();
  622. if ($loginToday){
  623. \app\common\model\User::update(["is_online"=>1,"onlinetime"=>time()],["id"=>$info["To_Account"]]);
  624. }else{
  625. \app\common\model\User::update(["is_online"=>1,"onlinetime"=>time()],["id"=>$info["To_Account"]]);
  626. }
  627. $res = [];
  628. $res["ActionStatus"] = "OK";
  629. $res["ErrorCode"] = 0;
  630. $res["ErrorInfo"] = "";
  631. echo json_encode($res);exit;
  632. } elseif($info["Action"] == "Logout" || $info["Action"] == "Disconnect") { // 用户退出 不需要管什么原因
  633. // 更新用户在线状态为离线
  634. \app\common\model\User::update(["is_online"=>0],["id"=>$info["To_Account"]]);
  635. //\app\common\model\User::update(["is_live"=>0],["id"=>$info["To_Account"]]);
  636. // 获取用户在派对直播间情况信息
  637. $redis = new Redis();
  638. $redisconfig = config("redis");
  639. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  640. $livingUserPartyId = $redis->hGet("livingUser", $info["To_Account"]);
  641. if ($livingUserPartyId) {
  642. // 扣除在线用户在房间情况
  643. $redis->hDel("livingUser", $info["To_Account"]);
  644. $redis->HDel("online_" . $livingUserPartyId, $info["To_Account"]);
  645. }
  646. // 获取用户所在群信息
  647. $groupids = $this->getJoindGroupList($info["To_Account"]);
  648. // \app\common\model\Test::update(["content"=>json_encode($groupids)],["id"=>1]);
  649. if($groupids["ErrorCode"] == 0) {
  650. $grouplist = $groupids["GroupIdList"];
  651. $groupsites = [];
  652. if($grouplist) foreach($grouplist as $k => $v) {
  653. // 获取群组消息
  654. $groupInfo = $this->getGroupInfo($v["GroupId"]);
  655. if($groupInfo["ErrorCode"] == 0) $groupsites[] = $groupInfo["GroupInfo"][0];
  656. // 踢出用户在线组
  657. $redis = new Redis();
  658. $redisconfig = config("redis");
  659. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  660. $redis->HDel("online_".$v["GroupId"],$info["To_Account"]);
  661. // 更新麦位前四
  662. $this->updatePosition($info["To_Account"],$v["GroupId"]);
  663. // 取消排麦
  664. $this->cancelLineup($info["To_Account"],$v["GroupId"]);
  665. // 房间状态变更
  666. $partyInfo = $redis->get("party_".$v["GroupId"]);
  667. if($partyInfo) {
  668. $partyInfo = json_decode($partyInfo,true);
  669. $memCount = count($redis->hGetAll("online_".$v["GroupId"]));
  670. if($memCount <= 0) {
  671. $partyInfo["is_online"] = 0;
  672. $redis->set("party_".$v["GroupId"],json_encode($partyInfo));
  673. \app\common\model\Party::update(["is_online"=>0],["id"=>$v["GroupId"]]);
  674. }
  675. }
  676. $liveInfo = $redis->get("live_".$v["GroupId"]);
  677. if($liveInfo) {
  678. $liveInfo = json_decode($liveInfo,true);
  679. $memCount = count($redis->hGetAll("online_".$v["GroupId"]));
  680. if($memCount <= 0) {
  681. $liveInfo["is_online"] = 0;
  682. $redis->set("live_".$v["GroupId"],json_encode($liveInfo));
  683. \app\common\model\Party::update(["is_online"=>0],["id"=>$v["GroupId"]]);
  684. }
  685. }
  686. }
  687. // 强制下麦
  688. print_r($this->downSite($info["To_Account"],$groupsites));
  689. }
  690. }
  691. break;
  692. // case 'Group.CallbackAfterNewMemberJoin': // 新成员入群
  693. // $redis = new Redis();
  694. // $redisconfig = config("redis");
  695. // $redis->connect($redisconfig["host"], $redisconfig["port"]);
  696. // $members = $input["NewMemberList"];
  697. // if($members) foreach($members as $k => $v) {
  698. // $redis->hSet("online_".$input["GroupId"],$v["Member_Account"],$v["Member_Account"]);
  699. // // 更新房间在线状态
  700. // $party_id = $input["GroupId"];
  701. // $partyInfo = $redis->get("party_".$party_id);
  702. // if($partyInfo) {
  703. // $partyInfo = json_decode($partyInfo,true);
  704. // if($partyInfo["is_online"] != 1) {
  705. // $partyInfo["is_online"] = 1;
  706. // $redis->set("party_".$party_id,json_encode($partyInfo));
  707. // \app\common\model\Party::update(["is_online"=>1],["id"=>$party_id]);
  708. // }
  709. // }
  710. // }
  711. // $res = [];
  712. // $res["ActionStatus"] = "OK";
  713. // $res["ErrorCode"] = 0;
  714. // $res["ErrorInfo"] = "";
  715. // echo json_encode($res);
  716. // break;
  717. // case 'Group.CallbackAfterMemberExit': // 成员离开
  718. // $redis = new Redis();
  719. // $redisconfig = config("redis");
  720. // $redis->connect($redisconfig["host"], $redisconfig["port"]);
  721. // $members = $input["ExitMemberList"];
  722. // if($members) foreach($members as $k => $v) {
  723. // $redis->HDel("online_".$input["GroupId"],$v["Member_Account"]);
  724. // }
  725. // // 更新房间在线状态
  726. // $party_id = $input["GroupId"];
  727. // $partyInfo = $redis->get("party_".$party_id);
  728. // if($partyInfo) {
  729. // $partyInfo = json_decode($partyInfo,true);
  730. // $memCount = count($redis->hGetAll("online_".$party_id));
  731. // if($memCount <= 0) {
  732. // $partyInfo["is_online"] = 0;
  733. // $redis->set("party_".$party_id,json_encode($partyInfo));
  734. // \app\common\model\Party::update(["is_online"=>0],["id"=>$party_id]);
  735. // }
  736. // }
  737. //
  738. // $liveInfo = $redis->get("live_".$party_id);
  739. // if($liveInfo) {
  740. // $liveInfo = json_decode($liveInfo,true);
  741. // $memCount = count($redis->hGetAll("online_".$party_id));
  742. // if($memCount <= 0) {
  743. // $liveInfo["is_online"] = 0;
  744. // $redis->set("live_".$party_id,json_encode($liveInfo));
  745. // \app\common\model\Party::update(["is_online"=>0],["id"=>$party_id]);
  746. // }
  747. // }
  748. //
  749. // $res = [];
  750. // $res["ActionStatus"] = "OK";
  751. // $res["ErrorCode"] = 0;
  752. // $res["ErrorInfo"] = "";
  753. // echo json_encode($res);
  754. // break;
  755. /*case 'Group.CallbackBeforeSendMsg': // 群内发送消息之前
  756. $config = config("wxMiniProgram");
  757. $access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$config["appid"]."&secret=".$config["secret"];
  758. $access_token_arr = $this->getJson($access_token_url);
  759. $access_token = isset($access_token_arr["access_token"])?$access_token_arr["access_token"]:"";
  760. if(!$access_token) {
  761. $res = [];
  762. $res["ActionStatus"] = "OK";
  763. $res["ErrorInfo"] = "";
  764. $res["ErrorCode"] = 0;
  765. echo json_encode($res);
  766. break;
  767. }
  768. // \app\common\model\Test::update(["content"=>json_encode($access_token)],["id"=>1]);
  769. $infoMessage = $input["MsgBody"];
  770. $badMessage = false;
  771. if($infoMessage) foreach($infoMessage as $k => $v) {
  772. if(isset($v["MsgContent"]["Text"])) {
  773. $message = json_decode($v["MsgContent"]["Text"],true);
  774. $content = isset($message["content"])?$message["content"]:"";
  775. $message_check_url ='https://api.weixin.qq.com/wxa/msg_sec_check?access_token='.$access_token;
  776. $tencentObj = new tencentim($message_check_url);
  777. $content_json = '{"content": "'.$content.'"}';
  778. $body = $tencentObj->messageCheck($content_json);
  779. if($body["errcode"] == 87014) {
  780. // \app\common\model\Test::update(["content"=>$body["errcode"]],["id"=>1]);
  781. // $res = [];
  782. // $res["ActionStatus"] = "OK";
  783. // $res["ErrorInfo"] = "检测到敏感词汇!";
  784. // $res["ErrorCode"] = 1;
  785. // echo json_encode($res);exit;
  786. $message["content"] = "*******";
  787. $infoMessage[$k]["MsgContent"]["Text"] = json_encode($message);
  788. $badMessage = true;
  789. }
  790. }
  791. }
  792. if($badMessage) {
  793. $res = [];
  794. $res["ActionStatus"] = "OK";
  795. $res["ErrorInfo"] = "检测到敏感词汇!";
  796. $res["ErrorCode"] = 10102;
  797. $res["MsgBody"] = $infoMessage;
  798. } else {
  799. $res = [];
  800. $res["ActionStatus"] = "OK";
  801. $res["ErrorInfo"] = "";
  802. $res["ErrorCode"] = 0;
  803. }
  804. // \app\common\model\Test::update(["content"=>json_encode($res)],["id"=>1]);
  805. echo json_encode($res);exit;
  806. break;*/
  807. default:
  808. break;
  809. }
  810. exit;
  811. }
  812. private function getJson($url){
  813. $ch = curl_init();
  814. curl_setopt($ch, CURLOPT_URL, $url);
  815. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  816. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  817. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  818. $output = curl_exec($ch);
  819. curl_close($ch);
  820. return json_decode($output, true);
  821. }
  822. /**
  823. * 发送消息给某人
  824. */
  825. public function sendMessageToUser($from_user,$to_user,$message) {
  826. // $from_user = 54;
  827. // $to_user = 6;
  828. // $message = "sdsd";
  829. $random = rand(10000000,99999999);
  830. $usersig = $this->usersig("administrator");
  831. // 获取配置信息
  832. $config = config("tencent_im");
  833. $url = "https://console.tim.qq.com/v4/openim/sendmsg";
  834. $url .= "?sdkappid=".$config["sdkappid"];
  835. $url .= "&identifier=administrator";
  836. $url .= "&usersig=".$usersig;
  837. $url .= "&random=".$random;
  838. $url .= "&contenttype=json";
  839. $tencentObj = new tencentim($url);
  840. $data = [];
  841. $data["SyncOtherMachine"] = 1;
  842. $data["From_Account"] = (string)$from_user;
  843. $data["To_Account"] = (string)$to_user;
  844. $data["MsgRandom"] = rand(1000000,9999999);
  845. $data["MsgTimeStamp"] = time();
  846. $data["MsgBody"][] = [
  847. "MsgType" => "TIMTextElem",
  848. "MsgContent" => [
  849. "Text"=> $message
  850. ],
  851. ];
  852. $tencentObj->toSend($data);
  853. }
  854. /**
  855. * 发送消息给某人-接口调用
  856. */
  857. public function sendToUser() {
  858. $from_user = $this->request->request("from_user");// 发送者
  859. $to_user = $this->request->request("to_user");// 接收者
  860. $message = $this->request->request("message");// 接收者
  861. if(!$from_user || !$to_user || !$message) $this->error("参数缺失!");
  862. $this->sendMessageToUser($from_user,$to_user,$message);
  863. }
  864. //请求的基础参数
  865. private function baseTencentObj($url)
  866. {
  867. $random = rand(10000000,99999999);
  868. $usersig = $this->usersig("administrator");
  869. // 获取配置信息
  870. $config = config("tencent_im");
  871. $url = "$url";
  872. $url .= "?sdkappid=".$config["sdkappid"];
  873. $url .= "&identifier=administrator";
  874. $url .= "&usersig=".$usersig;
  875. $url .= "&random=".$random;
  876. $url .= "&contenttype=json";
  877. return new tencentim($url);
  878. }
  879. /** 设置管理员(im端的改变)
  880. * @param $user_id
  881. * @param $party_id
  882. * @param int $status 1:yes 0:no
  883. * @return array|false|mixed
  884. */
  885. public function setUpIMAdmin($user_id, $party_id, int $status=0) {
  886. $url = "https://console.tim.qq.com/v4/group_open_http_svc/modify_group_member_info";
  887. $tencentObj = $this->baseTencentObj($url);
  888. $groupInfo = $this->getGroupInfo($party_id);
  889. if($groupInfo["GroupInfo"][0]["ErrorCode"] > 0) return false;
  890. // 更新的信息
  891. $data = [];
  892. $data["GroupId"] = $party_id;
  893. if ($status==1){
  894. $data['Role'] = 'Admin';
  895. }else{
  896. $data['Role'] = 'Member';
  897. }
  898. $data['Member_Account'] = $user_id;
  899. return $tencentObj->toSend($data);
  900. }
  901. //获取群成员详细资料(目前没用到)
  902. private function getGroupMemberDetails($party_id)
  903. {
  904. $url = "https://console.tim.qq.com/v4/group_open_http_svc/get_group_member_info";
  905. $tencentObj = $this->baseTencentObj($url);
  906. // 获取群组信息
  907. $groupInfo = $this->getGroupInfo($party_id);
  908. if($groupInfo["GroupInfo"][0]["ErrorCode"] > 0) return false;
  909. // 更新的信息
  910. $data = [];
  911. $data["GroupId"] = $party_id;
  912. return $tencentObj->toSend($data);
  913. }
  914. //IM管理员维护,同步本地管理员
  915. public function imAdminMaintenance()
  916. {
  917. $partyIds = \app\admin\model\party\Party::column('id');
  918. foreach ($partyIds as $party_id){
  919. $groupInfo = $this->getGroupInfo($party_id);
  920. if($groupInfo["GroupInfo"][0]["ErrorCode"] > 0) continue;
  921. $roomInfoData = $groupInfo["GroupInfo"][0]["AppDefinedData"];
  922. $roomInfo = [];
  923. if($roomInfoData) foreach($roomInfoData as $k => $v) {
  924. if($v["Key"] == "roomInfo") $roomInfo = $v["Value"];
  925. }
  926. $roomInfo = json_decode($roomInfo,true);
  927. // 更新管理员
  928. if(isset($roomInfo["managers"]) && !empty($roomInfo['managers'])) {
  929. foreach ($roomInfo['managers'] as $v){
  930. $this->setUpIMAdmin($v,$party_id,1);
  931. }
  932. }
  933. }
  934. $this->success("成功!");
  935. }
  936. }