Plantask.php 12 KB

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