Plantask.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. ////////////////////////////////////////下面都是计划任务方法///////////////////////////////////////////////////////////////
  11. //主动拉取im群组内 聊天记录
  12. public function auto_imgroup(){
  13. $im_config = config('tencent_im');
  14. $sdkappid = $im_config['sdkappid'];
  15. $sdkappkey = $im_config['key'];
  16. $identifier= $im_config['identifier'];
  17. $usersig = $this->usersig($sdkappid,$sdkappkey,$identifier);
  18. $random = rand(10000000,99999999);
  19. $url = 'https://console.tim.qq.com/v4/open_msg_svc/get_history?sdkappid='.$sdkappid.'&identifier=administrator&usersig='.$usersig.'&random='.$random.'&contenttype=json';
  20. $data = [
  21. 'ChatType' => 'Group',
  22. 'MsgTime' => date('YmdH',strtotime('-3 Hours')),
  23. ];
  24. $tasklog = [
  25. 'type' => $data['ChatType'] == 'C2C' ? 1 : 2,
  26. 'datehour' => $data['MsgTime'],
  27. 'createtime' => time(),
  28. 'status' => 0,
  29. ];
  30. dump($data);
  31. $jsonStr = json_encode($data);
  32. $header = array(
  33. 'Content-Type: application/json; charset=utf-8',
  34. 'Content-Length: ' . strlen($jsonStr)
  35. );
  36. $rs = curl_post($url,$jsonStr,$header);
  37. $rs = json_decode($rs,true);
  38. dump($rs);
  39. if(is_array($rs) && isset($rs['ErrorCode']) && $rs['ErrorCode'] == 0){
  40. $tasklog['status'] = 1;
  41. //正常的,可以下载了
  42. if(isset($rs['File']) && !empty($rs['File'])){
  43. foreach($rs['File'] as $key => $val){
  44. echo $val['URL'];
  45. //下载
  46. $gz_path = $this->downloadfile($val['URL'],$data['ChatType'],$data['MsgTime'].'_'.$data['ChatType'].'.json.gz');
  47. dump($gz_path);
  48. //解压
  49. $json_path = $this->jieyagz($gz_path);
  50. dump($json_path);
  51. //分析
  52. $content = $this->readjson_group($json_path);
  53. dump(count($content));
  54. //入库
  55. if(!empty($content)){
  56. Db::name('imlog_group')->insertAll($content);
  57. }
  58. }
  59. }
  60. }
  61. Db::name('imlog_tasklog')->insertGetId($tasklog);
  62. echo '结束';
  63. exit;
  64. }
  65. //主动拉取im用户私聊 聊天记录
  66. public function auto_imc2c(){
  67. $im_config = config('tencent_im');
  68. $sdkappid = $im_config['sdkappid'];
  69. $sdkappkey = $im_config['key'];
  70. $identifier= $im_config['identifier'];
  71. $usersig = $this->usersig($sdkappid,$sdkappkey,$identifier);
  72. $random = rand(10000000,99999999);
  73. $url = 'https://console.tim.qq.com/v4/open_msg_svc/get_history?sdkappid='.$sdkappid.'&identifier=administrator&usersig='.$usersig.'&random='.$random.'&contenttype=json';
  74. $data = [
  75. 'ChatType' => 'C2C',
  76. 'MsgTime' => date('YmdH',strtotime('-3 Hours')),
  77. ];
  78. $tasklog = [
  79. 'type' => $data['ChatType'] == 'C2C' ? 1 : 2,
  80. 'datehour' => $data['MsgTime'],
  81. 'createtime' => time(),
  82. 'status' => 0,
  83. ];
  84. dump($data);
  85. $jsonStr = json_encode($data);
  86. $header = array(
  87. 'Content-Type: application/json; charset=utf-8',
  88. 'Content-Length: ' . strlen($jsonStr)
  89. );
  90. $rs = curl_post($url,$jsonStr,$header);
  91. $rs = json_decode($rs,true);
  92. dump($rs);
  93. if(is_array($rs) && isset($rs['ErrorCode']) && $rs['ErrorCode'] == 0){
  94. $tasklog['status'] = 1;
  95. //正常的,可以下载了
  96. if(isset($rs['File']) && !empty($rs['File'])){
  97. foreach($rs['File'] as $key => $val){
  98. //下载
  99. $gz_path = $this->downloadfile($val['URL'],$data['ChatType'],$data['MsgTime'].'_'.$data['ChatType'].'.json.gz');
  100. dump($gz_path);
  101. //解压
  102. $json_path = $this->jieyagz($gz_path);
  103. dump($json_path);
  104. //分析
  105. $content = $this->readjson($json_path);
  106. dump(count($content));
  107. //入库
  108. if(!empty($content)){
  109. Db::name('imlog_c2c')->insertAll($content);
  110. }
  111. }
  112. }
  113. }
  114. Db::name('imlog_tasklog')->insertGetId($tasklog);
  115. echo '结束';
  116. exit;
  117. }
  118. //送礼物(api/party/giveGiftToYou)拆分出来的异步用户升级方法,
  119. public function auto_user_level_up(){
  120. Db::startTrans();
  121. $tasklist = Db::name('gift_user_party')->where('task_status',0)->limit(20)->lock(true)->select();
  122. if(empty($tasklist)){
  123. Db::rollback();
  124. echo 'empty';
  125. exit;
  126. }
  127. try {
  128. $getempirical_conf = config("site.getempirical");
  129. foreach($tasklist as $key => $giftuserparty){
  130. echo $giftuserparty['id'].'<br>';
  131. $giftValue = $giftuserparty['value'];
  132. $hotValue = $giftValue;
  133. //用户经验升级
  134. $getempirical = $getempirical_conf * $hotValue;
  135. // 获取用户贵族信息
  136. $noble = \app\common\model\User::getUserNoble($giftuserparty['user_id']);
  137. if(isset($noble["noble_on"]) && $noble["noble_on"] == 1) {
  138. $getempirical = $getempirical + $getempirical * ($noble["explain"]/100);
  139. }
  140. // 增加用户经验值
  141. $res = \app\common\model\User::addEmpirical($giftuserparty['user_id'],$getempirical);
  142. /*if ($res){
  143. $this->auth->level = $res->level;
  144. }*/
  145. //增加被送礼物用户的魅力等级
  146. $res_charm = \app\common\model\User::add_charm_level($giftuserparty['user_to_id'],$giftValue);
  147. // +exp
  148. \app\common\model\TaskLog::tofinish($giftuserparty['user_id'],"OBHqCX4g",$giftuserparty['number']);
  149. // +message
  150. //\app\common\model\Message::addMessage($user_id,"礼物通知","收到 ".$this->auth->nickname." 赠送的".$giftInfo["name"]." x".$number." 价值 ".$giftValue ." 钻石");
  151. //增加送礼用户的财富等级
  152. $res_wealth = \app\common\model\User::add_wealth_level($giftuserparty['user_id'],$giftValue);
  153. Db::name('gift_user_party')->where('id',$giftuserparty['id'])->update(['task_status'=>1]);
  154. }
  155. Db::commit();
  156. } catch (ValidateException $e) {
  157. Db::rollback();
  158. $this->error($e->getMessage());
  159. } catch (PDOException $e) {
  160. Db::rollback();
  161. $this->error($e->getMessage());
  162. } catch (Exception $e) {
  163. Db::rollback();
  164. $this->error($e->getMessage());
  165. }
  166. }
  167. /////////////////////////////////////////下面都是工具方法////////////////////////////////////////////////
  168. //下载远程文件 到指定目录
  169. private function downloadfile($file_url, $path = '', $save_file_name = '')
  170. {
  171. $basepath = '/uploaded/';
  172. if ($path) {
  173. $basepath = $basepath . $path . '/';
  174. }
  175. $basepath = $basepath . date('Ymd');
  176. $dir_path = ROOT_PATH . '/public' . $basepath;
  177. if (!is_dir($dir_path)) {
  178. mkdir($dir_path, 0777, true);
  179. }
  180. $file = file_get_contents($file_url);
  181. //传入保存文件的名称
  182. $filename = $save_file_name ?: pathinfo($file_url, PATHINFO_BASENAME);
  183. $resource = fopen($dir_path. '/'. $filename, 'w');
  184. fwrite($resource, $file);
  185. fclose($resource);
  186. return $dir_path . '/' . $filename;
  187. }
  188. //解压
  189. private function jieyagz($gz_path = ''){
  190. $json_path = substr($gz_path,0,-3);
  191. if ($zp = gzopen($gz_path, 'r')) { // 打开压缩文件
  192. if ($fp = fopen($json_path, 'w')) { // 打开目标文件
  193. while (!gzeof($zp)) {
  194. fwrite($fp, gzread($zp, 1024 * 512)); // 逐块读取和解压缩后写入
  195. }
  196. fclose($fp);
  197. }
  198. gzclose($zp);
  199. }
  200. return $json_path;
  201. }
  202. //读取json并分析,c2c
  203. private function readjson($json_path = ''){
  204. $newMsgList = [];
  205. $json_content = file_get_contents($json_path);
  206. $json_content = json_decode($json_content,true);
  207. if(!empty($json_content) && is_array($json_content) && isset($json_content['MsgList'])){
  208. $MsgList = $json_content['MsgList'];
  209. if(!empty($MsgList)){
  210. foreach($MsgList as $key => $val)
  211. {
  212. $newone = [
  213. 'ClientIP' => isset($val['ClientIP']) ? $val['ClientIP'] : '',
  214. 'CloudCustomData' => isset($val['CloudCustomData']) ? $val['CloudCustomData'] : '',
  215. 'From_Account' => isset($val['From_Account']) ? intval($val['From_Account']) : 0,
  216. //'MsgBody' => isset($val['MsgBody']) ? json_encode($val['MsgBody']) : '',
  217. 'MsgFromPlatform' => isset($val['MsgFromPlatform']) ? $val['MsgFromPlatform'] : '',
  218. 'MsgRandom' => isset($val['MsgRandom']) ? $val['MsgRandom'] : '',
  219. 'MsgSeq' => isset($val['MsgSeq']) ? $val['MsgSeq'] : '',
  220. 'MsgTimestamp' => isset($val['MsgTimestamp']) ? $val['MsgTimestamp'] : '',
  221. 'To_Account' => isset($val['To_Account']) ? intval($val['To_Account']) : 0,
  222. ];
  223. //解析数据类型
  224. if(isset($val['MsgBody'][0]['MsgType'])){
  225. $newone['MsgType'] = $val['MsgBody'][0]['MsgType'];
  226. //文本
  227. if($val['MsgBody'][0]['MsgType'] == 'TIMTextElem'){
  228. $newone['MsgInfo'] = '';
  229. if(isset($val['MsgBody'][0]['MsgContent']['Text'])){
  230. $newone['MsgInfo'] = $val['MsgBody'][0]['MsgContent']['Text'];
  231. }
  232. }
  233. //图片
  234. if($val['MsgBody'][0]['MsgType'] == 'TIMImageElem'){
  235. $newone['MsgInfo'] = '';
  236. if(isset($val['MsgBody'][0]['MsgContent']['ImageInfoArray'][0]['URL'])){
  237. $newone['MsgInfo'] = $val['MsgBody'][0]['MsgContent']['ImageInfoArray'][0]['URL'];
  238. }
  239. }
  240. //声音
  241. if($val['MsgBody'][0]['MsgType'] == 'TIMSoundElem'){
  242. $newone['MsgInfo'] = '';
  243. if(isset($val['MsgBody'][0]['MsgContent']['Url'])){
  244. $newone['MsgInfo'] = $val['MsgBody'][0]['MsgContent']['Url'];
  245. }
  246. }
  247. //视频
  248. if($val['MsgBody'][0]['MsgType'] == 'TIMVideoFileElem'){
  249. $newone['MsgInfo'] = '';
  250. if(isset($val['MsgBody'][0]['MsgContent']['VideoUrl'])){
  251. $newone['MsgInfo'] = $val['MsgBody'][0]['MsgContent']['VideoUrl'];
  252. }
  253. }
  254. //其他
  255. continue;
  256. }else{
  257. continue;
  258. }
  259. $newMsgList[] = $newone;
  260. }
  261. }
  262. }
  263. return $newMsgList;
  264. }
  265. //读取json并分析,group
  266. private function readjson_group($json_path = ''){
  267. $newMsgList = [];
  268. $json_content = file_get_contents($json_path);
  269. $json_content = json_decode($json_content,true);
  270. if(!empty($json_content) && is_array($json_content) && isset($json_content['MsgList'])){
  271. $MsgList = $json_content['MsgList'];
  272. if(!empty($MsgList)){
  273. foreach($MsgList as $key => $val)
  274. {
  275. $newone = [
  276. //'key' => $key,辅助查找
  277. 'ClientIP' => isset($val['ClientIP']) ? $val['ClientIP'] : '',
  278. 'From_Account' => isset($val['From_Account']) ? intval($val['From_Account']) : 0,
  279. 'GroupId' => isset($val['GroupId']) ? intval($val['GroupId']) : 0,
  280. //'MsgBody' => isset($val['MsgBody']) ? json_encode($val['MsgBody']) : '',
  281. 'MsgFromPlatform' => isset($val['MsgFromPlatform']) ? $val['MsgFromPlatform'] : '',
  282. 'MsgSeq' => isset($val['MsgSeq']) ? $val['MsgSeq'] : '',
  283. 'MsgTimestamp' => isset($val['MsgTimestamp']) ? $val['MsgTimestamp'] : '',
  284. ];
  285. //解析数据类型
  286. if(isset($val['MsgBody'][0]['MsgType'])){
  287. $newone['MsgType'] = $val['MsgBody'][0]['MsgType'];
  288. //文本
  289. if($val['MsgBody'][0]['MsgType'] == 'TIMTextElem'){
  290. $newone['MsgInfo'] = '';
  291. if(isset($val['MsgBody'][0]['MsgContent']['Text'])){
  292. //继续解析
  293. $text = json_decode($val['MsgBody'][0]['MsgContent']['Text'],true);
  294. if(isset($text['type']) && $text['type'] == 1){
  295. //TYPE_NORMAL =1;//普通消息
  296. $newone['MsgInfo'] = isset($text['content']) ? $text['content'] : '';
  297. $newMsgList[] = $newone;
  298. }
  299. //其他$text['type']的值都是房间内的,礼物的,表情等,不需要记录
  300. }
  301. }
  302. //房间内的不需要显示的内容
  303. if($val['MsgBody'][0]['MsgType'] == 'TIMCustomElem'){
  304. }
  305. }
  306. //大循环结束
  307. }
  308. }
  309. }
  310. return $newMsgList;
  311. }
  312. //请求im的签名
  313. private function usersig($sdkappid,$key,$identifier){
  314. $api = new TLSSigAPIv2($sdkappid,$key );
  315. $sig = $api->genUserSig($identifier);
  316. return $sig;
  317. }
  318. }