Plantask.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. if($val['MsgBody'][0]['MsgType'] == 'TIMImageElem'){
  193. $newone['MsgInfo'] = '';
  194. if(isset($val['MsgBody'][0]['MsgContent']['ImageInfoArray'][0]['URL'])){
  195. $newone['MsgInfo'] = $val['MsgBody'][0]['MsgContent']['ImageInfoArray'][0]['URL'];
  196. }
  197. }
  198. //声音
  199. if($val['MsgBody'][0]['MsgType'] == 'TIMSoundElem'){
  200. $newone['MsgInfo'] = '';
  201. if(isset($val['MsgBody'][0]['MsgContent']['Url'])){
  202. $newone['MsgInfo'] = $val['MsgBody'][0]['MsgContent']['Url'];
  203. }
  204. }
  205. //视频
  206. if($val['MsgBody'][0]['MsgType'] == 'TIMVideoFileElem'){
  207. $newone['MsgInfo'] = '';
  208. if(isset($val['MsgBody'][0]['MsgContent']['VideoUrl'])){
  209. $newone['MsgInfo'] = $val['MsgBody'][0]['MsgContent']['VideoUrl'];
  210. }
  211. }
  212. //其他
  213. continue;
  214. }else{
  215. continue;
  216. }
  217. $newMsgList[] = $newone;
  218. }
  219. }
  220. }
  221. return $newMsgList;
  222. }
  223. //读取json并分析,group
  224. private function readjson_group($json_path = ''){
  225. $newMsgList = [];
  226. $json_content = file_get_contents($json_path);
  227. $json_content = json_decode($json_content,true);
  228. if(!empty($json_content) && is_array($json_content) && isset($json_content['MsgList'])){
  229. $MsgList = $json_content['MsgList'];
  230. if(!empty($MsgList)){
  231. foreach($MsgList as $key => $val)
  232. {
  233. $newone = [
  234. //'key' => $key,辅助查找
  235. 'ClientIP' => isset($val['ClientIP']) ? $val['ClientIP'] : '',
  236. 'From_Account' => isset($val['From_Account']) ? intval($val['From_Account']) : 0,
  237. 'GroupId' => isset($val['GroupId']) ? intval($val['GroupId']) : 0,
  238. //'MsgBody' => isset($val['MsgBody']) ? json_encode($val['MsgBody']) : '',
  239. 'MsgFromPlatform' => isset($val['MsgFromPlatform']) ? $val['MsgFromPlatform'] : '',
  240. 'MsgSeq' => isset($val['MsgSeq']) ? $val['MsgSeq'] : '',
  241. 'MsgTimestamp' => isset($val['MsgTimestamp']) ? $val['MsgTimestamp'] : '',
  242. ];
  243. //解析数据类型
  244. if(isset($val['MsgBody'][0]['MsgType'])){
  245. $newone['MsgType'] = $val['MsgBody'][0]['MsgType'];
  246. //文本
  247. if($val['MsgBody'][0]['MsgType'] == 'TIMTextElem'){
  248. $newone['MsgInfo'] = '';
  249. if(isset($val['MsgBody'][0]['MsgContent']['Text'])){
  250. //继续解析
  251. $text = json_decode($val['MsgBody'][0]['MsgContent']['Text'],true);
  252. if(isset($text['type']) && $text['type'] == 1){
  253. //TYPE_NORMAL =1;//普通消息
  254. $newone['MsgInfo'] = isset($text['content']) ? $text['content'] : '';
  255. $newMsgList[] = $newone;
  256. }
  257. //其他$text['type']的值都是房间内的,礼物的,表情等,不需要记录
  258. }
  259. }
  260. //房间内的不需要显示的内容
  261. if($val['MsgBody'][0]['MsgType'] == 'TIMCustomElem'){
  262. }
  263. }
  264. //大循环结束
  265. }
  266. }
  267. }
  268. return $newMsgList;
  269. }
  270. //请求im的签名
  271. private function usersig($sdkappid,$key,$identifier){
  272. $api = new TLSSigAPIv2($sdkappid,$key );
  273. $sig = $api->genUserSig($identifier);
  274. return $sig;
  275. }
  276. ////////////////////////////////////////////////////////
  277. //精秀支付订单支付状态查询。 一分钟一次,没用到
  278. public function auto_pay_queue()
  279. {
  280. // 处理次数
  281. $queue_times = 6;
  282. //查询订单信息 十分钟未处理的
  283. $order_info = Db::name('pay_order')
  284. ->where('queue_status', 1)
  285. ->where('status',0)
  286. ->where('queue_times','<',$queue_times)
  287. ->where('createtime','<',time() - 600)
  288. ->order('queue_times','asc')
  289. ->find();
  290. if (!$order_info){
  291. return '无订单';
  292. }
  293. // 增加处理次数
  294. Db::name('pay_order')->where('id',$order_info['id'])->update(['queue_times' => $order_info['queue_times'] + 1]);
  295. // 查询订单是否支付成功
  296. $pay = new JingXiuPayUtil();
  297. if (!$pay->getOrder($order_info['pay_no'])){
  298. $this->error($pay->getMessage());
  299. }
  300. //PROCESSING=订单待支付,SUCCESS=订单支付成功,TIME_OUT=订单已过期,FAIL=订单支付失败,CLOSE=订单关闭,CALL_FAIL=支付成功未返回
  301. $params = $pay->getData();
  302. if (empty($params['order_status']) || $params['order_status'] != 'SUCCESS' || empty($params['out_trade_no'])) {
  303. $status = $params['order_status'] ?? 'error';
  304. return '支付信息有误'.$status.':'.$params['out_trade_no'];
  305. }
  306. // 开始处理业务逻辑
  307. $pay_no = $params['out_trade_no'];
  308. //查询订单信息
  309. $order_info = Db::name('pay_order')->where('pay_no', $pay_no)->find();
  310. if (!$order_info) {
  311. return '订单信息不存在'.$pay_no;
  312. }
  313. if ($order_info['status'] == 3){
  314. return '订单信息_已删除'.$pay_no;
  315. }
  316. if ($order_info['status'] == 1 || $order_info['status'] == 2) {
  317. return '充值入账更新余额失败_status已更新过'.$pay_no;
  318. }
  319. $extendType = '';
  320. //区分vip 还是 冲金币
  321. $is_g = stripos($pay_no, 'P');
  322. if ($is_g !== false) {
  323. $extendType = 'gold';
  324. } else {
  325. $is_v = stripos($pay_no, 'V');
  326. if ($is_v !== false) {
  327. $extendType = 'vip';
  328. }
  329. }
  330. //status已更新过
  331. $result = Db::name('pay_order')->where('pay_no', $pay_no)->where('status',$order_info['status'])->setField(['status' => 2]);
  332. if (!$result) {
  333. return '充值入账更新余额失败_status更新状态2不成功'.$pay_no;
  334. }
  335. //你可以在此编写订单逻辑
  336. $payEvent = Db::name('pay_event')->where('pay_no', $pay_no)->find();
  337. $args = json_decode($payEvent['args'] ?? '', true);
  338. $rechargeM = new \app\common\model\Recharge();
  339. if ($extendType == 'gold') {
  340. $payRes = $rechargeM->goldpaysucc($pay_no, $args);
  341. $payTypeStr = '充值';
  342. } elseif ($extendType == 'vip') {
  343. $payRes = $rechargeM->vippaysucc($pay_no, $args);
  344. $payTypeStr = 'vip';
  345. } else {
  346. $payRes = false;
  347. $payTypeStr = '未知支付类型';
  348. }
  349. if (!$payRes) {
  350. return '更新失败'.$payTypeStr.$pay_no;
  351. }
  352. return '订单处理成功'.$payTypeStr.$pay_no;
  353. }
  354. //发放代理奖励。没用到
  355. public function issuingreward() {
  356. set_time_limit(0);
  357. //本周开始时间
  358. $week_start_time = strtotime(date('Y-m-d')) - ((date('w')==0?7:date('w'))-1)*86400;
  359. $where['user_id'] = ['gt', 0];
  360. $where['issuingtime'] = ['elt', $week_start_time];
  361. $mt_admin = Db::name('admin');
  362. $list = $mt_admin->field('id, user_id, issuingtime')->where($where)->limit(100)->select();
  363. if (!$list) {
  364. echo 'mei shu ju';
  365. die;
  366. }
  367. //上周开始时间 上周结束时间
  368. $starttime = $week_start_time - 604800;
  369. $endtime = $week_start_time - 1;
  370. $money_map['log_type'] = ['in', [21, 22, 23, 54, 60, 82]];
  371. $money_map['createtime'] = ['between', [$starttime, $endtime]];
  372. $mt_user_money_log = Db::name('user_money_log');
  373. $mt_user = Db::name('user');
  374. $mt_extend_reward = Db::name('extend_reward');
  375. $issuingtime = time();
  376. foreach ($list as $k => &$v) {
  377. //开启事务
  378. Db::startTrans();
  379. //修改记录状态
  380. $rs = $mt_admin->where(['id' => $v['id'], 'issuingtime' => $v['issuingtime']])->setField('issuingtime', $issuingtime);
  381. if (!$rs) {
  382. Db::rollback();
  383. continue;
  384. }
  385. $user_ids = $mt_user->where(['intro_uid' => $v['user_id']])->column('id');
  386. if ($user_ids) {
  387. $money_map['user_id'] = ['in', $user_ids];
  388. //周收益
  389. $week_sum_money = $mt_user_money_log->where($money_map)->sum('change_value');
  390. $week_sum_money = $week_sum_money > 0 ? $week_sum_money : 0;
  391. if ($week_sum_money) {
  392. //查询推广奖励等级
  393. $extend_reward_info = $mt_extend_reward->where(['total_flow' => ['elt', $week_sum_money]])->order('id desc')->find();
  394. if ($extend_reward_info) {
  395. //额外提成
  396. $extra_moeny = number_format($week_sum_money * $extend_reward_info['extra_reward'] / 100, 2, '.', '');
  397. if ($extra_moeny > 0) {
  398. //发放额外提出
  399. $wallet_rs = model('wallet')->lockChangeAccountRemain($v['user_id'],0,'money',$extra_moeny,91,'额外提成奖励');
  400. if($wallet_rs['status'] === false){
  401. Db::rollback();
  402. } else {
  403. //发送系统消息
  404. \app\common\model\Message::addMessage($v['user_id'],'额外提成奖励','上周额外提成奖励已经发放');
  405. }
  406. }
  407. }
  408. }
  409. }
  410. Db::commit();
  411. }
  412. echo 'wan bi';
  413. die;
  414. }
  415. //自动推荐。 没用到
  416. public function auto_userRecommend()
  417. {
  418. $payMoney = config('site.recommend_pay_money');//财富值
  419. $getMoney = config('site.recommend_get_money');//魅力值
  420. if ($payMoney <= 0 && $getMoney <= 0) {
  421. echo 'empty';exit;
  422. }
  423. $where = 'u.is_recommend = 0 ';
  424. $whereAppend = '';
  425. if ($payMoney > 0) {
  426. $whereAppend = $where.' and uw.pay_money >= '.$payMoney;
  427. }
  428. if ($getMoney > 0) {
  429. $whereAppend = $where.' and uw.get_money >= '.$getMoney;
  430. }
  431. if ($payMoney >0 && $getMoney >0) {
  432. $whereAppend = $where. ' and (uw.pay_money >= '.$payMoney.' or uw.get_money >= '.$getMoney.')' ;
  433. }
  434. $user = Db::name('user')->alias('u')->field('u.id')
  435. ->join('user_wallet uw','uw.user_id = u.id','LEFT')
  436. ->where($whereAppend)
  437. ->select();
  438. if (empty($user)) {
  439. echo 'update 0';exit;
  440. }
  441. $userIds = array_column($user,'id');
  442. $userWhere['id'] = ['in',$userIds];
  443. $userUpdateData['is_recommend'] = 1;
  444. $userUpdateData['updatetime'] = time();
  445. $userRes = Db::name('user')->where($userWhere)->update($userUpdateData);
  446. echo 'update '.$userRes;exit;
  447. }
  448. }