Tenim.php 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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"] = [(string)$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'])) {
  637. if ($tempData['user'] == $userId) {//找到用户清理麦序
  638. $tempData['status'] = 0;
  639. $tempData['user'] = "";
  640. } else {
  641. if (empty($tempData['user']) && $tempData['status'] == 1) {
  642. $tempData['status'] = 0;
  643. }
  644. }
  645. }
  646. $imVal['value'] = json_encode($tempData);
  647. }
  648. }
  649. $imAttrParams = [
  650. 'party_id' => $roomId,
  651. 'party_attr' => $imRes['data'],
  652. ];
  653. $tenimService->getGroupAttrEdit($imAttrParams);
  654. }
  655. }
  656. }
  657. }elseif($input['EventGroupId'] == 2){
  658. }
  659. //结束
  660. echo json_encode(['code' => 0]);exit;
  661. }
  662. /**
  663. * 回调
  664. */
  665. public function callback() {
  666. $input = file_get_contents("php://input"); // 主题信息
  667. $input = json_decode($input,true);
  668. $CallbackCommand = $input["CallbackCommand"];
  669. switch ($CallbackCommand) {
  670. case 'State.StateChange': // 用户在线状态变更
  671. $info = $input["Info"];
  672. if($info["Action"] == "Login") { // 用户登录
  673. $loginToday = Db::name('user')->where('id',$info["To_Account"])->whereTime('onlinetime', 'today')->find();
  674. if ($loginToday){
  675. \app\common\model\User::update(["is_online"=>1,"onlinetime"=>time()],["id"=>$info["To_Account"]]);
  676. }else{
  677. \app\common\model\User::update(["is_online"=>1,"onlinetime"=>time()],["id"=>$info["To_Account"]]);
  678. }
  679. $res = [];
  680. $res["ActionStatus"] = "OK";
  681. $res["ErrorCode"] = 0;
  682. $res["ErrorInfo"] = "";
  683. echo json_encode($res);exit;
  684. } elseif($info["Action"] == "Logout" || $info["Action"] == "Disconnect") { // 用户退出 不需要管什么原因
  685. // 更新用户在线状态为离线
  686. \app\common\model\User::update(["is_online"=>0],["id"=>$info["To_Account"]]);
  687. \app\common\model\User::update(["is_live"=>0],["id"=>$info["To_Account"]]);
  688. // 获取用户在派对直播间情况信息
  689. $redis = new Redis();
  690. $redisconfig = config("redis");
  691. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  692. if ($redisconfig['redis_pwd']) {
  693. $redis->auth($redisconfig['redis_pwd']);
  694. }
  695. if($redisconfig['redis_selectdb'] > 0){
  696. $redis->select($redisconfig['redis_selectdb']);
  697. }
  698. $livingUserPartyId = $redis->hGet("livingUser", $info["To_Account"]);
  699. if ($livingUserPartyId) {
  700. // 扣除在线用户在房间情况
  701. $redis->hDel("livingUser", $info["To_Account"]);
  702. $redis->HDel("online_" . $livingUserPartyId, $info["To_Account"]);
  703. $redis->zRem("party_user_".$livingUserPartyId,$info["To_Account"]); //新加的
  704. }
  705. // 获取用户所在群信息
  706. $groupids = $this->getJoindGroupList($info["To_Account"]);
  707. // \app\common\model\Test::update(["content"=>json_encode($groupids)],["id"=>1]);
  708. if($groupids["ErrorCode"] == 0) {
  709. $grouplist = $groupids["GroupIdList"];
  710. $groupsites = [];
  711. if($grouplist) foreach($grouplist as $k => $v) {
  712. // 获取群组消息
  713. $groupInfo = $this->getGroupInfo($v["GroupId"]);
  714. if($groupInfo["ErrorCode"] == 0) $groupsites[] = $groupInfo["GroupInfo"][0];
  715. // 踢出用户在线组
  716. $redis = new Redis();
  717. $redisconfig = config("redis");
  718. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  719. if ($redisconfig['redis_pwd']) {
  720. $redis->auth($redisconfig['redis_pwd']);
  721. }
  722. if($redisconfig['redis_selectdb'] > 0){
  723. $redis->select($redisconfig['redis_selectdb']);
  724. }
  725. $redis->HDel("online_".$v["GroupId"],$info["To_Account"]);
  726. $redis->zRem("party_user_".$v["GroupId"],$info["To_Account"]); //新加的
  727. // 更新麦位前四
  728. $this->updatePosition($info["To_Account"],$v["GroupId"]);
  729. // 取消排麦
  730. $this->cancelLineup($info["To_Account"],$v["GroupId"]);
  731. // 房间状态变更
  732. $partyInfo = $redis->get("party_".$v["GroupId"]);
  733. if($partyInfo) {
  734. $partyInfo = json_decode($partyInfo,true);
  735. $memCount = count($redis->hGetAll("online_".$v["GroupId"]));
  736. if($memCount <= 0) {
  737. $partyInfo["is_online"] = 0;
  738. $redis->set("party_".$v["GroupId"],json_encode($partyInfo));
  739. \app\common\model\Party::update(["is_online"=>0],["id"=>$v["GroupId"]]);
  740. }
  741. }
  742. $liveInfo = $redis->get("live_".$v["GroupId"]);
  743. if($liveInfo) {
  744. $liveInfo = json_decode($liveInfo,true);
  745. $memCount = count($redis->hGetAll("online_".$v["GroupId"]));
  746. if($memCount <= 0) {
  747. $liveInfo["is_online"] = 0;
  748. $redis->set("live_".$v["GroupId"],json_encode($liveInfo));
  749. \app\common\model\Party::update(["is_online"=>0],["id"=>$v["GroupId"]]);
  750. }
  751. }
  752. }
  753. // 强制下麦
  754. print_r($this->downSite($info["To_Account"],$groupsites));
  755. }
  756. }
  757. break;
  758. /*case 'Group.CallbackAfterNewMemberJoin': // 新成员入群
  759. $redis = new Redis();
  760. $redisconfig = config("redis");
  761. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  762. if ($redisconfig['redis_pwd']) {
  763. $redis->auth($redisconfig['redis_pwd']);
  764. }
  765. if($redisconfig['redis_selectdb'] > 0){
  766. $redis->select($redisconfig['redis_selectdb']);
  767. }
  768. $members = $input["NewMemberList"];
  769. if($members) foreach($members as $k => $v) {
  770. $redis->hSet("online_".$input["GroupId"],$v["Member_Account"],$v["Member_Account"]);
  771. // 更新房间在线状态
  772. $party_id = $input["GroupId"];
  773. $partyInfo = $redis->get("party_".$party_id);
  774. if($partyInfo) {
  775. $partyInfo = json_decode($partyInfo,true);
  776. if($partyInfo["is_online"] != 1) {
  777. $partyInfo["is_online"] = 1;
  778. $redis->set("party_".$party_id,json_encode($partyInfo));
  779. \app\common\model\Party::update(["is_online"=>1],["id"=>$party_id]);
  780. }
  781. }
  782. }
  783. $res = [];
  784. $res["ActionStatus"] = "OK";
  785. $res["ErrorCode"] = 0;
  786. $res["ErrorInfo"] = "";
  787. echo json_encode($res);
  788. break;*/
  789. /*case 'Group.CallbackAfterMemberExit': // 成员离开
  790. $redis = new Redis();
  791. $redisconfig = config("redis");
  792. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  793. if ($redisconfig['redis_pwd']) {
  794. $redis->auth($redisconfig['redis_pwd']);
  795. }
  796. if($redisconfig['redis_selectdb'] > 0){
  797. $redis->select($redisconfig['redis_selectdb']);
  798. }
  799. $members = $input["ExitMemberList"];
  800. if($members) foreach($members as $k => $v) {
  801. $redis->HDel("online_".$input["GroupId"],$v["Member_Account"]);
  802. }
  803. // 更新房间在线状态
  804. $party_id = $input["GroupId"];
  805. $partyInfo = $redis->get("party_".$party_id);
  806. if($partyInfo) {
  807. $partyInfo = json_decode($partyInfo,true);
  808. $memCount = count($redis->hGetAll("online_".$party_id));
  809. if($memCount <= 0) {
  810. $partyInfo["is_online"] = 0;
  811. $redis->set("party_".$party_id,json_encode($partyInfo));
  812. \app\common\model\Party::update(["is_online"=>0],["id"=>$party_id]);
  813. }
  814. }
  815. $liveInfo = $redis->get("live_".$party_id);
  816. if($liveInfo) {
  817. $liveInfo = json_decode($liveInfo,true);
  818. $memCount = count($redis->hGetAll("online_".$party_id));
  819. if($memCount <= 0) {
  820. $liveInfo["is_online"] = 0;
  821. $redis->set("live_".$party_id,json_encode($liveInfo));
  822. \app\common\model\Party::update(["is_online"=>0],["id"=>$party_id]);
  823. }
  824. }
  825. $res = [];
  826. $res["ActionStatus"] = "OK";
  827. $res["ErrorCode"] = 0;
  828. $res["ErrorInfo"] = "";
  829. echo json_encode($res);
  830. break;*/
  831. /* case 'Group.CallbackBeforeSendMsg': // 群内发送消息之前
  832. $config = config("wxMiniProgram");
  833. $access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$config["appid"]."&secret=".$config["secret"];
  834. $access_token_arr = $this->getJson($access_token_url);
  835. $access_token = isset($access_token_arr["access_token"])?$access_token_arr["access_token"]:"";
  836. if(!$access_token) {
  837. $res = [];
  838. $res["ActionStatus"] = "OK";
  839. $res["ErrorInfo"] = "";
  840. $res["ErrorCode"] = 0;
  841. echo json_encode($res);
  842. break;
  843. }
  844. // \app\common\model\Test::update(["content"=>json_encode($access_token)],["id"=>1]);
  845. $infoMessage = $input["MsgBody"];
  846. $badMessage = false;
  847. if($infoMessage) foreach($infoMessage as $k => $v) {
  848. if(isset($v["MsgContent"]["Text"])) {
  849. $message = json_decode($v["MsgContent"]["Text"],true);
  850. $content = isset($message["content"])?$message["content"]:"";
  851. $message_check_url ='https://api.weixin.qq.com/wxa/msg_sec_check?access_token='.$access_token;
  852. $tencentObj = new tencentim($message_check_url);
  853. $content_json = '{"content": "'.$content.'"}';
  854. $body = $tencentObj->messageCheck($content_json);
  855. if($body["errcode"] == 87014) {
  856. // \app\common\model\Test::update(["content"=>$body["errcode"]],["id"=>1]);
  857. // $res = [];
  858. // $res["ActionStatus"] = "OK";
  859. // $res["ErrorInfo"] = "检测到敏感词汇!";
  860. // $res["ErrorCode"] = 1;
  861. // echo json_encode($res);exit;
  862. $message["content"] = "*******";
  863. $infoMessage[$k]["MsgContent"]["Text"] = json_encode($message);
  864. $badMessage = true;
  865. }
  866. }
  867. }
  868. if($badMessage) {
  869. $res = [];
  870. $res["ActionStatus"] = "OK";
  871. $res["ErrorInfo"] = "检测到敏感词汇!";
  872. $res["ErrorCode"] = 10102;
  873. $res["MsgBody"] = $infoMessage;
  874. } else {
  875. $res = [];
  876. $res["ActionStatus"] = "OK";
  877. $res["ErrorInfo"] = "";
  878. $res["ErrorCode"] = 0;
  879. }
  880. // \app\common\model\Test::update(["content"=>json_encode($res)],["id"=>1]);
  881. echo json_encode($res);exit;
  882. break;*/
  883. default:
  884. break;
  885. }
  886. exit;
  887. }
  888. private function getJson($url){
  889. $ch = curl_init();
  890. curl_setopt($ch, CURLOPT_URL, $url);
  891. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  892. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  893. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  894. $output = curl_exec($ch);
  895. curl_close($ch);
  896. return json_decode($output, true);
  897. }
  898. /**
  899. * 发送消息给某人
  900. */
  901. public function sendMessageToUser($from_user,$to_user,$message) {
  902. // $from_user = 54;
  903. // $to_user = 6;
  904. // $message = "sdsd";
  905. $random = rand(10000000,99999999);
  906. $usersig = $this->usersig("administrator");
  907. // 获取配置信息
  908. $config = config("tencent_im");
  909. $url = "https://console.tim.qq.com/v4/openim/sendmsg";
  910. $url .= "?sdkappid=".$config["sdkappid"];
  911. $url .= "&identifier=administrator";
  912. $url .= "&usersig=".$usersig;
  913. $url .= "&random=".$random;
  914. $url .= "&contenttype=json";
  915. $tencentObj = new tencentim($url);
  916. $data = [];
  917. $data["SyncOtherMachine"] = 1;
  918. $data["From_Account"] = (string)$from_user;
  919. $data["To_Account"] = (string)$to_user;
  920. $data["MsgRandom"] = rand(1000000,9999999);
  921. $data["MsgTimeStamp"] = time();
  922. $data["MsgBody"][] = [
  923. "MsgType" => "TIMTextElem",
  924. "MsgContent" => [
  925. "Text"=> $message
  926. ],
  927. ];
  928. $tencentObj->toSend($data);
  929. }
  930. /**
  931. * 发送消息给某人-接口调用
  932. */
  933. public function sendToUser() {
  934. $from_user = $this->request->request("from_user");// 发送者
  935. $to_user = $this->request->request("to_user");// 接收者
  936. $message = $this->request->request("message");// 接收者
  937. if(!$from_user || !$to_user || !$message) $this->error("参数缺失!");
  938. $this->sendMessageToUser($from_user,$to_user,$message);
  939. }
  940. //请求的基础参数
  941. private function baseTencentObj($url)
  942. {
  943. $random = rand(10000000,99999999);
  944. $usersig = $this->usersig("administrator");
  945. // 获取配置信息
  946. $config = config("tencent_im");
  947. $url = "$url";
  948. $url .= "?sdkappid=".$config["sdkappid"];
  949. $url .= "&identifier=administrator";
  950. $url .= "&usersig=".$usersig;
  951. $url .= "&random=".$random;
  952. $url .= "&contenttype=json";
  953. return new tencentim($url);
  954. }
  955. /** 设置管理员(im端的改变)
  956. * @param $user_id
  957. * @param $party_id
  958. * @param int $status 1:yes 0:no
  959. * @return array|false|mixed
  960. */
  961. public function setUpIMAdmin($user_id, $party_id, int $status=0) {
  962. $url = "https://console.tim.qq.com/v4/group_open_http_svc/modify_group_member_info";
  963. $tencentObj = $this->baseTencentObj($url);
  964. $groupInfo = $this->getGroupInfo($party_id);
  965. if($groupInfo["GroupInfo"][0]["ErrorCode"] > 0) return false;
  966. // 更新的信息
  967. $data = [];
  968. $data["GroupId"] = $party_id;
  969. if ($status==1){
  970. $data['Role'] = 'Admin';
  971. }else{
  972. $data['Role'] = 'Member';
  973. }
  974. $data['Member_Account'] = $user_id;
  975. return $tencentObj->toSend($data);
  976. }
  977. //获取群成员详细资料(目前没用到)
  978. private function getGroupMemberDetails($party_id)
  979. {
  980. $url = "https://console.tim.qq.com/v4/group_open_http_svc/get_group_member_info";
  981. $tencentObj = $this->baseTencentObj($url);
  982. // 获取群组信息
  983. $groupInfo = $this->getGroupInfo($party_id);
  984. if($groupInfo["GroupInfo"][0]["ErrorCode"] > 0) return false;
  985. // 更新的信息
  986. $data = [];
  987. $data["GroupId"] = $party_id;
  988. return $tencentObj->toSend($data);
  989. }
  990. //IM管理员维护,同步本地管理员
  991. public function imAdminMaintenance()
  992. {
  993. $partyIds = \app\admin\model\party\Party::column('id');
  994. foreach ($partyIds as $party_id){
  995. $groupInfo = $this->getGroupInfo($party_id);
  996. if($groupInfo["GroupInfo"][0]["ErrorCode"] > 0) continue;
  997. $roomInfoData = $groupInfo["GroupInfo"][0]["AppDefinedData"];
  998. $roomInfo = [];
  999. if($roomInfoData) foreach($roomInfoData as $k => $v) {
  1000. if($v["Key"] == "roomInfo") $roomInfo = $v["Value"];
  1001. }
  1002. $roomInfo = json_decode($roomInfo,true);
  1003. // 更新管理员
  1004. if(isset($roomInfo["managers"]) && !empty($roomInfo['managers'])) {
  1005. foreach ($roomInfo['managers'] as $v){
  1006. $this->setUpIMAdmin($v,$party_id,1);
  1007. }
  1008. }
  1009. }
  1010. $this->success("成功!");
  1011. }
  1012. /**
  1013. * 获取用户在线更新
  1014. */
  1015. public function autoUserLine() {
  1016. $random = rand(10000000,99999999);
  1017. $usersig = $this->usersig("administrator");
  1018. // 获取配置信息
  1019. $config = config("tencent_im");
  1020. $url = "https://console.tim.qq.com/v4/openim/query_online_status";
  1021. $url .= "?sdkappid=".$config["sdkappid"];
  1022. $url .= "&identifier=administrator";
  1023. $url .= "&usersig=".$usersig;
  1024. $url .= "&random=".$random;
  1025. $url .= "&contenttype=json";
  1026. $tencentObj = new tencentim($url);
  1027. // 获取用户
  1028. $where = [];
  1029. $userData = model('User')->field('id')->where($where)->order('id asc')->select();
  1030. $userArr = array_column($userData,'id');
  1031. $data = [];
  1032. $data["To_Account"] = $userArr;
  1033. $result = [];
  1034. if(!empty($data)) {
  1035. $result = $tencentObj->toSend($data);
  1036. if (isset($result['ActionStatus']) && $result['ActionStatus'] == 'OK') {
  1037. if (isset($result['ErrorList'])) {//没有人员
  1038. unset($result['ErrorList']);
  1039. /*foreach ($result['ErrorList'] as $errKey => $errVal) {
  1040. $userError[] = $errVal['To_Account'];
  1041. }*/
  1042. }
  1043. if (isset($result['QueryResult'])) {
  1044. $userOnline = $userOffline=[];
  1045. foreach ($result['QueryResult'] as $key => $value) {
  1046. if (in_array($value['Status'],['Online','PushOnline'])) {//在线(前台在线和后台在线)
  1047. $userOnline[] = intval($value['To_Account']);
  1048. } else {//离线
  1049. $userOffline[] = intval($value['To_Account']);
  1050. }
  1051. }
  1052. if (!empty($userOnline)) {//处理在线
  1053. $onlineWhere['id'] = ['in',$userOnline];
  1054. $onlineWhere['is_online'] = 0;
  1055. model('User')->where($onlineWhere)->update(['is_online'=>1]);
  1056. }
  1057. if (!empty($userOffline)) {//处理离线
  1058. $offlineWhere['id'] = ['in',$userOffline];
  1059. $offlineWhere['is_online'] = 1;
  1060. model('User')->where($offlineWhere)->update(['is_online'=>0]);
  1061. }
  1062. }
  1063. }
  1064. }
  1065. $this->success('操作成功');
  1066. }
  1067. /**
  1068. * 获取房间用户
  1069. */
  1070. public function getRoomUser() {
  1071. $partyIds = $this->request->param('party_ids','');
  1072. if (!empty($partyIds)) {
  1073. $partyIds = explode(',',$partyIds);
  1074. }
  1075. $random = rand(10000000,99999999);
  1076. $usersig = $this->usersig("administrator");
  1077. // 获取配置信息
  1078. $config = config("tencent_im");
  1079. $url = "https://console.tim.qq.com/v4/group_open_avchatroom_http_svc/get_members";
  1080. $url .= "?sdkappid=".$config["sdkappid"];
  1081. $url .= "&identifier=administrator";
  1082. $url .= "&usersig=".$usersig;
  1083. $url .= "&random=".$random;
  1084. $url .= "&contenttype=json";
  1085. $tencentObj = new tencentim($url);
  1086. //获取用户 ["57","62","63","105","106","107","108","109","116","119"]
  1087. $partyArr = $partyIds;
  1088. $memberList = [];
  1089. if(!empty($partyArr)) {
  1090. foreach ($partyArr as $key => $value) {
  1091. $data["GroupId"] = $value;
  1092. $result = $tencentObj->toSend($data);
  1093. if (isset($result['ActionStatus']) && $result['ActionStatus'] == 'OK') {
  1094. if (isset($result['MemberList']) && !empty($result['MemberList'])) {
  1095. foreach ($result['MemberList'] as $ukey => $uval) {
  1096. $memberList[$value]['member_list'][] = [
  1097. 'avatar' => $uval['Avatar'],
  1098. 'user_id'=> $uval['Member_Account'],
  1099. 'nickname' => $uval['NickName'],
  1100. ];
  1101. }
  1102. $memberList[$value]['online_num'] = count($memberList[$value]['member_list']);
  1103. }
  1104. }
  1105. }
  1106. }
  1107. $result = $memberList;
  1108. $this->success('操作成功',$result);
  1109. }
  1110. /**
  1111. * 获取房间用户
  1112. */
  1113. public function getGroupAttr() {
  1114. $partyId = $this->request->param('party_id','');
  1115. if (empty($partyIds)) {
  1116. $this->error('参数错误');
  1117. }
  1118. $random = rand(10000000,99999999);
  1119. $usersig = $this->usersig("administrator");
  1120. // 获取配置信息
  1121. $config = config("tencent_im");
  1122. $url = "https://console.tim.qq.com/v4/group_open_attr_http_svc/get_group_attr";
  1123. $url .= "?sdkappid=".$config["sdkappid"];
  1124. $url .= "&identifier=administrator";
  1125. $url .= "&usersig=".$usersig;
  1126. $url .= "&random=".$random;
  1127. $url .= "&contenttype=json";
  1128. $tencentObj = new tencentim($url);
  1129. $result = [];
  1130. if(!empty($partyId)) {
  1131. $data["GroupId"] = $partyId;
  1132. $result = $tencentObj->toSend($data);
  1133. }
  1134. $this->success('操作成功',$result);
  1135. }
  1136. /**
  1137. * 获取黑名单列表
  1138. */
  1139. public function getBlackList() {
  1140. $userId = $this->request->param('user_id',0);
  1141. if (empty($userId)) {
  1142. $this->error('参数错误');
  1143. }
  1144. $tenimService = new TenimService();
  1145. $params = [
  1146. 'user_id' => $userId,
  1147. 'page' => 0,
  1148. 'limit' => 30,
  1149. ];
  1150. $imResult = $tenimService->getBlackList($params);
  1151. if (!$imResult['status']) {
  1152. $this->error($imResult['msg']);
  1153. }
  1154. $this->success('操作成功',$imResult['data']);
  1155. }
  1156. }