Tenim.php 36 KB

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