Tenim.php 50 KB

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