Tenim.php 51 KB

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