Plantask.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. //主动拉取im群组内 聊天记录。没用到
  10. public function auto_imgroup(){
  11. $im_config = config('tencent_im');
  12. $sdkappid = $im_config['sdkappid'];
  13. $sdkappkey = $im_config['key'];
  14. $identifier= $im_config['identifier'];
  15. $usersig = $this->usersig($sdkappid,$sdkappkey,$identifier);
  16. $random = rand(10000000,99999999);
  17. $url = 'https://console.tim.qq.com/v4/open_msg_svc/get_history?sdkappid='.$sdkappid.'&identifier=administrator&usersig='.$usersig.'&random='.$random.'&contenttype=json';
  18. $data = [
  19. 'ChatType' => 'Group',
  20. 'MsgTime' => date('YmdH',strtotime('-3 Hours')),
  21. ];
  22. $tasklog = [
  23. 'type' => $data['ChatType'] == 'C2C' ? 1 : 2,
  24. 'datehour' => $data['MsgTime'],
  25. 'createtime' => time(),
  26. 'status' => 0,
  27. ];
  28. dump($data);
  29. $jsonStr = json_encode($data);
  30. $header = array(
  31. 'Content-Type: application/json; charset=utf-8',
  32. 'Content-Length: ' . strlen($jsonStr)
  33. );
  34. $rs = curl_post($url,$jsonStr,$header);
  35. $rs = json_decode($rs,true);
  36. dump($rs);
  37. if(is_array($rs) && isset($rs['ErrorCode']) && $rs['ErrorCode'] == 0){
  38. $tasklog['status'] = 1;
  39. //正常的,可以下载了
  40. if(isset($rs['File']) && !empty($rs['File'])){
  41. foreach($rs['File'] as $key => $val){
  42. echo $val['URL'];
  43. //下载
  44. $gz_path = $this->downloadfile($val['URL'],$data['ChatType'],$data['MsgTime'].'_'.$data['ChatType'].'.json.gz');
  45. dump($gz_path);
  46. //解压
  47. $json_path = $this->jieyagz($gz_path);
  48. dump($json_path);
  49. //分析
  50. $content = $this->readjson_group($json_path);
  51. dump(count($content));
  52. //入库
  53. if(!empty($content)){
  54. Db::name('imlog_group')->insertAll($content);
  55. }
  56. }
  57. }
  58. }
  59. Db::name('imlog_tasklog')->insertGetId($tasklog);
  60. echo '结束';
  61. exit;
  62. }
  63. //主动拉取im用户私聊 聊天记录
  64. public function auto_imc2c(){
  65. $im_config = config('tencent_im');
  66. $sdkappid = $im_config['sdkappid'];
  67. $sdkappkey = $im_config['key'];
  68. $identifier= $im_config['identifier'];
  69. $usersig = $this->usersig($sdkappid,$sdkappkey,$identifier);
  70. $random = rand(10000000,99999999);
  71. $url = 'https://console.tim.qq.com/v4/open_msg_svc/get_history?sdkappid='.$sdkappid.'&identifier=administrator&usersig='.$usersig.'&random='.$random.'&contenttype=json';
  72. $data = [
  73. 'ChatType' => 'C2C',
  74. 'MsgTime' => date('YmdH',strtotime('-3 Hours')),
  75. ];
  76. $tasklog = [
  77. 'type' => $data['ChatType'] == 'C2C' ? 1 : 2,
  78. 'datehour' => $data['MsgTime'],
  79. 'createtime' => time(),
  80. 'status' => 0,
  81. ];
  82. dump($data);
  83. $jsonStr = json_encode($data);
  84. $header = array(
  85. 'Content-Type: application/json; charset=utf-8',
  86. 'Content-Length: ' . strlen($jsonStr)
  87. );
  88. $rs = curl_post($url,$jsonStr,$header);
  89. $rs = json_decode($rs,true);
  90. dump($rs);
  91. if(is_array($rs) && isset($rs['ErrorCode']) && $rs['ErrorCode'] == 0){
  92. $tasklog['status'] = 1;
  93. //正常的,可以下载了
  94. if(isset($rs['File']) && !empty($rs['File'])){
  95. foreach($rs['File'] as $key => $val){
  96. //下载
  97. $gz_path = $this->downloadfile($val['URL'],$data['ChatType'],$data['MsgTime'].'_'.$data['ChatType'].'.json.gz');
  98. dump($gz_path);
  99. //解压
  100. $json_path = $this->jieyagz($gz_path);
  101. dump($json_path);
  102. //分析
  103. $content = $this->readjson($json_path);
  104. dump(count($content));
  105. //入库
  106. if(!empty($content)){
  107. Db::name('imlog_c2c')->insertAll($content);
  108. }
  109. }
  110. }
  111. }
  112. Db::name('imlog_tasklog')->insertGetId($tasklog);
  113. echo '结束';
  114. exit;
  115. }
  116. //定时跑用户活跃,改成离线。 一分钟一次
  117. public function auto_user_active()
  118. {
  119. $start_time = time() - (3600 * 24);
  120. $end_time = time() - (3600 * 2);
  121. $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})";
  122. db()->query($sql);
  123. }
  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. //精秀支付订单支付状态查询。 一分钟一次,没用到
  286. public function auto_pay_queue()
  287. {
  288. // 处理次数
  289. $queue_times = 6;
  290. //查询订单信息 十分钟未处理的
  291. $order_info = Db::name('pay_order')
  292. ->where('queue_status', 1)
  293. ->where('status',0)
  294. ->where('queue_times','<',$queue_times)
  295. ->where('createtime','<',time() - 600)
  296. ->order('queue_times','asc')
  297. ->find();
  298. if (!$order_info){
  299. return '无订单';
  300. }
  301. // 增加处理次数
  302. Db::name('pay_order')->where('id',$order_info['id'])->update(['queue_times' => $order_info['queue_times'] + 1]);
  303. // 查询订单是否支付成功
  304. $pay = new JingXiuPayUtil();
  305. if (!$pay->getOrder($order_info['pay_no'])){
  306. $this->error($pay->getMessage());
  307. }
  308. //PROCESSING=订单待支付,SUCCESS=订单支付成功,TIME_OUT=订单已过期,FAIL=订单支付失败,CLOSE=订单关闭,CALL_FAIL=支付成功未返回
  309. $params = $pay->getData();
  310. if (empty($params['order_status']) || $params['order_status'] != 'SUCCESS' || empty($params['out_trade_no'])) {
  311. $status = $params['order_status'] ?? 'error';
  312. return '支付信息有误'.$status.':'.$params['out_trade_no'];
  313. }
  314. // 开始处理业务逻辑
  315. $pay_no = $params['out_trade_no'];
  316. //查询订单信息
  317. $order_info = Db::name('pay_order')->where('pay_no', $pay_no)->find();
  318. if (!$order_info) {
  319. return '订单信息不存在'.$pay_no;
  320. }
  321. if ($order_info['status'] == 3){
  322. return '订单信息_已删除'.$pay_no;
  323. }
  324. if ($order_info['status'] == 1 || $order_info['status'] == 2) {
  325. return '充值入账更新余额失败_status已更新过'.$pay_no;
  326. }
  327. $extendType = '';
  328. //区分vip 还是 冲金币
  329. $is_g = stripos($pay_no, 'P');
  330. if ($is_g !== false) {
  331. $extendType = 'gold';
  332. } else {
  333. $is_v = stripos($pay_no, 'V');
  334. if ($is_v !== false) {
  335. $extendType = 'vip';
  336. }
  337. }
  338. //status已更新过
  339. $result = Db::name('pay_order')->where('pay_no', $pay_no)->where('status',$order_info['status'])->setField(['status' => 2]);
  340. if (!$result) {
  341. return '充值入账更新余额失败_status更新状态2不成功'.$pay_no;
  342. }
  343. //你可以在此编写订单逻辑
  344. $payEvent = Db::name('pay_event')->where('pay_no', $pay_no)->find();
  345. $args = json_decode($payEvent['args'] ?? '', true);
  346. $rechargeM = new \app\common\model\Recharge();
  347. if ($extendType == 'gold') {
  348. $payRes = $rechargeM->goldpaysucc($pay_no, $args);
  349. $payTypeStr = '充值';
  350. } elseif ($extendType == 'vip') {
  351. $payRes = $rechargeM->vippaysucc($pay_no, $args);
  352. $payTypeStr = 'vip';
  353. } else {
  354. $payRes = false;
  355. $payTypeStr = '未知支付类型';
  356. }
  357. if (!$payRes) {
  358. return '更新失败'.$payTypeStr.$pay_no;
  359. }
  360. return '订单处理成功'.$payTypeStr.$pay_no;
  361. }
  362. //发放代理奖励。没用到
  363. public function issuingreward() {
  364. set_time_limit(0);
  365. //本周开始时间
  366. $week_start_time = strtotime(date('Y-m-d')) - ((date('w')==0?7:date('w'))-1)*86400;
  367. $where['user_id'] = ['gt', 0];
  368. $where['issuingtime'] = ['elt', $week_start_time];
  369. $mt_admin = Db::name('admin');
  370. $list = $mt_admin->field('id, user_id, issuingtime')->where($where)->limit(100)->select();
  371. if (!$list) {
  372. echo 'mei shu ju';
  373. die;
  374. }
  375. //上周开始时间 上周结束时间
  376. $starttime = $week_start_time - 604800;
  377. $endtime = $week_start_time - 1;
  378. $money_map['log_type'] = ['in', [21, 22, 23, 54, 60, 82]];
  379. $money_map['createtime'] = ['between', [$starttime, $endtime]];
  380. $mt_user_money_log = Db::name('user_money_log');
  381. $mt_user = Db::name('user');
  382. $mt_extend_reward = Db::name('extend_reward');
  383. $issuingtime = time();
  384. foreach ($list as $k => &$v) {
  385. //开启事务
  386. Db::startTrans();
  387. //修改记录状态
  388. $rs = $mt_admin->where(['id' => $v['id'], 'issuingtime' => $v['issuingtime']])->setField('issuingtime', $issuingtime);
  389. if (!$rs) {
  390. Db::rollback();
  391. continue;
  392. }
  393. $user_ids = $mt_user->where(['intro_uid' => $v['user_id']])->column('id');
  394. if ($user_ids) {
  395. $money_map['user_id'] = ['in', $user_ids];
  396. //周收益
  397. $week_sum_money = $mt_user_money_log->where($money_map)->sum('change_value');
  398. $week_sum_money = $week_sum_money > 0 ? $week_sum_money : 0;
  399. if ($week_sum_money) {
  400. //查询推广奖励等级
  401. $extend_reward_info = $mt_extend_reward->where(['total_flow' => ['elt', $week_sum_money]])->order('id desc')->find();
  402. if ($extend_reward_info) {
  403. //额外提成
  404. $extra_moeny = number_format($week_sum_money * $extend_reward_info['extra_reward'] / 100, 2, '.', '');
  405. if ($extra_moeny > 0) {
  406. //发放额外提出
  407. $wallet_rs = model('wallet')->lockChangeAccountRemain($v['user_id'],0,'money',$extra_moeny,91,'额外提成奖励');
  408. if($wallet_rs['status'] === false){
  409. Db::rollback();
  410. } else {
  411. //发送系统消息
  412. \app\common\model\Message::addMessage($v['user_id'],'额外提成奖励','上周额外提成奖励已经发放');
  413. }
  414. }
  415. }
  416. }
  417. }
  418. Db::commit();
  419. }
  420. echo 'wan bi';
  421. die;
  422. }
  423. //自动推荐。 没用到
  424. public function auto_userRecommend()
  425. {
  426. $payMoney = config('site.recommend_pay_money');//财富值
  427. $getMoney = config('site.recommend_get_money');//魅力值
  428. if ($payMoney <= 0 && $getMoney <= 0) {
  429. echo 'empty';exit;
  430. }
  431. $where = 'u.is_recommend = 0 ';
  432. $whereAppend = '';
  433. if ($payMoney > 0) {
  434. $whereAppend = $where.' and uw.pay_money >= '.$payMoney;
  435. }
  436. if ($getMoney > 0) {
  437. $whereAppend = $where.' and uw.get_money >= '.$getMoney;
  438. }
  439. if ($payMoney >0 && $getMoney >0) {
  440. $whereAppend = $where. ' and (uw.pay_money >= '.$payMoney.' or uw.get_money >= '.$getMoney.')' ;
  441. }
  442. $user = Db::name('user')->alias('u')->field('u.id')
  443. ->join('user_wallet uw','uw.user_id = u.id','LEFT')
  444. ->where($whereAppend)
  445. ->select();
  446. if (empty($user)) {
  447. echo 'update 0';exit;
  448. }
  449. $userIds = array_column($user,'id');
  450. $userWhere['id'] = ['in',$userIds];
  451. $userUpdateData['is_recommend'] = 1;
  452. $userUpdateData['updatetime'] = time();
  453. $userRes = Db::name('user')->where($userWhere)->update($userUpdateData);
  454. echo 'update '.$userRes;exit;
  455. }
  456. }