Tenim.php 40 KB

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