|  | @@ -6,6 +6,7 @@ use think\Controller;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  use \GatewayWorker\Lib\Gateway;
 | 
	
		
			
				|  |  |  use app\common\library\Tlssigapiv2;
 | 
	
		
			
				|  |  | +use think\Db;
 | 
	
		
			
				|  |  |  class Test extends Controller
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  |      public function test(){
 | 
	
	
		
			
				|  | @@ -26,7 +27,7 @@ class Test extends Controller
 | 
	
		
			
				|  |  |          echo $ipaddress;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +    //下载聊天记录
 | 
	
		
			
				|  |  |      public function im(){
 | 
	
		
			
				|  |  |          $sdkappid  = '1400818730';
 | 
	
		
			
				|  |  |          $sdkappkey = 'f31aa80a36cef72829fbcdcb10aa2aadcea2944787f2b268d46021e67711326b';
 | 
	
	
		
			
				|  | @@ -37,10 +38,16 @@ class Test extends Controller
 | 
	
		
			
				|  |  |          $url = 'https://console.tim.qq.com/v4/open_msg_svc/get_history?sdkappid='.$sdkappid.'&identifier=administrator&usersig='.$usersig.'&random='.$random.'&contenttype=json';
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          $data = [
 | 
	
		
			
				|  |  | -            'ChatType' => 'Group',
 | 
	
		
			
				|  |  | +//            'ChatType' => 'Group',
 | 
	
		
			
				|  |  |              'ChatType' => 'C2C',
 | 
	
		
			
				|  |  |  //            'MsgTime'  => date('YmdH',strtotime('-1 Days')),
 | 
	
		
			
				|  |  | -            'MsgTime'  => date('YmdH',strtotime('-20 Hours')),
 | 
	
		
			
				|  |  | +            'MsgTime'  => date('YmdH',strtotime('-1 Hours')),
 | 
	
		
			
				|  |  | +        ];
 | 
	
		
			
				|  |  | +        $tasklog = [
 | 
	
		
			
				|  |  | +            'type' => $data['ChatType'] == 'C2C' ? 1 : 2,
 | 
	
		
			
				|  |  | +            'datehour' => $data['MsgTime'],
 | 
	
		
			
				|  |  | +            'createtime' => time(),
 | 
	
		
			
				|  |  | +            'status' => 0,
 | 
	
		
			
				|  |  |          ];
 | 
	
		
			
				|  |  |          dump($data);
 | 
	
		
			
				|  |  |          $jsonStr = json_encode($data);
 | 
	
	
		
			
				|  | @@ -53,30 +60,119 @@ class Test extends Controller
 | 
	
		
			
				|  |  |          $rs = json_decode($rs,true);
 | 
	
		
			
				|  |  |          dump($rs);
 | 
	
		
			
				|  |  |          if(is_array($rs) && isset($rs['ErrorCode']) && $rs['ErrorCode'] == 0){
 | 
	
		
			
				|  |  | +            $tasklog['status'] = 1;
 | 
	
		
			
				|  |  |              //正常的,可以下载了
 | 
	
		
			
				|  |  |              if(isset($rs['File']) && !empty($rs['File'])){
 | 
	
		
			
				|  |  |                  foreach($rs['File'] as $key => $val){
 | 
	
		
			
				|  |  |                      //下载
 | 
	
		
			
				|  |  | -                    $newgz = downloadfile($val['URL'],$data['ChatType'],$data['MsgTime'].'_'.$data['ChatType'].'.json.gz');
 | 
	
		
			
				|  |  | -                    echo $newgz;
 | 
	
		
			
				|  |  | +                    $gz_path = $this->downloadfile($val['URL'],$data['ChatType'],$data['MsgTime'].'_'.$data['ChatType'].'.json.gz');
 | 
	
		
			
				|  |  | +                    dump($gz_path);
 | 
	
		
			
				|  |  |                      //解压
 | 
	
		
			
				|  |  | +                    $json_path = $this->jieyagz($gz_path);
 | 
	
		
			
				|  |  | +                    dump($json_path);
 | 
	
		
			
				|  |  |                      //分析
 | 
	
		
			
				|  |  | +                    $content = $this->readjson($json_path);
 | 
	
		
			
				|  |  | +                    dump($content);
 | 
	
		
			
				|  |  |                      //入库
 | 
	
		
			
				|  |  | +                    if(!empty($content)){
 | 
	
		
			
				|  |  | +                        Db::name('imlog_c2c')->insertAll($content);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | +        Db::name('imlog_tasklog')->insertGetId($tasklog);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +        echo '结束';
 | 
	
		
			
				|  |  | +        exit;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      //测试下载gz
 | 
	
		
			
				|  |  | -    public function test11(){
 | 
	
		
			
				|  |  | +    public function test_downloadgz(){
 | 
	
		
			
				|  |  |          $data = [
 | 
	
		
			
				|  |  |              'ChatType' => 'C2C',
 | 
	
		
			
				|  |  | -            'MsgTime'  => date('YmdH',strtotime('-20 Hours')),
 | 
	
		
			
				|  |  | +            'MsgTime'  => date('YmdH',strtotime('-21 Hours')),
 | 
	
		
			
				|  |  |          ];
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          $url = 'https://download.tim.qq.com/msg_history/v2/e9c80dce105d880ab8f62f0d267983c0edf3aff49031f5b06204c3402ddbdaf63a7b2aed.gz';
 | 
	
		
			
				|  |  | -        $newgz = downloadfile($url,$data['ChatType'],$data['MsgTime'].'_'.$data['ChatType'].'.json.gz');
 | 
	
		
			
				|  |  | +        $newgz = $this->downloadfile($url,$data['ChatType'],$data['MsgTime'].'_'.$data['ChatType'].'.json.gz');
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    //下载远程文件 到指定目录
 | 
	
		
			
				|  |  | +    private function downloadfile($file_url, $path = '', $save_file_name = '')
 | 
	
		
			
				|  |  | +    {
 | 
	
		
			
				|  |  | +        $basepath = '/uploaded/';
 | 
	
		
			
				|  |  | +        if ($path) {
 | 
	
		
			
				|  |  | +            $basepath = $basepath . $path . '/';
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        $basepath = $basepath . date('Ymd');
 | 
	
		
			
				|  |  | +        $dir_path = ROOT_PATH . '/public' . $basepath;
 | 
	
		
			
				|  |  | +        if (!is_dir($dir_path)) {
 | 
	
		
			
				|  |  | +            mkdir($dir_path, 0777, true);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        $file = file_get_contents($file_url);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //传入保存文件的名称
 | 
	
		
			
				|  |  | +        $filename = $save_file_name ?: pathinfo($file_url, PATHINFO_BASENAME);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        $resource = fopen($dir_path. '/'. $filename, 'w');
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        fwrite($resource, $file);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        fclose($resource);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return $dir_path . '/' . $filename;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    //解压
 | 
	
		
			
				|  |  | +    private function jieyagz($gz_path = ''){
 | 
	
		
			
				|  |  | +//        $gz_path   = 'E:\phpstudy_pro\wwwnew\ggyuyin\/public/uploaded/C2C/20230726/2023072514_C2C.json.gz';
 | 
	
		
			
				|  |  | +        $json_path = substr($gz_path,0,-3);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if ($zp = gzopen($gz_path, 'r')) { // 打开压缩文件
 | 
	
		
			
				|  |  | +            if ($fp = fopen($json_path, 'w')) { // 打开目标文件
 | 
	
		
			
				|  |  | +                while (!gzeof($zp)) {
 | 
	
		
			
				|  |  | +                    fwrite($fp, gzread($zp, 1024 * 512)); // 逐块读取和解压缩后写入
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                fclose($fp);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            gzclose($zp);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return $json_path;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    //读取json并分析
 | 
	
		
			
				|  |  | +    private function readjson($json_path = ''){
 | 
	
		
			
				|  |  | +        $newMsgList = [];
 | 
	
		
			
				|  |  | +//        $json_path    = 'E:\phpstudy_pro\wwwnew\ggyuyin\/public/uploaded/C2C/20230726/2023072514_C2C.json';
 | 
	
		
			
				|  |  | +        $json_content = file_get_contents($json_path);
 | 
	
		
			
				|  |  | +        $json_content = json_decode($json_content,true);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if(!empty($json_content) && is_array($json_content) && isset($json_content['MsgList'])){
 | 
	
		
			
				|  |  | +            $MsgList = $json_content['MsgList'];
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            if(!empty($MsgList)){
 | 
	
		
			
				|  |  | +                foreach($MsgList as $key => $val)
 | 
	
		
			
				|  |  | +                {
 | 
	
		
			
				|  |  | +                    $newMsgList[] = [
 | 
	
		
			
				|  |  | +                        'ClientIP'        => $val['ClientIP'],
 | 
	
		
			
				|  |  | +                        'CloudCustomData' => isset($val['CloudCustomData']) ? $val['CloudCustomData'] : '',
 | 
	
		
			
				|  |  | +                        'From_Account'    => intval($val['From_Account']),
 | 
	
		
			
				|  |  | +                        'MsgBody'         => json_encode($val['MsgBody']),
 | 
	
		
			
				|  |  | +                        'MsgFromPlatform' => $val['MsgFromPlatform'],
 | 
	
		
			
				|  |  | +                        'MsgRandom'       => $val['MsgRandom'],
 | 
	
		
			
				|  |  | +                        'MsgSeq'          => $val['MsgSeq'],
 | 
	
		
			
				|  |  | +                        'MsgTimestamp'    => $val['MsgTimestamp'],
 | 
	
		
			
				|  |  | +                        'To_Account'      => intval($val['To_Account']),
 | 
	
		
			
				|  |  | +                    ];
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return $newMsgList;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      private function usersig($sdkappid,$key){
 |