Plantask.php 14 KB

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