Tenim.php 39 KB

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