浏览代码

群组聊天记录

lizhen_gitee 1 年之前
父节点
当前提交
bb037b8a99
共有 2 个文件被更改,包括 121 次插入23 次删除
  1. 120 22
      application/index/controller/Test.php
  2. 1 1
      public/assets/js/backend/imlogc2c.js

+ 120 - 22
application/index/controller/Test.php

@@ -27,8 +27,7 @@ class Test extends Controller
         echo $ipaddress;
     }
 
-    //下载聊天记录
-    public function im(){
+    public function auto_imgroup(){
         $sdkappid  = '1400818730';
         $sdkappkey = 'f31aa80a36cef72829fbcdcb10aa2aadcea2944787f2b268d46021e67711326b';
         $usersig   = $this->usersig($sdkappid,$sdkappkey);
@@ -38,10 +37,8 @@ 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' => 'C2C',
-//            'MsgTime'  => date('YmdH',strtotime('-1 Days')),
-            'MsgTime'  => date('YmdH',strtotime('-3 Hours')),
+            'ChatType' => 'Group',
+            'MsgTime'  => date('YmdH',strtotime('-19 Hours')),
         ];
         $tasklog = [
             'type' => $data['ChatType'] == 'C2C' ? 1 : 2,
@@ -64,6 +61,7 @@ class Test extends Controller
             //正常的,可以下载了
             if(isset($rs['File']) && !empty($rs['File'])){
                 foreach($rs['File'] as $key => $val){
+                    echo $val['URL'];
                     //下载
                     $gz_path = $this->downloadfile($val['URL'],$data['ChatType'],$data['MsgTime'].'_'.$data['ChatType'].'.json.gz');
                     dump($gz_path);
@@ -71,11 +69,11 @@ class Test extends Controller
                     $json_path = $this->jieyagz($gz_path);
                     dump($json_path);
                     //分析
-                    $content = $this->readjson($json_path);
+                    $content = $this->readjson_group($json_path);
                     dump($content);
                     //入库
                     if(!empty($content)){
-                        Db::name('imlog_c2c')->insertAll($content);
+                        Db::name('imlog_group')->insertAll($content);
                     }
                 }
             }
@@ -86,17 +84,64 @@ class Test extends Controller
         exit;
     }
 
-    //测试下载gz
-    public function test_downloadgz(){
+    //下载聊天记录
+    public function auto_imc2c(){
+        $sdkappid  = '1400818730';
+        $sdkappkey = 'f31aa80a36cef72829fbcdcb10aa2aadcea2944787f2b268d46021e67711326b';
+        $usersig   = $this->usersig($sdkappid,$sdkappkey);
+
+
+        $random = rand(10000000,99999999);
+        $url = 'https://console.tim.qq.com/v4/open_msg_svc/get_history?sdkappid='.$sdkappid.'&identifier=administrator&usersig='.$usersig.'&random='.$random.'&contenttype=json';
+
         $data = [
             'ChatType' => 'C2C',
-            'MsgTime'  => date('YmdH',strtotime('-21 Hours')),
+            'MsgTime'  => date('YmdH',strtotime('-3 Hours')),
         ];
+        $tasklog = [
+            'type' => $data['ChatType'] == 'C2C' ? 1 : 2,
+            'datehour' => $data['MsgTime'],
+            'createtime' => time(),
+            'status' => 0,
+        ];
+        dump($data);
+        $jsonStr = json_encode($data);
 
-        $url = 'https://download.tim.qq.com/msg_history/v2/e9c80dce105d880ab8f62f0d267983c0edf3aff49031f5b06204c3402ddbdaf63a7b2aed.gz';
-        $newgz = $this->downloadfile($url,$data['ChatType'],$data['MsgTime'].'_'.$data['ChatType'].'.json.gz');
+        $header = array(
+            'Content-Type: application/json; charset=utf-8',
+            'Content-Length: ' . strlen($jsonStr)
+        );
+        $rs = curl_post($url,$jsonStr,$header);
+        $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){
+                    //下载
+                    $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;
     }
 
+
     //下载远程文件 到指定目录
     private function downloadfile($file_url, $path = '', $save_file_name = '')
     {
@@ -158,15 +203,15 @@ class Test extends Controller
                 foreach($MsgList as $key => $val)
                 {
                     $newone = [
-                        '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']),
+                        'ClientIP'        => isset($val['ClientIP'])        ? $val['ClientIP']             : '',
+                        'CloudCustomData' => isset($val['CloudCustomData']) ? $val['CloudCustomData']      : '',
+                        'From_Account'    => isset($val['From_Account'])    ? intval($val['From_Account']) : 0,
+                        //'MsgBody'         => isset($val['MsgBody'])         ? json_encode($val['MsgBody']) : '',
+                        'MsgFromPlatform' => isset($val['MsgFromPlatform']) ? $val['MsgFromPlatform']      : '',
+                        'MsgRandom'       => isset($val['MsgRandom'])       ? $val['MsgRandom']            : '',
+                        'MsgSeq'          => isset($val['MsgSeq'])          ? $val['MsgSeq']               : '',
+                        'MsgTimestamp'    => isset($val['MsgTimestamp'])    ? $val['MsgTimestamp']         : '',
+                        'To_Account'      => isset($val['To_Account'])      ? intval($val['To_Account'])   : 0,
                     ];
                     //解析数据类型
                     if(isset($val['MsgBody'][0]['MsgType'])){
@@ -209,6 +254,59 @@ class Test extends Controller
         return $newMsgList;
     }
 
+    //读取json并分析
+    public function readjson_group($json_path = ''){
+        $newMsgList = [];
+        $json_path    = 'E:\phpstudy_pro\wwwnew\ggyuyin\/public/uploaded/Group/20230727/2023072615_Group.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)
+                {
+                    $newone = [
+                        'ClientIP'        => isset($val['ClientIP'])        ? $val['ClientIP']             : '',
+                        'From_Account'    => isset($val['From_Account'])    ? intval($val['From_Account']) : 0,
+                        'GroupId'         => isset($val['GroupId'])         ? intval($val['GroupId'])      : 0,
+                        //'MsgBody'         => isset($val['MsgBody'])         ? json_encode($val['MsgBody']) : '',
+                        'MsgFromPlatform' => isset($val['MsgFromPlatform']) ? $val['MsgFromPlatform']      : '',
+                        'MsgSeq'          => isset($val['MsgSeq'])          ? $val['MsgSeq']               : '',
+                        'MsgTimestamp'    => isset($val['MsgTimestamp'])    ? $val['MsgTimestamp']         : '',
+                    ];
+                    //解析数据类型
+                    if(isset($val['MsgBody'][0]['MsgType'])){
+                        $newone['MsgType'] = $val['MsgBody'][0]['MsgType'];
+                        //文本
+                        if($val['MsgBody'][0]['MsgType'] == 'TIMTextElem'){
+                            $newone['MsgInfo'] = '';
+                            if(isset($val['MsgBody'][0]['MsgContent']['Text'])){
+                                //继续解析
+                                $text = json_decode($val['MsgBody'][0]['MsgContent']['Text'],true);
+                                if(isset($text['type']) && $text['type'] == 1){
+                                    //TYPE_NORMAL =1;//普通消息
+                                    $newone['MsgInfo'] = isset($text['content']) ? $text['content'] : '';
+                                }
+                                //其他$text['type']的值都是房间内的,礼物的,表情等,不需要记录
+                            }
+                        }
+                        //房间内的不需要显示的内容
+                        if($val['MsgBody'][0]['MsgType'] == 'TIMCustomElem'){
+
+                        }
+
+                    }
+
+                    $newMsgList[] = $newone;
+                }
+            }
+        }
+
+        return $newMsgList;
+    }
+
     private function usersig($sdkappid,$key){
         $api = new TLSSigAPIv2($sdkappid,$key );
         $sig = $api->genUserSig('administrator');

+ 1 - 1
public/assets/js/backend/imlogc2c.js

@@ -29,13 +29,13 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                         {field: 'ClientIP', title: __('Clientip'), operate: 'LIKE'},
                         {field: 'From_Account', title: __('From_account')},
                         {field: 'fromuser.nickname', title: __('fromuser.nickname'), operate: 'LIKE'},
-                        {field: 'MsgType', title: '类型'},
                         {field: 'MsgFromPlatform', title: __('Msgfromplatform'), operate: 'LIKE'},
                         {field: 'MsgRandom', title: __('Msgrandom'), operate: 'LIKE'},
                         {field: 'MsgSeq', title: __('Msgseq'), operate: 'LIKE'},
                         {field: 'MsgTimestamp', title: __('Msgtimestamp'), operate: 'LIKE'},
                         {field: 'To_Account', title: __('To_account')},
                         {field: 'touser.nickname', title: __('touser.nickname'), operate: 'LIKE'},
+                        {field: 'MsgType', title: '类型'},
                         {field: 'operate', title: __('Operate'),
                             buttons:[
                                 {