Test.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. use \GatewayWorker\Lib\Gateway;
  5. use app\common\library\Tlssigapiv2;
  6. use think\Db;
  7. class Test extends Controller
  8. {
  9. public function test(){
  10. $Gateway = new Gateway();
  11. $Gateway::$registerAddress = '127.0.0.1:2345';
  12. $Gateway::sendToAll('来自服务端的主动推送'.date('Y-m-d H:i:s'));
  13. }
  14. public function ip(){
  15. $ipaddress = ip_to_address();
  16. echo $ipaddress;
  17. }
  18. public function newip(){
  19. $ipaddress = newip_to_address();
  20. echo $ipaddress;
  21. }
  22. public function auto_imgroup(){
  23. $sdkappid = '1400818730';
  24. $sdkappkey = 'f31aa80a36cef72829fbcdcb10aa2aadcea2944787f2b268d46021e67711326b';
  25. $usersig = $this->usersig($sdkappid,$sdkappkey);
  26. $random = rand(10000000,99999999);
  27. $url = 'https://console.tim.qq.com/v4/open_msg_svc/get_history?sdkappid='.$sdkappid.'&identifier=administrator&usersig='.$usersig.'&random='.$random.'&contenttype=json';
  28. $data = [
  29. 'ChatType' => 'Group',
  30. 'MsgTime' => date('YmdH',strtotime('-23 Hours')),
  31. ];
  32. $tasklog = [
  33. 'type' => $data['ChatType'] == 'C2C' ? 1 : 2,
  34. 'datehour' => $data['MsgTime'],
  35. 'createtime' => time(),
  36. 'status' => 0,
  37. ];
  38. dump($data);
  39. $jsonStr = json_encode($data);
  40. $header = array(
  41. 'Content-Type: application/json; charset=utf-8',
  42. 'Content-Length: ' . strlen($jsonStr)
  43. );
  44. $rs = curl_post($url,$jsonStr,$header);
  45. $rs = json_decode($rs,true);
  46. dump($rs);
  47. if(is_array($rs) && isset($rs['ErrorCode']) && $rs['ErrorCode'] == 0){
  48. $tasklog['status'] = 1;
  49. //正常的,可以下载了
  50. if(isset($rs['File']) && !empty($rs['File'])){
  51. foreach($rs['File'] as $key => $val){
  52. echo $val['URL'];
  53. //下载
  54. $gz_path = $this->downloadfile($val['URL'],$data['ChatType'],$data['MsgTime'].'_'.$data['ChatType'].'.json.gz');
  55. dump($gz_path);
  56. //解压
  57. $json_path = $this->jieyagz($gz_path);
  58. dump($json_path);
  59. //分析
  60. $content = $this->readjson_group($json_path);
  61. dump($content);
  62. //入库
  63. if(!empty($content)){
  64. Db::name('imlog_group')->insertAll($content);
  65. }
  66. }
  67. }
  68. }
  69. Db::name('imlog_tasklog')->insertGetId($tasklog);
  70. echo '结束';
  71. exit;
  72. }
  73. //下载聊天记录
  74. public function auto_imc2c(){
  75. $sdkappid = '1400818730';
  76. $sdkappkey = 'f31aa80a36cef72829fbcdcb10aa2aadcea2944787f2b268d46021e67711326b';
  77. $usersig = $this->usersig($sdkappid,$sdkappkey);
  78. $random = rand(10000000,99999999);
  79. $url = 'https://console.tim.qq.com/v4/open_msg_svc/get_history?sdkappid='.$sdkappid.'&identifier=administrator&usersig='.$usersig.'&random='.$random.'&contenttype=json';
  80. $data = [
  81. 'ChatType' => 'C2C',
  82. 'MsgTime' => date('YmdH',strtotime('-3 Hours')),
  83. ];
  84. $tasklog = [
  85. 'type' => $data['ChatType'] == 'C2C' ? 1 : 2,
  86. 'datehour' => $data['MsgTime'],
  87. 'createtime' => time(),
  88. 'status' => 0,
  89. ];
  90. dump($data);
  91. $jsonStr = json_encode($data);
  92. $header = array(
  93. 'Content-Type: application/json; charset=utf-8',
  94. 'Content-Length: ' . strlen($jsonStr)
  95. );
  96. $rs = curl_post($url,$jsonStr,$header);
  97. $rs = json_decode($rs,true);
  98. dump($rs);
  99. if(is_array($rs) && isset($rs['ErrorCode']) && $rs['ErrorCode'] == 0){
  100. $tasklog['status'] = 1;
  101. //正常的,可以下载了
  102. if(isset($rs['File']) && !empty($rs['File'])){
  103. foreach($rs['File'] as $key => $val){
  104. //下载
  105. $gz_path = $this->downloadfile($val['URL'],$data['ChatType'],$data['MsgTime'].'_'.$data['ChatType'].'.json.gz');
  106. dump($gz_path);
  107. //解压
  108. $json_path = $this->jieyagz($gz_path);
  109. dump($json_path);
  110. //分析
  111. $content = $this->readjson($json_path);
  112. dump($content);
  113. //入库
  114. if(!empty($content)){
  115. Db::name('imlog_c2c')->insertAll($content);
  116. }
  117. }
  118. }
  119. }
  120. Db::name('imlog_tasklog')->insertGetId($tasklog);
  121. echo '结束';
  122. exit;
  123. }
  124. //下载远程文件 到指定目录
  125. private function downloadfile($file_url, $path = '', $save_file_name = '')
  126. {
  127. $basepath = '/uploaded/';
  128. if ($path) {
  129. $basepath = $basepath . $path . '/';
  130. }
  131. $basepath = $basepath . date('Ymd');
  132. $dir_path = ROOT_PATH . '/public' . $basepath;
  133. if (!is_dir($dir_path)) {
  134. mkdir($dir_path, 0777, true);
  135. }
  136. $file = file_get_contents($file_url);
  137. //传入保存文件的名称
  138. $filename = $save_file_name ?: pathinfo($file_url, PATHINFO_BASENAME);
  139. $resource = fopen($dir_path. '/'. $filename, 'w');
  140. fwrite($resource, $file);
  141. fclose($resource);
  142. return $dir_path . '/' . $filename;
  143. }
  144. //解压
  145. private function jieyagz($gz_path = ''){
  146. // $gz_path = 'E:\phpstudy_pro\wwwnew\ggyuyin\/public/uploaded/C2C/20230726/2023072514_C2C.json.gz';
  147. $json_path = substr($gz_path,0,-3);
  148. if ($zp = gzopen($gz_path, 'r')) { // 打开压缩文件
  149. if ($fp = fopen($json_path, 'w')) { // 打开目标文件
  150. while (!gzeof($zp)) {
  151. fwrite($fp, gzread($zp, 1024 * 512)); // 逐块读取和解压缩后写入
  152. }
  153. fclose($fp);
  154. }
  155. gzclose($zp);
  156. }
  157. return $json_path;
  158. }
  159. //读取json并分析
  160. private function readjson($json_path = ''){
  161. $newMsgList = [];
  162. // $json_path = 'E:\phpstudy_pro\wwwnew\ggyuyin\/public/uploaded/C2C/20230726/2023072514_C2C.json';
  163. $json_content = file_get_contents($json_path);
  164. $json_content = json_decode($json_content,true);
  165. if(!empty($json_content) && is_array($json_content) && isset($json_content['MsgList'])){
  166. $MsgList = $json_content['MsgList'];
  167. if(!empty($MsgList)){
  168. foreach($MsgList as $key => $val)
  169. {
  170. $newone = [
  171. 'ClientIP' => isset($val['ClientIP']) ? $val['ClientIP'] : '',
  172. 'CloudCustomData' => isset($val['CloudCustomData']) ? $val['CloudCustomData'] : '',
  173. 'From_Account' => isset($val['From_Account']) ? intval($val['From_Account']) : 0,
  174. //'MsgBody' => isset($val['MsgBody']) ? json_encode($val['MsgBody']) : '',
  175. 'MsgFromPlatform' => isset($val['MsgFromPlatform']) ? $val['MsgFromPlatform'] : '',
  176. 'MsgRandom' => isset($val['MsgRandom']) ? $val['MsgRandom'] : '',
  177. 'MsgSeq' => isset($val['MsgSeq']) ? $val['MsgSeq'] : '',
  178. 'MsgTimestamp' => isset($val['MsgTimestamp']) ? $val['MsgTimestamp'] : '',
  179. 'To_Account' => isset($val['To_Account']) ? intval($val['To_Account']) : 0,
  180. ];
  181. //解析数据类型
  182. if(isset($val['MsgBody'][0]['MsgType'])){
  183. $newone['MsgType'] = $val['MsgBody'][0]['MsgType'];
  184. //文本
  185. if($val['MsgBody'][0]['MsgType'] == 'TIMTextElem'){
  186. $newone['MsgInfo'] = '';
  187. if(isset($val['MsgBody'][0]['MsgContent']['Text'])){
  188. $newone['MsgInfo'] = $val['MsgBody'][0]['MsgContent']['Text'];
  189. }
  190. }
  191. //图片
  192. if($val['MsgBody'][0]['MsgType'] == 'TIMImageElem'){
  193. $newone['MsgInfo'] = '';
  194. if(isset($val['MsgBody'][0]['MsgContent']['ImageInfoArray'][0]['URL'])){
  195. $newone['MsgInfo'] = $val['MsgBody'][0]['MsgContent']['ImageInfoArray'][0]['URL'];
  196. }
  197. }
  198. //声音
  199. if($val['MsgBody'][0]['MsgType'] == 'TIMSoundElem'){
  200. $newone['MsgInfo'] = '';
  201. if(isset($val['MsgBody'][0]['MsgContent']['Url'])){
  202. $newone['MsgInfo'] = $val['MsgBody'][0]['MsgContent']['Url'];
  203. }
  204. }
  205. //视频
  206. if($val['MsgBody'][0]['MsgType'] == 'TIMVideoFileElem'){
  207. $newone['MsgInfo'] = '';
  208. if(isset($val['MsgBody'][0]['MsgContent']['VideoUrl'])){
  209. $newone['MsgInfo'] = $val['MsgBody'][0]['MsgContent']['VideoUrl'];
  210. }
  211. }
  212. //其他
  213. continue;
  214. }else{
  215. continue;
  216. }
  217. $newMsgList[] = $newone;
  218. }
  219. }
  220. }
  221. return $newMsgList;
  222. }
  223. //读取json并分析
  224. public function readjson_group($json_path = ''){
  225. $newMsgList = [];
  226. //$json_path = 'E:\phpstudy_pro\wwwnew\ggyuyin\/public/uploaded/Group/20230727/2023072615_Group.json';
  227. $json_content = file_get_contents($json_path);
  228. $json_content = json_decode($json_content,true);
  229. if(!empty($json_content) && is_array($json_content) && isset($json_content['MsgList'])){
  230. $MsgList = $json_content['MsgList'];
  231. if(!empty($MsgList)){
  232. foreach($MsgList as $key => $val)
  233. {
  234. $newone = [
  235. //'key' => $key,辅助查找
  236. 'ClientIP' => isset($val['ClientIP']) ? $val['ClientIP'] : '',
  237. 'From_Account' => isset($val['From_Account']) ? intval($val['From_Account']) : 0,
  238. 'GroupId' => isset($val['GroupId']) ? intval($val['GroupId']) : 0,
  239. //'MsgBody' => isset($val['MsgBody']) ? json_encode($val['MsgBody']) : '',
  240. 'MsgFromPlatform' => isset($val['MsgFromPlatform']) ? $val['MsgFromPlatform'] : '',
  241. 'MsgSeq' => isset($val['MsgSeq']) ? $val['MsgSeq'] : '',
  242. 'MsgTimestamp' => isset($val['MsgTimestamp']) ? $val['MsgTimestamp'] : '',
  243. ];
  244. //解析数据类型
  245. if(isset($val['MsgBody'][0]['MsgType'])){
  246. $newone['MsgType'] = $val['MsgBody'][0]['MsgType'];
  247. //文本
  248. if($val['MsgBody'][0]['MsgType'] == 'TIMTextElem'){
  249. $newone['MsgInfo'] = '';
  250. if(isset($val['MsgBody'][0]['MsgContent']['Text'])){
  251. //继续解析
  252. $text = json_decode($val['MsgBody'][0]['MsgContent']['Text'],true);
  253. if(isset($text['type']) && $text['type'] == 1){
  254. //TYPE_NORMAL =1;//普通消息
  255. $newone['MsgInfo'] = isset($text['content']) ? $text['content'] : '';
  256. $newMsgList[] = $newone;
  257. }
  258. //其他$text['type']的值都是房间内的,礼物的,表情等,不需要记录
  259. }
  260. }
  261. //房间内的不需要显示的内容
  262. if($val['MsgBody'][0]['MsgType'] == 'TIMCustomElem'){
  263. }
  264. }
  265. //大循环结束
  266. }
  267. }
  268. }
  269. return $newMsgList;
  270. }
  271. private function usersig($sdkappid,$key){
  272. $api = new TLSSigAPIv2($sdkappid,$key );
  273. $sig = $api->genUserSig('administrator');
  274. return $sig;
  275. }
  276. public function test_redis(){
  277. $redis = new Redis();
  278. $redisconfig = config("redis");
  279. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  280. if ($redisconfig['redis_pwd']) {
  281. $redis->auth($redisconfig['redis_pwd']);
  282. }
  283. if($redisconfig['redis_selectdb'] > 0){
  284. $redis->select($redisconfig['redis_selectdb']);
  285. }
  286. $party_id = 141;
  287. $res = $redis->hGetAll("online_".$party_id);
  288. dump($res);
  289. }
  290. }