Tenim.php 34 KB

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