Plantask.php 12 KB

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