Tenim.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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. $roomId = $input['EventInfo']['RoomId'];
  590. //房间事件组
  591. if($input['EventGroupId'] == 1){
  592. if($input['EventType'] == 101){
  593. //创建房间,第一个进来的
  594. }elseif($input['EventType'] == 102){
  595. //解散房间,最后一个走的
  596. //没有UserId
  597. }elseif($input['EventType'] == 103){
  598. //进入房间,主播role=20 观众role=21
  599. $redis = new Redis();
  600. $redisconfig = config("redis");
  601. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  602. $userId = $input['EventInfo']['UserId'];
  603. $redis->hSet("online_" . $roomId, $userId, $userId);
  604. // 记录在线用户在房间情况
  605. $redis->hSet("livingUser",$userId,$roomId);
  606. // 更新房间在线状态
  607. $partyInfo = $redis->get("party_" . $roomId);
  608. if ($partyInfo) {
  609. $partyInfo = json_decode($partyInfo, true);
  610. if ($partyInfo["is_online"] != 1) {
  611. $partyInfo["is_online"] = 1;
  612. $redis->set("party_" . $roomId, json_encode($partyInfo));
  613. \app\common\model\Party::update(["is_online" => 1], ["id" => $roomId]);
  614. }
  615. }
  616. // 更新房间在线状态
  617. $liveInfo = $redis->get("livebc_" . $roomId);
  618. if ($liveInfo) {
  619. $liveInfo = json_decode($liveInfo, true);
  620. if ($input['EventInfo']['Role'] == 20) {
  621. $liveInfo["is_online"] = 1;
  622. $redis->set("livebc_" . $roomId, json_encode($liveInfo));
  623. \app\common\model\Party::update(["is_online" => 1], ["id" => $roomId]);
  624. $rs_user = Db::name('user')->where('id',$userId)->update(['is_livebc'=>1]);
  625. }
  626. }
  627. }elseif($input['EventType'] == 104){
  628. //退出房间,主播role=20 观众role=21
  629. $redis = new Redis();
  630. $redisconfig = config("redis");
  631. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  632. $userId = $input['EventInfo']['UserId'];
  633. $redis->HDel("online_" . $roomId, $userId);
  634. // 扣除在线用户在房间情况
  635. $redis->hDel("livingUser",$userId);
  636. // 更新房间在线状态
  637. $partyInfo = $redis->get("party_" . $roomId);
  638. if ($partyInfo) {
  639. $partyInfo = json_decode($partyInfo, true);
  640. $memCount = count($redis->hGetAll("online_" . $roomId));
  641. if ($memCount <= 0) {
  642. $partyInfo["is_online"] = 0;
  643. $redis->set("party_" . $roomId, json_encode($partyInfo));
  644. \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]);
  645. }
  646. }
  647. //
  648. $liveInfo = $redis->get("livebc_" . $roomId);
  649. if ($liveInfo) {
  650. $liveInfo = json_decode($liveInfo, true);
  651. if($input['EventInfo']['Role'] == 20){
  652. $liveInfo["is_online"] = 0;
  653. $redis->set("livebc_" . $roomId, json_encode($liveInfo));
  654. \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]);
  655. $rs_user = Db::name('user')->where('id',$userId)->update(['is_livebc'=>0]);
  656. }
  657. }
  658. }
  659. }elseif($input['EventGroupId'] == 2){
  660. }
  661. //结束
  662. echo json_encode(['code' => 0]);exit;
  663. }
  664. /**
  665. * 回调
  666. */
  667. public function callback() {
  668. $input = file_get_contents("php://input"); // 主题信息
  669. $input = json_decode($input,true);
  670. $CallbackCommand = $input["CallbackCommand"];
  671. switch ($CallbackCommand) {
  672. case 'State.StateChange': // 用户在线状态变更
  673. $info = $input["Info"];
  674. if($info["Action"] == "Login") { // 用户登录
  675. $loginToday = Db::name('user')->where('id',$info["To_Account"])->whereTime('onlinetime', 'today')->find();
  676. if ($loginToday){
  677. \app\common\model\User::update(["is_online"=>1,"onlinetime"=>time()],["id"=>$info["To_Account"]]);
  678. }else{
  679. \app\common\model\User::update(["is_online"=>1,"onlinetime"=>time()],["id"=>$info["To_Account"]]);
  680. }
  681. $res = [];
  682. $res["ActionStatus"] = "OK";
  683. $res["ErrorCode"] = 0;
  684. $res["ErrorInfo"] = "";
  685. echo json_encode($res);exit;
  686. } elseif($info["Action"] == "Logout" || $info["Action"] == "Disconnect") { // 用户退出 不需要管什么原因
  687. // 更新用户在线状态为离线
  688. \app\common\model\User::update(["is_online"=>0],["id"=>$info["To_Account"]]);
  689. //\app\common\model\User::update(["is_live"=>0],["id"=>$info["To_Account"]]);
  690. // 获取用户在派对直播间情况信息
  691. $redis = new Redis();
  692. $redisconfig = config("redis");
  693. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  694. $livingUserPartyId = $redis->hGet("livingUser", $info["To_Account"]);
  695. if ($livingUserPartyId) {
  696. // 扣除在线用户在房间情况
  697. $redis->hDel("livingUser", $info["To_Account"]);
  698. $redis->HDel("online_" . $livingUserPartyId, $info["To_Account"]);
  699. }
  700. // 获取用户所在群信息
  701. $groupids = $this->getJoindGroupList($info["To_Account"]);
  702. // \app\common\model\Test::update(["content"=>json_encode($groupids)],["id"=>1]);
  703. if($groupids["ErrorCode"] == 0) {
  704. $grouplist = $groupids["GroupIdList"];
  705. $groupsites = [];
  706. if($grouplist) foreach($grouplist as $k => $v) {
  707. // 获取群组消息
  708. $groupInfo = $this->getGroupInfo($v["GroupId"]);
  709. if($groupInfo["ErrorCode"] == 0) $groupsites[] = $groupInfo["GroupInfo"][0];
  710. // 踢出用户在线组
  711. $redis = new Redis();
  712. $redisconfig = config("redis");
  713. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  714. $redis->HDel("online_".$v["GroupId"],$info["To_Account"]);
  715. // 更新麦位前四
  716. $this->updatePosition($info["To_Account"],$v["GroupId"]);
  717. // 取消排麦
  718. $this->cancelLineup($info["To_Account"],$v["GroupId"]);
  719. // 房间状态变更
  720. $partyInfo = $redis->get("party_".$v["GroupId"]);
  721. if($partyInfo) {
  722. $partyInfo = json_decode($partyInfo,true);
  723. $memCount = count($redis->hGetAll("online_".$v["GroupId"]));
  724. if($memCount <= 0) {
  725. $partyInfo["is_online"] = 0;
  726. $redis->set("party_".$v["GroupId"],json_encode($partyInfo));
  727. \app\common\model\Party::update(["is_online"=>0],["id"=>$v["GroupId"]]);
  728. }
  729. }
  730. $liveInfo = $redis->get("live_".$v["GroupId"]);
  731. if($liveInfo) {
  732. $liveInfo = json_decode($liveInfo,true);
  733. $memCount = count($redis->hGetAll("online_".$v["GroupId"]));
  734. if($memCount <= 0) {
  735. $liveInfo["is_online"] = 0;
  736. $redis->set("live_".$v["GroupId"],json_encode($liveInfo));
  737. \app\common\model\Party::update(["is_online"=>0],["id"=>$v["GroupId"]]);
  738. }
  739. }
  740. }
  741. // 强制下麦
  742. print_r($this->downSite($info["To_Account"],$groupsites));
  743. }
  744. }
  745. break;
  746. // case 'Group.CallbackAfterNewMemberJoin': // 新成员入群
  747. // $redis = new Redis();
  748. // $redisconfig = config("redis");
  749. // $redis->connect($redisconfig["host"], $redisconfig["port"]);
  750. // $members = $input["NewMemberList"];
  751. // if($members) foreach($members as $k => $v) {
  752. // $redis->hSet("online_".$input["GroupId"],$v["Member_Account"],$v["Member_Account"]);
  753. // // 更新房间在线状态
  754. // $party_id = $input["GroupId"];
  755. // $partyInfo = $redis->get("party_".$party_id);
  756. // if($partyInfo) {
  757. // $partyInfo = json_decode($partyInfo,true);
  758. // if($partyInfo["is_online"] != 1) {
  759. // $partyInfo["is_online"] = 1;
  760. // $redis->set("party_".$party_id,json_encode($partyInfo));
  761. // \app\common\model\Party::update(["is_online"=>1],["id"=>$party_id]);
  762. // }
  763. // }
  764. // }
  765. // $res = [];
  766. // $res["ActionStatus"] = "OK";
  767. // $res["ErrorCode"] = 0;
  768. // $res["ErrorInfo"] = "";
  769. // echo json_encode($res);
  770. // break;
  771. // case 'Group.CallbackAfterMemberExit': // 成员离开
  772. // $redis = new Redis();
  773. // $redisconfig = config("redis");
  774. // $redis->connect($redisconfig["host"], $redisconfig["port"]);
  775. // $members = $input["ExitMemberList"];
  776. // if($members) foreach($members as $k => $v) {
  777. // $redis->HDel("online_".$input["GroupId"],$v["Member_Account"]);
  778. // }
  779. // // 更新房间在线状态
  780. // $party_id = $input["GroupId"];
  781. // $partyInfo = $redis->get("party_".$party_id);
  782. // if($partyInfo) {
  783. // $partyInfo = json_decode($partyInfo,true);
  784. // $memCount = count($redis->hGetAll("online_".$party_id));
  785. // if($memCount <= 0) {
  786. // $partyInfo["is_online"] = 0;
  787. // $redis->set("party_".$party_id,json_encode($partyInfo));
  788. // \app\common\model\Party::update(["is_online"=>0],["id"=>$party_id]);
  789. // }
  790. // }
  791. //
  792. // $liveInfo = $redis->get("live_".$party_id);
  793. // if($liveInfo) {
  794. // $liveInfo = json_decode($liveInfo,true);
  795. // $memCount = count($redis->hGetAll("online_".$party_id));
  796. // if($memCount <= 0) {
  797. // $liveInfo["is_online"] = 0;
  798. // $redis->set("live_".$party_id,json_encode($liveInfo));
  799. // \app\common\model\Party::update(["is_online"=>0],["id"=>$party_id]);
  800. // }
  801. // }
  802. //
  803. // $res = [];
  804. // $res["ActionStatus"] = "OK";
  805. // $res["ErrorCode"] = 0;
  806. // $res["ErrorInfo"] = "";
  807. // echo json_encode($res);
  808. // break;
  809. /*case 'Group.CallbackBeforeSendMsg': // 群内发送消息之前
  810. $config = config("wxMiniProgram");
  811. $access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$config["appid"]."&secret=".$config["secret"];
  812. $access_token_arr = $this->getJson($access_token_url);
  813. $access_token = isset($access_token_arr["access_token"])?$access_token_arr["access_token"]:"";
  814. if(!$access_token) {
  815. $res = [];
  816. $res["ActionStatus"] = "OK";
  817. $res["ErrorInfo"] = "";
  818. $res["ErrorCode"] = 0;
  819. echo json_encode($res);
  820. break;
  821. }
  822. // \app\common\model\Test::update(["content"=>json_encode($access_token)],["id"=>1]);
  823. $infoMessage = $input["MsgBody"];
  824. $badMessage = false;
  825. if($infoMessage) foreach($infoMessage as $k => $v) {
  826. if(isset($v["MsgContent"]["Text"])) {
  827. $message = json_decode($v["MsgContent"]["Text"],true);
  828. $content = isset($message["content"])?$message["content"]:"";
  829. $message_check_url ='https://api.weixin.qq.com/wxa/msg_sec_check?access_token='.$access_token;
  830. $tencentObj = new tencentim($message_check_url);
  831. $content_json = '{"content": "'.$content.'"}';
  832. $body = $tencentObj->messageCheck($content_json);
  833. if($body["errcode"] == 87014) {
  834. // \app\common\model\Test::update(["content"=>$body["errcode"]],["id"=>1]);
  835. // $res = [];
  836. // $res["ActionStatus"] = "OK";
  837. // $res["ErrorInfo"] = "检测到敏感词汇!";
  838. // $res["ErrorCode"] = 1;
  839. // echo json_encode($res);exit;
  840. $message["content"] = "*******";
  841. $infoMessage[$k]["MsgContent"]["Text"] = json_encode($message);
  842. $badMessage = true;
  843. }
  844. }
  845. }
  846. if($badMessage) {
  847. $res = [];
  848. $res["ActionStatus"] = "OK";
  849. $res["ErrorInfo"] = "检测到敏感词汇!";
  850. $res["ErrorCode"] = 10102;
  851. $res["MsgBody"] = $infoMessage;
  852. } else {
  853. $res = [];
  854. $res["ActionStatus"] = "OK";
  855. $res["ErrorInfo"] = "";
  856. $res["ErrorCode"] = 0;
  857. }
  858. // \app\common\model\Test::update(["content"=>json_encode($res)],["id"=>1]);
  859. echo json_encode($res);exit;
  860. break;*/
  861. default:
  862. break;
  863. }
  864. exit;
  865. }
  866. private function getJson($url){
  867. $ch = curl_init();
  868. curl_setopt($ch, CURLOPT_URL, $url);
  869. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  870. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  871. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  872. $output = curl_exec($ch);
  873. curl_close($ch);
  874. return json_decode($output, true);
  875. }
  876. /**
  877. * 发送消息给某人
  878. */
  879. public function sendMessageToUser($from_user,$to_user,$message) {
  880. // $from_user = 54;
  881. // $to_user = 6;
  882. // $message = "sdsd";
  883. $random = rand(10000000,99999999);
  884. $usersig = $this->usersig("administrator");
  885. // 获取配置信息
  886. $config = config("tencent_im");
  887. $url = "https://console.tim.qq.com/v4/openim/sendmsg";
  888. $url .= "?sdkappid=".$config["sdkappid"];
  889. $url .= "&identifier=administrator";
  890. $url .= "&usersig=".$usersig;
  891. $url .= "&random=".$random;
  892. $url .= "&contenttype=json";
  893. $tencentObj = new tencentim($url);
  894. $data = [];
  895. $data["SyncOtherMachine"] = 1;
  896. $data["From_Account"] = (string)$from_user;
  897. $data["To_Account"] = (string)$to_user;
  898. $data["MsgRandom"] = rand(1000000,9999999);
  899. $data["MsgTimeStamp"] = time();
  900. $data["MsgBody"][] = [
  901. "MsgType" => "TIMTextElem",
  902. "MsgContent" => [
  903. "Text"=> $message
  904. ],
  905. ];
  906. $tencentObj->toSend($data);
  907. }
  908. /**
  909. * 发送消息给某人-接口调用
  910. */
  911. public function sendToUser() {
  912. $from_user = $this->request->request("from_user");// 发送者
  913. $to_user = $this->request->request("to_user");// 接收者
  914. $message = $this->request->request("message");// 接收者
  915. if(!$from_user || !$to_user || !$message) $this->error("参数缺失!");
  916. $this->sendMessageToUser($from_user,$to_user,$message);
  917. }
  918. //请求的基础参数
  919. private function baseTencentObj($url)
  920. {
  921. $random = rand(10000000,99999999);
  922. $usersig = $this->usersig("administrator");
  923. // 获取配置信息
  924. $config = config("tencent_im");
  925. $url = "$url";
  926. $url .= "?sdkappid=".$config["sdkappid"];
  927. $url .= "&identifier=administrator";
  928. $url .= "&usersig=".$usersig;
  929. $url .= "&random=".$random;
  930. $url .= "&contenttype=json";
  931. return new tencentim($url);
  932. }
  933. /** 设置管理员(im端的改变)
  934. * @param $user_id
  935. * @param $party_id
  936. * @param int $status 1:yes 0:no
  937. * @return array|false|mixed
  938. */
  939. public function setUpIMAdmin($user_id, $party_id, int $status=0) {
  940. $url = "https://console.tim.qq.com/v4/group_open_http_svc/modify_group_member_info";
  941. $tencentObj = $this->baseTencentObj($url);
  942. $groupInfo = $this->getGroupInfo($party_id);
  943. if($groupInfo["GroupInfo"][0]["ErrorCode"] > 0) return false;
  944. // 更新的信息
  945. $data = [];
  946. $data["GroupId"] = $party_id;
  947. if ($status==1){
  948. $data['Role'] = 'Admin';
  949. }else{
  950. $data['Role'] = 'Member';
  951. }
  952. $data['Member_Account'] = $user_id;
  953. return $tencentObj->toSend($data);
  954. }
  955. //获取群成员详细资料(目前没用到)
  956. private function getGroupMemberDetails($party_id)
  957. {
  958. $url = "https://console.tim.qq.com/v4/group_open_http_svc/get_group_member_info";
  959. $tencentObj = $this->baseTencentObj($url);
  960. // 获取群组信息
  961. $groupInfo = $this->getGroupInfo($party_id);
  962. if($groupInfo["GroupInfo"][0]["ErrorCode"] > 0) return false;
  963. // 更新的信息
  964. $data = [];
  965. $data["GroupId"] = $party_id;
  966. return $tencentObj->toSend($data);
  967. }
  968. //IM管理员维护,同步本地管理员
  969. public function imAdminMaintenance()
  970. {
  971. $partyIds = \app\admin\model\party\Party::column('id');
  972. foreach ($partyIds as $party_id){
  973. $groupInfo = $this->getGroupInfo($party_id);
  974. if($groupInfo["GroupInfo"][0]["ErrorCode"] > 0) continue;
  975. $roomInfoData = $groupInfo["GroupInfo"][0]["AppDefinedData"];
  976. $roomInfo = [];
  977. if($roomInfoData) foreach($roomInfoData as $k => $v) {
  978. if($v["Key"] == "roomInfo") $roomInfo = $v["Value"];
  979. }
  980. $roomInfo = json_decode($roomInfo,true);
  981. // 更新管理员
  982. if(isset($roomInfo["managers"]) && !empty($roomInfo['managers'])) {
  983. foreach ($roomInfo['managers'] as $v){
  984. $this->setUpIMAdmin($v,$party_id,1);
  985. }
  986. }
  987. }
  988. $this->success("成功!");
  989. }
  990. }