Tenim.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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) {
  235. // $manage_user_id = "2";
  236. // $party_id = "6";
  237. $random = rand(10000000,99999999);
  238. $usersig = $this->usersig("administrator");
  239. // 获取配置信息
  240. $config = config("tencent_im");
  241. $url = "https://console.tim.qq.com/v4/group_open_http_svc/modify_group_base_info";
  242. $url .= "?sdkappid=".$config["sdkappid"];
  243. $url .= "&identifier=administrator";
  244. $url .= "&usersig=".$usersig;
  245. $url .= "&random=".$random;
  246. $url .= "&contenttype=json";
  247. $tencentObj = new tencentim($url);
  248. // 获取群组信息
  249. $groupInfo = $this->getGroupInfo($party_id);
  250. if($groupInfo["GroupInfo"][0]["ErrorCode"] > 0) return false;
  251. $roomInfoData = $groupInfo["GroupInfo"][0]["AppDefinedData"];
  252. $roomInfo = [];
  253. if($roomInfoData) foreach($roomInfoData as $k => $v) {
  254. if($v["Key"] == "roomInfo") $roomInfo = $v["Value"];
  255. }
  256. $roomInfo = json_decode($roomInfo,true);
  257. // 更新的信息
  258. if(isset($roomInfo["managers"])) {
  259. if(!$roomInfo["managers"]) $roomInfo["managers"] = [];
  260. array_push($roomInfo["managers"],$manage_user_id);
  261. $roomInfo["managers"] = array_unique($roomInfo["managers"]);
  262. }
  263. $data = [];
  264. $data["GroupId"] = $party_id;
  265. $data["AppDefinedData"][0] = [
  266. "Key" => "roomInfo",
  267. "Value" => json_encode($roomInfo),
  268. // "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)),
  269. ];
  270. if(isset($roomInfo["ownerId"]) && isset($roomInfo["roomName"])) {
  271. $tencentObj->toSend($data);
  272. }
  273. }
  274. /**
  275. * 强制下线
  276. */
  277. public function downSite($user_id,$groupsites) {
  278. $random = rand(10000000,99999999);
  279. $usersig = $this->usersig("administrator");
  280. // 获取配置信息
  281. $config = config("tencent_im");
  282. $url = "https://console.tim.qq.com/v4/group_open_http_svc/modify_group_base_info";
  283. $url .= "?sdkappid=".$config["sdkappid"];
  284. $url .= "&identifier=administrator";
  285. $url .= "&usersig=".$usersig;
  286. $url .= "&random=".$random;
  287. $url .= "&contenttype=json";
  288. $tencentObj = new tencentim($url);
  289. // 先更新下麦时间
  290. $update = [];
  291. $update["offsite_time"] = time();
  292. $update["status"] = 2;
  293. \app\common\model\UserOnsiteTime::update($update,["user_id"=>$user_id,"status"=>1]);
  294. // 循环房间
  295. if($groupsites) foreach($groupsites as $k => $v) {
  296. // 循环座位
  297. $data = [];
  298. foreach($v["AppDefinedData"] as $m => $n) {
  299. // 解析字段信息
  300. $siteInfo = json_decode($n["Value"],true);
  301. if($m != 0 && isset($siteInfo["status"]) && $siteInfo["status"] == 1 && isset($siteInfo["status"]) && $siteInfo["user"] == $user_id) {
  302. $data["AppDefinedData"][] = [
  303. "Key" => $n["Key"],
  304. "Value" => '{"mute":false,"status":0,"user":""}',
  305. ];
  306. }
  307. }
  308. if(!empty($data)) {
  309. $data["GroupId"] = $v["GroupId"];
  310. return $tencentObj->toSend($data);
  311. }
  312. }
  313. }
  314. /**
  315. * 踢出房间内所有用户
  316. */
  317. public function outMemberFromRoom($party_id) {
  318. if($party_id <= 0) return false;
  319. $message = [];
  320. $message["type"] = "91";
  321. $message["content"] = "房间已被管理员关闭!详情请联系客服!";
  322. $msgData = [];
  323. $msgData["version"] = "1.0";
  324. $msgData["action"] = 301;
  325. $msgData["command"] = "";
  326. $msgData["message"] = json_encode($message);
  327. $random = rand(10000000,99999999);
  328. $usersig = $this->usersig("administrator");
  329. // 获取配置信息
  330. $config = config("tencent_im");
  331. $url = "https://console.tim.qq.com/v4/group_open_http_svc/send_group_msg";
  332. $url .= "?sdkappid=".$config["sdkappid"];
  333. $url .= "&identifier=administrator";
  334. $url .= "&usersig=".$usersig;
  335. $url .= "&random=".$random;
  336. $url .= "&contenttype=json";
  337. $tencentObj = new tencentim($url);
  338. $data = [];
  339. $data["GroupId"] = $party_id;
  340. $data["Random"] = rand(1000000,9999999);
  341. $data["MsgBody"][] = [
  342. "MsgType" => "TIMCustomElem",
  343. "MsgContent" => [
  344. "Data"=> json_encode($msgData)
  345. ],
  346. ];
  347. $tencentObj->toSend($data);
  348. }
  349. /**
  350. * 获取usersig签名
  351. */
  352. public function getUsersig() {
  353. $user_id = $this->auth->id;
  354. $this->success("获取成功!",$this->usersig($user_id));
  355. }
  356. /**
  357. * 获取usersig签名-具体操作
  358. */
  359. private function usersig($user_id) {
  360. // 获取配置信息
  361. $config = config("tencent_im");
  362. $usersigObj = new getusersig($config["sdkappid"],$config["key"]);
  363. $usersig = $usersigObj->genUserSig($user_id);
  364. return $usersig;
  365. }
  366. /**
  367. * 强制下麦
  368. */
  369. public function updateSiteInfo() {
  370. $user_id = $this->request->request("user_id");// 用户ID
  371. if(!$user_id) $this->error("参数缺失!");
  372. // 更新用户在线状态为离线
  373. \app\common\model\User::update(["is_online"=>0],["id"=>$user_id]);
  374. \app\common\model\User::update(["is_live"=>0],["id"=>$user_id]);
  375. \app\common\model\Party::update(["is_online"=>0],["user_id"=>$user_id]);
  376. // 获取用户所在群信息
  377. $groupids = $this->getJoindGroupList($user_id);
  378. if($groupids["ErrorCode"] == 0) {
  379. $grouplist = $groupids["GroupIdList"];
  380. $groupsites = [];
  381. if($grouplist) foreach($grouplist as $k => $v) {
  382. // 获取群组消息
  383. $groupInfo = $this->getGroupInfo($v["GroupId"]);
  384. if($groupInfo["ErrorCode"] == 0) $groupsites[] = $groupInfo["GroupInfo"][0];
  385. // 踢出用户在线组
  386. $redis = new Redis();
  387. $redisconfig = config("redis");
  388. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  389. if ($redisconfig['redis_pwd']) {
  390. $redis->auth(config('redis_pwd'));
  391. }
  392. if($redisconfig['redis_selectdb'] > 0){
  393. $redis->select(config('redis_selectdb'));
  394. }
  395. $redis->HDel("online_".$v["GroupId"],$user_id);
  396. }
  397. // 强制下麦
  398. $this->downSite($user_id,$groupsites);
  399. }
  400. $this->success("状态更新成功!");
  401. }
  402. /**
  403. * 更新麦位前四
  404. */
  405. public function updatePosition($user_id,$party_id) {
  406. // 获取用户头像
  407. $userInfo = \app\common\model\User::field("avatar")->where(["id"=>$user_id])->find();
  408. $userAvatar = isset($userInfo["avatar"])?$userInfo["avatar"]:"";
  409. $redis = new Redis();
  410. $redisconfig = config("redis");
  411. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  412. if ($redisconfig['redis_pwd']) {
  413. $redis->auth(config('redis_pwd'));
  414. }
  415. if($redisconfig['redis_selectdb'] > 0){
  416. $redis->select(config('redis_selectdb'));
  417. }
  418. $redisData = $redis->get("party_".$party_id);
  419. if(!$redisData) {
  420. return true;
  421. }
  422. $partyInfo = json_decode($redisData,true);
  423. // 遍历已有头像
  424. $partyuser = isset($partyInfo["party_user"])?$partyInfo["party_user"]:"";
  425. if(is_array($partyuser)) foreach($partyuser as $k => $v) if($v === $userAvatar) unset($partyInfo["party_user"][$k]);
  426. $redis->set("party_".$party_id,json_encode($partyInfo));
  427. }
  428. /**
  429. * 取消排麦
  430. */
  431. public function cancelLineUp($user_id,$party_id) {
  432. if (!$party_id || !$user_id) {
  433. return false;
  434. }
  435. $redis = new Redis();
  436. $redisconfig = config("redis");
  437. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  438. if ($redisconfig['redis_pwd']) {
  439. $redis->auth(config('redis_pwd'));
  440. }
  441. if($redisconfig['redis_selectdb'] > 0){
  442. $redis->select(config('redis_selectdb'));
  443. }
  444. $data=unserialize($redis->hGet("party_lineup",$party_id));
  445. // 更改红点排麦数量
  446. $num = 0;
  447. $send = false;
  448. if($data) foreach($data as $k => $v) {
  449. if($v["user_id"] == $user_id) {
  450. $send = true;
  451. unset($data[$k]);
  452. break;
  453. }
  454. }
  455. $redis->hSet("party_lineup",$party_id,serialize($data));
  456. if($send) {
  457. is_array($data) && $num = count($data);
  458. $random = rand(10000000,99999999);
  459. $usersig = $this->usersig("administrator");
  460. // 获取配置信息
  461. $config = config("tencent_im");
  462. $url = "https://console.tim.qq.com/v4/group_open_http_svc/send_group_msg";
  463. $url .= "?sdkappid=".$config["sdkappid"];
  464. $url .= "&identifier=administrator";
  465. $url .= "&usersig=".$usersig;
  466. $url .= "&random=".$random;
  467. $url .= "&contenttype=json";
  468. $tencentObj = new tencentim($url);
  469. $data = [];
  470. $data["GroupId"] = $party_id;
  471. $data["Random"] = rand(1000000,9999999);
  472. $message = [
  473. "message" => json_encode(["type"=>23,"content"=>$num]),
  474. "version" => "1.0",
  475. "action" => 301,
  476. "command" => ""
  477. ];
  478. $data["MsgBody"][] = [
  479. "MsgType" => "TIMCustomElem",
  480. "MsgContent" => [
  481. "Data"=> json_encode($message)
  482. ],
  483. ];
  484. $tencentObj->toSend($data);
  485. }
  486. }
  487. /**
  488. * 回调
  489. */
  490. public function callback() {
  491. $input = file_get_contents("php://input"); // 主题信息
  492. $input = json_decode($input,true);
  493. $CallbackCommand = $input["CallbackCommand"];
  494. switch ($CallbackCommand) {
  495. case 'State.StateChange': // 用户在线状态变更
  496. $info = $input["Info"];
  497. if($info["Action"] == "Login") { // 用户登录
  498. \app\common\model\User::update(["is_online"=>1,"onlinetime"=>time()],["id"=>$info["To_Account"]]);
  499. $res = [];
  500. $res["ActionStatus"] = "OK";
  501. $res["ErrorCode"] = 0;
  502. $res["ErrorInfo"] = "";
  503. echo json_encode($res);exit;
  504. } elseif($info["Action"] == "Logout" || $info["Action"] == "Disconnect") { // 用户退出 不需要管什么原因
  505. // 更新用户在线状态为离线
  506. \app\common\model\User::update(["is_online"=>0],["id"=>$info["To_Account"]]);
  507. \app\common\model\User::update(["is_live"=>0],["id"=>$info["To_Account"]]);
  508. // 获取用户在派对直播间情况信息
  509. $redis = new Redis();
  510. $redisconfig = config("redis");
  511. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  512. if ($redisconfig['redis_pwd']) {
  513. $redis->auth(config('redis_pwd'));
  514. }
  515. if($redisconfig['redis_selectdb'] > 0){
  516. $redis->select(config('redis_selectdb'));
  517. }
  518. $livingUserPartyId = $redis->hGet("livingUser", $info["To_Account"]);
  519. if ($livingUserPartyId) {
  520. // 扣除在线用户在房间情况
  521. $redis->hDel("livingUser", $info["To_Account"]);
  522. $redis->HDel("online_" . $livingUserPartyId, $info["To_Account"]);
  523. }
  524. // 获取用户所在群信息
  525. $groupids = $this->getJoindGroupList($info["To_Account"]);
  526. // \app\common\model\Test::update(["content"=>json_encode($groupids)],["id"=>1]);
  527. if($groupids["ErrorCode"] == 0) {
  528. $grouplist = $groupids["GroupIdList"];
  529. $groupsites = [];
  530. if($grouplist) foreach($grouplist as $k => $v) {
  531. // 获取群组消息
  532. $groupInfo = $this->getGroupInfo($v["GroupId"]);
  533. if($groupInfo["ErrorCode"] == 0) $groupsites[] = $groupInfo["GroupInfo"][0];
  534. // 踢出用户在线组
  535. $redis = new Redis();
  536. $redisconfig = config("redis");
  537. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  538. if ($redisconfig['redis_pwd']) {
  539. $redis->auth(config('redis_pwd'));
  540. }
  541. if($redisconfig['redis_selectdb'] > 0){
  542. $redis->select(config('redis_selectdb'));
  543. }
  544. $redis->HDel("online_".$v["GroupId"],$info["To_Account"]);
  545. // 更新麦位前四
  546. $this->updatePosition($info["To_Account"],$v["GroupId"]);
  547. // 取消排麦
  548. $this->cancelLineup($info["To_Account"],$v["GroupId"]);
  549. // 房间状态变更
  550. $partyInfo = $redis->get("party_".$v["GroupId"]);
  551. if($partyInfo) {
  552. $partyInfo = json_decode($partyInfo,true);
  553. $memCount = count($redis->hGetAll("online_".$v["GroupId"]));
  554. if($memCount <= 0) {
  555. $partyInfo["is_online"] = 0;
  556. $redis->set("party_".$v["GroupId"],json_encode($partyInfo));
  557. \app\common\model\Party::update(["is_online"=>0],["id"=>$v["GroupId"]]);
  558. }
  559. }
  560. $liveInfo = $redis->get("live_".$v["GroupId"]);
  561. if($liveInfo) {
  562. $liveInfo = json_decode($liveInfo,true);
  563. $memCount = count($redis->hGetAll("online_".$v["GroupId"]));
  564. if($memCount <= 0) {
  565. $liveInfo["is_online"] = 0;
  566. $redis->set("live_".$v["GroupId"],json_encode($liveInfo));
  567. \app\common\model\Party::update(["is_online"=>0],["id"=>$v["GroupId"]]);
  568. }
  569. }
  570. }
  571. // 强制下麦
  572. print_r($this->downSite($info["To_Account"],$groupsites));
  573. }
  574. }
  575. break;
  576. // case 'Group.CallbackAfterNewMemberJoin': // 新成员入群
  577. // $redis = new Redis();
  578. // $redisconfig = config("redis");
  579. // $redis->connect($redisconfig["host"], $redisconfig["port"]);
  580. /*if ($redisconfig['redis_pwd']) {
  581. $redis->auth(config('redis_pwd'));
  582. }
  583. if($redisconfig['redis_selectdb'] > 0){
  584. $redis->select(config('redis_selectdb'));
  585. }*/
  586. // $members = $input["NewMemberList"];
  587. // if($members) foreach($members as $k => $v) {
  588. // $redis->hSet("online_".$input["GroupId"],$v["Member_Account"],$v["Member_Account"]);
  589. // // 更新房间在线状态
  590. // $party_id = $input["GroupId"];
  591. // $partyInfo = $redis->get("party_".$party_id);
  592. // if($partyInfo) {
  593. // $partyInfo = json_decode($partyInfo,true);
  594. // if($partyInfo["is_online"] != 1) {
  595. // $partyInfo["is_online"] = 1;
  596. // $redis->set("party_".$party_id,json_encode($partyInfo));
  597. // \app\common\model\Party::update(["is_online"=>1],["id"=>$party_id]);
  598. // }
  599. // }
  600. // }
  601. // $res = [];
  602. // $res["ActionStatus"] = "OK";
  603. // $res["ErrorCode"] = 0;
  604. // $res["ErrorInfo"] = "";
  605. // echo json_encode($res);
  606. // break;
  607. // case 'Group.CallbackAfterMemberExit': // 成员离开
  608. // $redis = new Redis();
  609. // $redisconfig = config("redis");
  610. // $redis->connect($redisconfig["host"], $redisconfig["port"]);
  611. /*if ($redisconfig['redis_pwd']) {
  612. $redis->auth(config('redis_pwd'));
  613. }
  614. if($redisconfig['redis_selectdb'] > 0){
  615. $redis->select(config('redis_selectdb'));
  616. }*/
  617. // $members = $input["ExitMemberList"];
  618. // if($members) foreach($members as $k => $v) {
  619. // $redis->HDel("online_".$input["GroupId"],$v["Member_Account"]);
  620. // }
  621. // // 更新房间在线状态
  622. // $party_id = $input["GroupId"];
  623. // $partyInfo = $redis->get("party_".$party_id);
  624. // if($partyInfo) {
  625. // $partyInfo = json_decode($partyInfo,true);
  626. // $memCount = count($redis->hGetAll("online_".$party_id));
  627. // if($memCount <= 0) {
  628. // $partyInfo["is_online"] = 0;
  629. // $redis->set("party_".$party_id,json_encode($partyInfo));
  630. // \app\common\model\Party::update(["is_online"=>0],["id"=>$party_id]);
  631. // }
  632. // }
  633. //
  634. // $liveInfo = $redis->get("live_".$party_id);
  635. // if($liveInfo) {
  636. // $liveInfo = json_decode($liveInfo,true);
  637. // $memCount = count($redis->hGetAll("online_".$party_id));
  638. // if($memCount <= 0) {
  639. // $liveInfo["is_online"] = 0;
  640. // $redis->set("live_".$party_id,json_encode($liveInfo));
  641. // \app\common\model\Party::update(["is_online"=>0],["id"=>$party_id]);
  642. // }
  643. // }
  644. //
  645. // $res = [];
  646. // $res["ActionStatus"] = "OK";
  647. // $res["ErrorCode"] = 0;
  648. // $res["ErrorInfo"] = "";
  649. // echo json_encode($res);
  650. // break;
  651. case 'Group.CallbackBeforeSendMsg': // 群内发送消息之前
  652. $config = config("wxMiniProgram");
  653. $access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$config["appid"]."&secret=".$config["secret"];
  654. $access_token_arr = $this->getJson($access_token_url);
  655. $access_token = isset($access_token_arr["access_token"])?$access_token_arr["access_token"]:"";
  656. if(!$access_token) {
  657. $res = [];
  658. $res["ActionStatus"] = "OK";
  659. $res["ErrorInfo"] = "";
  660. $res["ErrorCode"] = 0;
  661. echo json_encode($res);
  662. break;
  663. }
  664. // \app\common\model\Test::update(["content"=>json_encode($access_token)],["id"=>1]);
  665. $infoMessage = $input["MsgBody"];
  666. $badMessage = false;
  667. if($infoMessage) foreach($infoMessage as $k => $v) {
  668. if(isset($v["MsgContent"]["Text"])) {
  669. $message = json_decode($v["MsgContent"]["Text"],true);
  670. $content = isset($message["content"])?$message["content"]:"";
  671. $message_check_url ='https://api.weixin.qq.com/wxa/msg_sec_check?access_token='.$access_token;
  672. $tencentObj = new tencentim($message_check_url);
  673. $content_json = '{"content": "'.$content.'"}';
  674. $body = $tencentObj->messageCheck($content_json);
  675. if($body["errcode"] == 87014) {
  676. // \app\common\model\Test::update(["content"=>$body["errcode"]],["id"=>1]);
  677. // $res = [];
  678. // $res["ActionStatus"] = "OK";
  679. // $res["ErrorInfo"] = "检测到敏感词汇!";
  680. // $res["ErrorCode"] = 1;
  681. // echo json_encode($res);exit;
  682. $message["content"] = "*******";
  683. $infoMessage[$k]["MsgContent"]["Text"] = json_encode($message);
  684. $badMessage = true;
  685. }
  686. }
  687. }
  688. if($badMessage) {
  689. $res = [];
  690. $res["ActionStatus"] = "OK";
  691. $res["ErrorInfo"] = "检测到敏感词汇!";
  692. $res["ErrorCode"] = 10102;
  693. $res["MsgBody"] = $infoMessage;
  694. } else {
  695. $res = [];
  696. $res["ActionStatus"] = "OK";
  697. $res["ErrorInfo"] = "";
  698. $res["ErrorCode"] = 0;
  699. }
  700. // \app\common\model\Test::update(["content"=>json_encode($res)],["id"=>1]);
  701. echo json_encode($res);exit;
  702. break;
  703. default:
  704. break;
  705. }
  706. exit;
  707. }
  708. private function getJson($url){
  709. $ch = curl_init();
  710. curl_setopt($ch, CURLOPT_URL, $url);
  711. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  712. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  713. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  714. $output = curl_exec($ch);
  715. curl_close($ch);
  716. return json_decode($output, true);
  717. }
  718. /**
  719. * 发送消息给某人
  720. */
  721. public function sendMessageToUser($from_user,$to_user,$message) {
  722. // $from_user = 54;
  723. // $to_user = 6;
  724. // $message = "sdsd";
  725. $random = rand(10000000,99999999);
  726. $usersig = $this->usersig("administrator");
  727. // 获取配置信息
  728. $config = config("tencent_im");
  729. $url = "https://console.tim.qq.com/v4/openim/sendmsg";
  730. $url .= "?sdkappid=".$config["sdkappid"];
  731. $url .= "&identifier=administrator";
  732. $url .= "&usersig=".$usersig;
  733. $url .= "&random=".$random;
  734. $url .= "&contenttype=json";
  735. $tencentObj = new tencentim($url);
  736. $data = [];
  737. $data["SyncOtherMachine"] = 1;
  738. $data["From_Account"] = (string)$from_user;
  739. $data["To_Account"] = (string)$to_user;
  740. $data["MsgRandom"] = rand(1000000,9999999);
  741. $data["MsgTimeStamp"] = time();
  742. $data["MsgBody"][] = [
  743. "MsgType" => "TIMTextElem",
  744. "MsgContent" => [
  745. "Text"=> $message
  746. ],
  747. ];
  748. $tencentObj->toSend($data);
  749. }
  750. /**
  751. * 发送消息给某人-接口调用
  752. */
  753. public function sendToUser() {
  754. $from_user = $this->request->request("from_user");// 发送者
  755. $to_user = $this->request->request("to_user");// 接收者
  756. $message = $this->request->request("message");// 接收者
  757. if(!$from_user || !$to_user || !$message) $this->error("参数缺失!");
  758. $this->sendMessageToUser($from_user,$to_user,$message);
  759. }
  760. }