Tenim.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\utils\RedisUtil;
  5. use think\Request;
  6. use getusersig\getusersig;
  7. use tencentim\tencentim;
  8. use think\Db;
  9. /**
  10. * 腾讯im接口
  11. */
  12. class Tenim extends Api
  13. {
  14. protected $noNeedLogin = ['trtc_callback','callback','test','createIMGroup','updateRoomInfo','setImManage','sendMessageToUser','outMemberFromRoom','selectstatus','loginoutim', 'tencentcall'];
  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. //trtc_callback,直播callback
  417. //https://cloud.tencent.com/document/product/647/51586#eventId
  418. /*
  419. 2021-12-24 15:34:22 start2:{
  420. "EventGroupId": 1,
  421. "EventType": 103,
  422. "CallbackTs": 1640331262241,
  423. "EventInfo": {
  424. "RoomId": 1604,
  425. "EventTs": 1640331262,
  426. "EventMsTs": 1640331262238,
  427. "UserId": "26",
  428. "Role": 20,
  429. "TerminalType": 2,
  430. "UserType": 3,
  431. "Reason": 1
  432. }
  433. }
  434. 2021-12-24 15:34:22 start2:{
  435. "EventGroupId": 1,
  436. "EventType": 101,
  437. "CallbackTs": 1640331262247,
  438. "EventInfo": {
  439. "RoomId": 1604,
  440. "EventTs": 1640331262,
  441. "EventMsTs": 1640331262238,
  442. "UserId": "26"
  443. }
  444. }
  445. 2021-12-24 15:34:55 start2:{
  446. "EventGroupId": 1,
  447. "EventType": 102,
  448. "CallbackTs": 1640331295244,
  449. "EventInfo": {
  450. "RoomId": 1604,
  451. "EventTs": 1640331295,
  452. "EventMsTs": 1640331295239
  453. }
  454. }
  455. 2021-12-24 15:34:55 start2:{
  456. "EventGroupId": 1,
  457. "EventType": 104,
  458. "CallbackTs": 1640331295244,
  459. "EventInfo": {
  460. "RoomId": 1604,
  461. "EventTs": 1640331295,
  462. "EventMsTs": 1640331295239,
  463. "UserId": "26",
  464. "Role": 20,
  465. "Reason": 1
  466. }
  467. }
  468. */
  469. public function trtc_callback(){
  470. $input = file_get_contents("php://input");
  471. filePut('start:'.$input,'callback.txt');
  472. /*$input = '{
  473. "EventGroupId": 1,
  474. "EventType": 104,
  475. "CallbackTs": 1640331295244,
  476. "EventInfo": {
  477. "RoomId": 1604,
  478. "EventTs": 1640331295,
  479. "EventMsTs": 1640331295239,
  480. "UserId": "26",
  481. "Role": 20,
  482. "Reason": 1
  483. }
  484. }';*/
  485. $input = json_decode($input,true);
  486. $roomId = $input['EventInfo']['RoomId'];
  487. //房间事件组
  488. if($input['EventGroupId'] == 1){
  489. if($input['EventType'] == 101){
  490. //创建房间,第一个进来的
  491. }elseif($input['EventType'] == 102){
  492. //解散房间,最后一个走的
  493. //没有UserId
  494. }elseif($input['EventType'] == 103){
  495. //进入房间,主播role=20 观众role=21
  496. $userId = $input['EventInfo']['UserId'];
  497. RedisUtil::getInstance('online',$roomId)->hSet($userId, $userId);
  498. // 记录在线用户在房间情况
  499. RedisUtil::getInstance('livingUser')->hSet($userId,$roomId);
  500. }elseif($input['EventType'] == 104){
  501. //退出房间,主播role=20 观众role=21
  502. $userId = $input['EventInfo']['UserId'];
  503. RedisUtil::getInstance('online',$roomId)->HDel($userId);
  504. // 扣除在线用户在房间情况
  505. RedisUtil::getInstance('livingUser')->hDel($userId);
  506. }
  507. }elseif($input['EventGroupId'] == 2){
  508. }
  509. //结束
  510. echo json_encode(['code' => 0]);exit;
  511. }
  512. /**
  513. * 回调
  514. */
  515. public function callback() {
  516. $input = file_get_contents("php://input"); // 主题信息
  517. if(!$input) {
  518. echo '无数据';exit;
  519. }
  520. $input = json_decode($input,true);
  521. $CallbackCommand = $input["CallbackCommand"];
  522. switch ($CallbackCommand) {
  523. case 'State.StateChange': // 用户在线状态变更
  524. $info = $input["Info"];
  525. if($info["Action"] == "Login") { // 用户登录
  526. /*$loginToday = Db::name('user')->where('id',$info["To_Account"])->whereTime('onlinetime', 'today')->find();
  527. if ($loginToday){
  528. \app\common\model\User::update(["is_online"=>1,"onlinetime"=>time()],["id"=>$info["To_Account"]]);
  529. }else{
  530. \app\common\model\User::update(["is_online"=>1,"onlinetime"=>time()],["id"=>$info["To_Account"]]);
  531. }*/
  532. $res = [];
  533. $res["ActionStatus"] = "OK";
  534. $res["ErrorCode"] = 0;
  535. $res["ErrorInfo"] = "";
  536. echo json_encode($res);exit;
  537. } elseif($info["Action"] == "Logout" || $info["Action"] == "Disconnect") { // 用户退出 不需要管什么原因
  538. // 更新用户在线状态为离线
  539. // $updateData['is_online'] = 0;
  540. // $user = Db::name('user')->where('id',$info["To_Account"])->find();
  541. // if (!empty($user)) {
  542. // // 客户觉得实时判断导致在线用户数量过少,此处,不再根据IM处理用户在线状态,已在API父类中做接口访问频率处理用户在线状态
  543. // // if ($user['gender'] == 1) {
  544. // // $updateData['is_active'] = 0;
  545. // // }
  546. // }
  547. // \app\common\model\User::update($updateData,["id"=>$info["To_Account"]]);
  548. //\app\common\model\User::update(["is_live"=>0],["id"=>$info["To_Account"]]);
  549. }
  550. break;
  551. // case 'Group.CallbackAfterNewMemberJoin': // 新成员入群
  552. // $members = $input["NewMemberList"];
  553. // if($members) foreach($members as $k => $v) {
  554. // RedisUtil::getInstance('online',$input["GroupId"])->hSet($v["Member_Account"],$v["Member_Account"]);
  555. // // 更新房间在线状态
  556. // $party_id = $input["GroupId"];
  557. // $partyInfo = $redis->get("party_".$party_id);
  558. // if($partyInfo) {
  559. // $partyInfo = json_decode($partyInfo,true);
  560. // if($partyInfo["is_online"] != 1) {
  561. // $partyInfo["is_online"] = 1;
  562. // $redis->set("party_".$party_id,json_encode($partyInfo));
  563. // \app\common\model\Party::update(["is_online"=>1],["id"=>$party_id]);
  564. // }
  565. // }
  566. // }
  567. // $res = [];
  568. // $res["ActionStatus"] = "OK";
  569. // $res["ErrorCode"] = 0;
  570. // $res["ErrorInfo"] = "";
  571. // echo json_encode($res);
  572. // break;
  573. // case 'Group.CallbackAfterMemberExit': // 成员离开
  574. // $members = $input["ExitMemberList"];
  575. // if($members) foreach($members as $k => $v) {
  576. // RedisUtil::getInstance('online',$input["GroupId"])->HDel($v["Member_Account"]);
  577. // }
  578. // // 更新房间在线状态
  579. // $party_id = $input["GroupId"];
  580. // $partyInfo = RedisUtil::getInstance('party',$party_id)->get();
  581. // if($partyInfo) {
  582. // $partyInfo = json_decode($partyInfo,true);
  583. // $memCount = count(RedisUtil::getInstance('online',$party_id)->hGetAll());
  584. // if($memCount <= 0) {
  585. // $partyInfo["is_online"] = 0;
  586. // RedisUtil::getInstance('party',$party_id)->set(json_encode($partyInfo));
  587. // \app\common\model\Party::update(["is_online"=>0],["id"=>$party_id]);
  588. // }
  589. // }
  590. //
  591. // $liveInfo = RedisUtil::getInstance('live',$party_id)->get();
  592. // if($liveInfo) {
  593. // $liveInfo = json_decode($liveInfo,true);
  594. // $memCount = count(RedisUtil::getInstance('online',$party_id)->hGetAll());
  595. // if($memCount <= 0) {
  596. // $liveInfo["is_online"] = 0;
  597. // RedisUtil::getInstance('live',$party_id)->set(json_encode($liveInfo));
  598. // \app\common\model\Party::update(["is_online"=>0],["id"=>$party_id]);
  599. // }
  600. // }
  601. //
  602. // $res = [];
  603. // $res["ActionStatus"] = "OK";
  604. // $res["ErrorCode"] = 0;
  605. // $res["ErrorInfo"] = "";
  606. // echo json_encode($res);
  607. // break;
  608. /*case 'Group.CallbackBeforeSendMsg': // 群内发送消息之前
  609. $config = config("wxMiniProgram");
  610. $access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$config["appid"]."&secret=".$config["secret"];
  611. $access_token_arr = $this->getJson($access_token_url);
  612. $access_token = isset($access_token_arr["access_token"])?$access_token_arr["access_token"]:"";
  613. if(!$access_token) {
  614. $res = [];
  615. $res["ActionStatus"] = "OK";
  616. $res["ErrorInfo"] = "";
  617. $res["ErrorCode"] = 0;
  618. echo json_encode($res);
  619. break;
  620. }
  621. // \app\common\model\Test::update(["content"=>json_encode($access_token)],["id"=>1]);
  622. $infoMessage = $input["MsgBody"];
  623. $badMessage = false;
  624. if($infoMessage) foreach($infoMessage as $k => $v) {
  625. if(isset($v["MsgContent"]["Text"])) {
  626. $message = json_decode($v["MsgContent"]["Text"],true);
  627. $content = isset($message["content"])?$message["content"]:"";
  628. $message_check_url ='https://api.weixin.qq.com/wxa/msg_sec_check?access_token='.$access_token;
  629. $tencentObj = new tencentim($message_check_url);
  630. $content_json = '{"content": "'.$content.'"}';
  631. $body = $tencentObj->messageCheck($content_json);
  632. if($body["errcode"] == 87014) {
  633. // \app\common\model\Test::update(["content"=>$body["errcode"]],["id"=>1]);
  634. // $res = [];
  635. // $res["ActionStatus"] = "OK";
  636. // $res["ErrorInfo"] = "检测到敏感词汇!";
  637. // $res["ErrorCode"] = 1;
  638. // echo json_encode($res);exit;
  639. $message["content"] = "*******";
  640. $infoMessage[$k]["MsgContent"]["Text"] = json_encode($message);
  641. $badMessage = true;
  642. }
  643. }
  644. }
  645. if($badMessage) {
  646. $res = [];
  647. $res["ActionStatus"] = "OK";
  648. $res["ErrorInfo"] = "检测到敏感词汇!";
  649. $res["ErrorCode"] = 10102;
  650. $res["MsgBody"] = $infoMessage;
  651. } else {
  652. $res = [];
  653. $res["ActionStatus"] = "OK";
  654. $res["ErrorInfo"] = "";
  655. $res["ErrorCode"] = 0;
  656. }
  657. // \app\common\model\Test::update(["content"=>json_encode($res)],["id"=>1]);
  658. echo json_encode($res);exit;
  659. break;*/
  660. default:
  661. break;
  662. }
  663. exit;
  664. }
  665. private function getJson($url){
  666. $ch = curl_init();
  667. curl_setopt($ch, CURLOPT_URL, $url);
  668. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  669. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  670. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  671. $output = curl_exec($ch);
  672. curl_close($ch);
  673. return json_decode($output, true);
  674. }
  675. /**
  676. * 发送消息给某人
  677. * type:0普通消息 1自定义消息
  678. */
  679. public function sendMessageToUser($from_user,$to_user,$message, $type = 0) {
  680. // $from_user = 54;
  681. // $to_user = 6;
  682. // $message = "sdsd";
  683. $random = rand(10000000,99999999);
  684. $usersig = $this->usersig("administrator");
  685. // 获取配置信息
  686. $config = config("tencent_im");
  687. $url = "https://console.tim.qq.com/v4/openim/sendmsg";
  688. $url .= "?sdkappid=".$config["sdkappid"];
  689. $url .= "&identifier=administrator";
  690. $url .= "&usersig=".$usersig;
  691. $url .= "&random=".$random;
  692. $url .= "&contenttype=json";
  693. $tencentObj = new tencentim($url);
  694. $data = [];
  695. $data["SyncOtherMachine"] = 1;
  696. $data["From_Account"] = (string)$from_user;
  697. $data["To_Account"] = (string)$to_user;
  698. $data["MsgRandom"] = rand(1000000,9999999);
  699. $data["MsgTimeStamp"] = time();
  700. if ($type == 1) { //自定义消息
  701. $body_message = '{"businessID":"custom_tips_message","receiverShow":true,"senderId":"'.$from_user.'","text":"'.$message.'","tipType":257,"version":0}';
  702. $data["MsgBody"][] = [
  703. "MsgType" => "TIMCustomElem",
  704. "MsgContent" => [
  705. "Data" => $body_message,
  706. "Desc" => $body_message,
  707. "Ext" => 'custom_tips_message',
  708. "Sound" => ''
  709. ],
  710. ];
  711. } else {
  712. $data["MsgBody"][] = [
  713. "MsgType" => "TIMTextElem",
  714. "MsgContent" => [
  715. "Text" => $message
  716. ],
  717. ];
  718. }
  719. $rs = $tencentObj->toSend($data);//p($rs);
  720. }
  721. /**
  722. * 发送消息给某人-接口调用
  723. */
  724. public function sendToUser() {
  725. $from_user = $this->request->request("from_user");// 发送者
  726. $to_user = $this->request->request("to_user");// 接收者
  727. $message = $this->request->request("message");// 接收者
  728. if(!$from_user || !$to_user || !$message) $this->error("参数缺失!");
  729. $this->sendMessageToUser($from_user,$to_user,$message, 1);
  730. }
  731. //请求的基础参数
  732. private function baseTencentObj($url)
  733. {
  734. $random = rand(10000000,99999999);
  735. $usersig = $this->usersig("administrator");
  736. // 获取配置信息
  737. $config = config("tencent_im");
  738. $url = "$url";
  739. $url .= "?sdkappid=".$config["sdkappid"];
  740. $url .= "&identifier=administrator";
  741. $url .= "&usersig=".$usersig;
  742. $url .= "&random=".$random;
  743. $url .= "&contenttype=json";
  744. return new tencentim($url);
  745. }
  746. /** 设置管理员(im端的改变)
  747. * @param $user_id
  748. * @param $party_id
  749. * @param int $status 1:yes 0:no
  750. * @return array|false|mixed
  751. */
  752. public function setUpIMAdmin($user_id, $party_id, int $status=0) {
  753. $url = "https://console.tim.qq.com/v4/group_open_http_svc/modify_group_member_info";
  754. $tencentObj = $this->baseTencentObj($url);
  755. $groupInfo = $this->getGroupInfo($party_id);
  756. if($groupInfo["GroupInfo"][0]["ErrorCode"] > 0) return false;
  757. // 更新的信息
  758. $data = [];
  759. $data["GroupId"] = $party_id;
  760. if ($status==1){
  761. $data['Role'] = 'Admin';
  762. }else{
  763. $data['Role'] = 'Member';
  764. }
  765. $data['Member_Account'] = $user_id;
  766. return $tencentObj->toSend($data);
  767. }
  768. //获取群成员详细资料(目前没用到)
  769. private function getGroupMemberDetails($party_id)
  770. {
  771. $url = "https://console.tim.qq.com/v4/group_open_http_svc/get_group_member_info";
  772. $tencentObj = $this->baseTencentObj($url);
  773. // 获取群组信息
  774. $groupInfo = $this->getGroupInfo($party_id);
  775. if($groupInfo["GroupInfo"][0]["ErrorCode"] > 0) return false;
  776. // 更新的信息
  777. $data = [];
  778. $data["GroupId"] = $party_id;
  779. return $tencentObj->toSend($data);
  780. }
  781. //IM管理员维护,同步本地管理员
  782. public function imAdminMaintenance()
  783. {
  784. $partyIds = \app\admin\model\party\Party::column('id');
  785. foreach ($partyIds as $party_id){
  786. $groupInfo = $this->getGroupInfo($party_id);
  787. if($groupInfo["GroupInfo"][0]["ErrorCode"] > 0) continue;
  788. $roomInfoData = $groupInfo["GroupInfo"][0]["AppDefinedData"];
  789. $roomInfo = [];
  790. if($roomInfoData) foreach($roomInfoData as $k => $v) {
  791. if($v["Key"] == "roomInfo") $roomInfo = $v["Value"];
  792. }
  793. $roomInfo = json_decode($roomInfo,true);
  794. // 更新管理员
  795. if(isset($roomInfo["managers"]) && !empty($roomInfo['managers'])) {
  796. foreach ($roomInfo['managers'] as $v){
  797. $this->setUpIMAdmin($v,$party_id,1);
  798. }
  799. }
  800. }
  801. $this->success("成功!");
  802. }
  803. //拉黑
  804. public function black_one($from_user = '',$to_user = '') {
  805. $from_user = (string)$from_user;
  806. $to_user = (string)$to_user;
  807. $random = rand(10000000,99999999);
  808. $usersig = $this->usersig("administrator");
  809. // 获取配置信息
  810. $config = config("tencent_im");
  811. $url = "https://console.tim.qq.com/v4/sns/black_list_add";
  812. $url .= "?sdkappid=".$config["sdkappid"];
  813. $url .= "&identifier=administrator";
  814. $url .= "&usersig=".$usersig;
  815. $url .= "&random=".$random;
  816. $url .= "&contenttype=json";
  817. $tencentObj = new tencentim($url);
  818. $data = [];
  819. $data["From_Account"] = $from_user;
  820. $data["To_Account"] = [$to_user];
  821. $rs = $tencentObj->toSend($data);
  822. return $rs;
  823. }
  824. //取消拉黑
  825. public function un_black_one($from_user = '',$to_user = '') {
  826. $from_user = (string)$from_user;
  827. $to_user = (string)$to_user;
  828. $random = rand(10000000,99999999);
  829. $usersig = $this->usersig("administrator");
  830. // 获取配置信息
  831. $config = config("tencent_im");
  832. $url = "https://console.tim.qq.com/v4/sns/black_list_delete";
  833. $url .= "?sdkappid=".$config["sdkappid"];
  834. $url .= "&identifier=administrator";
  835. $url .= "&usersig=".$usersig;
  836. $url .= "&random=".$random;
  837. $url .= "&contenttype=json";
  838. $tencentObj = new tencentim($url);
  839. $data = [];
  840. $data["From_Account"] = $from_user;
  841. $data["To_Account"] = [$to_user];
  842. $rs = $tencentObj->toSend($data);
  843. return $rs;
  844. }
  845. //撤回单聊消息
  846. public function withdraw_message($from_user = '', $to_user = '',$msgkey = '') {
  847. // $from_user = 54;
  848. // $to_user = 6;
  849. // $message = "sdsd";
  850. $random = rand(10000000,99999999);
  851. $usersig = $this->usersig("administrator");
  852. // 获取配置信息
  853. $config = config("tencent_im");
  854. $url = "https://console.tim.qq.com/v4/openim/admin_msgwithdraw";
  855. $url .= "?sdkappid=".$config["sdkappid"];
  856. $url .= "&identifier=administrator";
  857. $url .= "&usersig=".$usersig;
  858. $url .= "&random=".$random;
  859. $url .= "&contenttype=json";
  860. $tencentObj = new tencentim($url);
  861. $data = [];
  862. $data["From_Account"] = (string)$from_user;
  863. $data["To_Account"] = (string)$to_user;
  864. $data["MsgKey"] = (string)$msgkey;
  865. $rs = $tencentObj->toSend($data);//p($rs);
  866. return $rs;
  867. }
  868. }