Test.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. //下载聊天记录
  23. public function im(){
  24. $sdkappid = '1400818730';
  25. $sdkappkey = 'f31aa80a36cef72829fbcdcb10aa2aadcea2944787f2b268d46021e67711326b';
  26. $usersig = $this->usersig($sdkappid,$sdkappkey);
  27. $random = rand(10000000,99999999);
  28. $url = 'https://console.tim.qq.com/v4/open_msg_svc/get_history?sdkappid='.$sdkappid.'&identifier=administrator&usersig='.$usersig.'&random='.$random.'&contenttype=json';
  29. $data = [
  30. // 'ChatType' => 'Group',
  31. 'ChatType' => 'C2C',
  32. // 'MsgTime' => date('YmdH',strtotime('-1 Days')),
  33. 'MsgTime' => date('YmdH',strtotime('-1 Hours')),
  34. ];
  35. $tasklog = [
  36. 'type' => $data['ChatType'] == 'C2C' ? 1 : 2,
  37. 'datehour' => $data['MsgTime'],
  38. 'createtime' => time(),
  39. 'status' => 0,
  40. ];
  41. dump($data);
  42. $jsonStr = json_encode($data);
  43. $header = array(
  44. 'Content-Type: application/json; charset=utf-8',
  45. 'Content-Length: ' . strlen($jsonStr)
  46. );
  47. $rs = curl_post($url,$jsonStr,$header);
  48. $rs = json_decode($rs,true);
  49. dump($rs);
  50. if(is_array($rs) && isset($rs['ErrorCode']) && $rs['ErrorCode'] == 0){
  51. $tasklog['status'] = 1;
  52. //正常的,可以下载了
  53. if(isset($rs['File']) && !empty($rs['File'])){
  54. foreach($rs['File'] as $key => $val){
  55. //下载
  56. $gz_path = $this->downloadfile($val['URL'],$data['ChatType'],$data['MsgTime'].'_'.$data['ChatType'].'.json.gz');
  57. dump($gz_path);
  58. //解压
  59. $json_path = $this->jieyagz($gz_path);
  60. dump($json_path);
  61. //分析
  62. $content = $this->readjson($json_path);
  63. dump($content);
  64. //入库
  65. if(!empty($content)){
  66. Db::name('imlog_c2c')->insertAll($content);
  67. }
  68. }
  69. }
  70. }
  71. Db::name('imlog_tasklog')->insertGetId($tasklog);
  72. echo '结束';
  73. exit;
  74. }
  75. //测试下载gz
  76. public function test_downloadgz(){
  77. $data = [
  78. 'ChatType' => 'C2C',
  79. 'MsgTime' => date('YmdH',strtotime('-21 Hours')),
  80. ];
  81. $url = 'https://download.tim.qq.com/msg_history/v2/e9c80dce105d880ab8f62f0d267983c0edf3aff49031f5b06204c3402ddbdaf63a7b2aed.gz';
  82. $newgz = $this->downloadfile($url,$data['ChatType'],$data['MsgTime'].'_'.$data['ChatType'].'.json.gz');
  83. }
  84. //下载远程文件 到指定目录
  85. private function downloadfile($file_url, $path = '', $save_file_name = '')
  86. {
  87. $basepath = '/uploaded/';
  88. if ($path) {
  89. $basepath = $basepath . $path . '/';
  90. }
  91. $basepath = $basepath . date('Ymd');
  92. $dir_path = ROOT_PATH . '/public' . $basepath;
  93. if (!is_dir($dir_path)) {
  94. mkdir($dir_path, 0777, true);
  95. }
  96. $file = file_get_contents($file_url);
  97. //传入保存文件的名称
  98. $filename = $save_file_name ?: pathinfo($file_url, PATHINFO_BASENAME);
  99. $resource = fopen($dir_path. '/'. $filename, 'w');
  100. fwrite($resource, $file);
  101. fclose($resource);
  102. return $dir_path . '/' . $filename;
  103. }
  104. //解压
  105. private function jieyagz($gz_path = ''){
  106. // $gz_path = 'E:\phpstudy_pro\wwwnew\ggyuyin\/public/uploaded/C2C/20230726/2023072514_C2C.json.gz';
  107. $json_path = substr($gz_path,0,-3);
  108. if ($zp = gzopen($gz_path, 'r')) { // 打开压缩文件
  109. if ($fp = fopen($json_path, 'w')) { // 打开目标文件
  110. while (!gzeof($zp)) {
  111. fwrite($fp, gzread($zp, 1024 * 512)); // 逐块读取和解压缩后写入
  112. }
  113. fclose($fp);
  114. }
  115. gzclose($zp);
  116. }
  117. return $json_path;
  118. }
  119. //读取json并分析
  120. private function readjson($json_path = ''){
  121. $newMsgList = [];
  122. // $json_path = 'E:\phpstudy_pro\wwwnew\ggyuyin\/public/uploaded/C2C/20230726/2023072514_C2C.json';
  123. $json_content = file_get_contents($json_path);
  124. $json_content = json_decode($json_content,true);
  125. if(!empty($json_content) && is_array($json_content) && isset($json_content['MsgList'])){
  126. $MsgList = $json_content['MsgList'];
  127. if(!empty($MsgList)){
  128. foreach($MsgList as $key => $val)
  129. {
  130. $newMsgList[] = [
  131. 'ClientIP' => $val['ClientIP'],
  132. 'CloudCustomData' => isset($val['CloudCustomData']) ? $val['CloudCustomData'] : '',
  133. 'From_Account' => intval($val['From_Account']),
  134. 'MsgBody' => json_encode($val['MsgBody']),
  135. 'MsgFromPlatform' => $val['MsgFromPlatform'],
  136. 'MsgRandom' => $val['MsgRandom'],
  137. 'MsgSeq' => $val['MsgSeq'],
  138. 'MsgTimestamp' => $val['MsgTimestamp'],
  139. 'To_Account' => intval($val['To_Account']),
  140. ];
  141. }
  142. }
  143. }
  144. return $newMsgList;
  145. }
  146. private function usersig($sdkappid,$key){
  147. $api = new TLSSigAPIv2($sdkappid,$key );
  148. $sig = $api->genUserSig('administrator');
  149. return $sig;
  150. }
  151. public function test_redis(){
  152. $redis = new Redis();
  153. $redisconfig = config("redis");
  154. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  155. if ($redisconfig['redis_pwd']) {
  156. $redis->auth($redisconfig['redis_pwd']);
  157. }
  158. if($redisconfig['redis_selectdb'] > 0){
  159. $redis->select($redisconfig['redis_selectdb']);
  160. }
  161. $party_id = 141;
  162. $res = $redis->hGetAll("online_".$party_id);
  163. dump($res);
  164. }
  165. }