123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- <?php
- namespace app\index\controller;
- //use app\utils\JingXiu\JingXiuPayUtil;
- use think\Controller;
- use think\Db;
- use app\common\library\Tlssigapiv2;
- class Plantask extends Controller
- {
- //主动拉取im群组内 聊天记录。没用到
- public function auto_imgroup(){
- $im_config = config('tencent_im');
- $sdkappid = $im_config['sdkappid'];
- $sdkappkey = $im_config['key'];
- $identifier= $im_config['identifier'];
- $usersig = $this->usersig($sdkappid,$sdkappkey,$identifier);
- $random = rand(10000000,99999999);
- $url = 'https://console.tim.qq.com/v4/open_msg_svc/get_history?sdkappid='.$sdkappid.'&identifier=administrator&usersig='.$usersig.'&random='.$random.'&contenttype=json';
- $data = [
- 'ChatType' => 'Group',
- 'MsgTime' => date('YmdH',strtotime('-3 Hours')),
- ];
- $tasklog = [
- 'type' => $data['ChatType'] == 'C2C' ? 1 : 2,
- 'datehour' => $data['MsgTime'],
- 'createtime' => time(),
- 'status' => 0,
- ];
- dump($data);
- $jsonStr = json_encode($data);
- $header = array(
- 'Content-Type: application/json; charset=utf-8',
- 'Content-Length: ' . strlen($jsonStr)
- );
- $rs = curl_post($url,$jsonStr,$header);
- $rs = json_decode($rs,true);
- dump($rs);
- if(is_array($rs) && isset($rs['ErrorCode']) && $rs['ErrorCode'] == 0){
- $tasklog['status'] = 1;
- //正常的,可以下载了
- if(isset($rs['File']) && !empty($rs['File'])){
- foreach($rs['File'] as $key => $val){
- echo $val['URL'];
- //下载
- $gz_path = $this->downloadfile($val['URL'],$data['ChatType'],$data['MsgTime'].'_'.$data['ChatType'].'.json.gz');
- dump($gz_path);
- //解压
- $json_path = $this->jieyagz($gz_path);
- dump($json_path);
- //分析
- $content = $this->readjson_group($json_path);
- dump(count($content));
- //入库
- if(!empty($content)){
- Db::name('imlog_group')->insertAll($content);
- }
- }
- }
- }
- Db::name('imlog_tasklog')->insertGetId($tasklog);
- echo '结束';
- exit;
- }
- //主动拉取im用户私聊 聊天记录
- public function auto_imc2c(){
- $im_config = config('tencent_im');
- $sdkappid = $im_config['sdkappid'];
- $sdkappkey = $im_config['key'];
- $identifier= $im_config['identifier'];
- $usersig = $this->usersig($sdkappid,$sdkappkey,$identifier);
- $random = rand(10000000,99999999);
- $url = 'https://console.tim.qq.com/v4/open_msg_svc/get_history?sdkappid='.$sdkappid.'&identifier=administrator&usersig='.$usersig.'&random='.$random.'&contenttype=json';
- $data = [
- 'ChatType' => 'C2C',
- 'MsgTime' => date('YmdH',strtotime('-3 Hours')),
- ];
- $tasklog = [
- 'type' => $data['ChatType'] == 'C2C' ? 1 : 2,
- 'datehour' => $data['MsgTime'],
- 'createtime' => time(),
- 'status' => 0,
- ];
- dump($data);
- $jsonStr = json_encode($data);
- $header = array(
- 'Content-Type: application/json; charset=utf-8',
- 'Content-Length: ' . strlen($jsonStr)
- );
- $rs = curl_post($url,$jsonStr,$header);
- $rs = json_decode($rs,true);
- dump($rs);
- if(is_array($rs) && isset($rs['ErrorCode']) && $rs['ErrorCode'] == 0){
- $tasklog['status'] = 1;
- //正常的,可以下载了
- if(isset($rs['File']) && !empty($rs['File'])){
- foreach($rs['File'] as $key => $val){
- //下载
- $gz_path = $this->downloadfile($val['URL'],$data['ChatType'],$data['MsgTime'].'_'.$data['ChatType'].'.json.gz');
- dump($gz_path);
- //解压
- $json_path = $this->jieyagz($gz_path);
- dump($json_path);
- //分析
- $content = $this->readjson($json_path);
- dump(count($content));
- //入库
- if(!empty($content)){
- Db::name('imlog_c2c')->insertAll($content);
- }
- }
- }
- }
- Db::name('imlog_tasklog')->insertGetId($tasklog);
- echo '结束';
- exit;
- }
- //定时跑用户活跃,改成离线。 一分钟一次
- public function auto_user_active()
- {
- $start_time = time() - (3600 * 24);
- $end_time = time() - (3600 * 2);
- $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})";
- db()->query($sql);
- }
- ////////////////////////////////////////////////////////
- /////////////////////////////////////////下面都是工具方法////////////////////////////////////////////////
- //下载远程文件 到指定目录
- private function downloadfile($file_url, $path = '', $save_file_name = '')
- {
- $basepath = '/uploaded/';
- if ($path) {
- $basepath = $basepath . $path . '/';
- }
- $basepath = $basepath . date('Ymd');
- $dir_path = ROOT_PATH . '/public' . $basepath;
- if (!is_dir($dir_path)) {
- mkdir($dir_path, 0777, true);
- }
- $file = file_get_contents($file_url);
- //传入保存文件的名称
- $filename = $save_file_name ?: pathinfo($file_url, PATHINFO_BASENAME);
- $resource = fopen($dir_path. '/'. $filename, 'w');
- fwrite($resource, $file);
- fclose($resource);
- return $dir_path . '/' . $filename;
- }
- //解压
- private function jieyagz($gz_path = ''){
- $json_path = substr($gz_path,0,-3);
- if ($zp = gzopen($gz_path, 'r')) { // 打开压缩文件
- if ($fp = fopen($json_path, 'w')) { // 打开目标文件
- while (!gzeof($zp)) {
- fwrite($fp, gzread($zp, 1024 * 512)); // 逐块读取和解压缩后写入
- }
- fclose($fp);
- }
- gzclose($zp);
- }
- return $json_path;
- }
- //读取json并分析,c2c
- private function readjson($json_path = ''){
- $newMsgList = [];
- $json_content = file_get_contents($json_path);
- $json_content = json_decode($json_content,true);
- if(!empty($json_content) && is_array($json_content) && isset($json_content['MsgList'])){
- $MsgList = $json_content['MsgList'];
- if(!empty($MsgList)){
- foreach($MsgList as $key => $val)
- {
- $newone = [
- 'ClientIP' => isset($val['ClientIP']) ? $val['ClientIP'] : '',
- 'CloudCustomData' => isset($val['CloudCustomData']) ? $val['CloudCustomData'] : '',
- 'From_Account' => isset($val['From_Account']) ? intval($val['From_Account']) : 0,
- //'MsgBody' => isset($val['MsgBody']) ? json_encode($val['MsgBody']) : '',
- 'MsgFromPlatform' => isset($val['MsgFromPlatform']) ? $val['MsgFromPlatform'] : '',
- 'MsgRandom' => isset($val['MsgRandom']) ? $val['MsgRandom'] : '',
- 'MsgSeq' => isset($val['MsgSeq']) ? $val['MsgSeq'] : '',
- 'MsgTimestamp' => isset($val['MsgTimestamp']) ? $val['MsgTimestamp'] : '',
- 'To_Account' => isset($val['To_Account']) ? intval($val['To_Account']) : 0,
- ];
- //解析数据类型
- if(isset($val['MsgBody'][0]['MsgType'])){
- $newone['MsgType'] = $val['MsgBody'][0]['MsgType'];
- //文本
- if($val['MsgBody'][0]['MsgType'] == 'TIMTextElem'){
- $newone['MsgInfo'] = '';
- if(isset($val['MsgBody'][0]['MsgContent']['Text'])){
- $newone['MsgInfo'] = $val['MsgBody'][0]['MsgContent']['Text'];
- }
- }
- //自定义
- elseif($val['MsgBody'][0]['MsgType'] == 'TIMCustomElem'){
- $newone['MsgInfo'] = '送礼物消息';
- if(isset($val['MsgBody'][0]['MsgContent']['Desc']) && !empty($val['MsgBody'][0]['MsgContent']['Desc'])){
- $newone['MsgInfo'] = $val['MsgBody'][0]['MsgContent']['Desc'];
- }
- }
- //图片
- elseif($val['MsgBody'][0]['MsgType'] == 'TIMImageElem'){
- $newone['MsgInfo'] = '';
- if(isset($val['MsgBody'][0]['MsgContent']['ImageInfoArray'][0]['URL'])){
- $newone['MsgInfo'] = $val['MsgBody'][0]['MsgContent']['ImageInfoArray'][0]['URL'];
- }
- }
- //声音
- elseif($val['MsgBody'][0]['MsgType'] == 'TIMSoundElem'){
- $newone['MsgInfo'] = '';
- if(isset($val['MsgBody'][0]['MsgContent']['Url'])){
- $newone['MsgInfo'] = $val['MsgBody'][0]['MsgContent']['Url'];
- }
- }
- //视频
- elseif($val['MsgBody'][0]['MsgType'] == 'TIMVideoFileElem'){
- $newone['MsgInfo'] = '';
- if(isset($val['MsgBody'][0]['MsgContent']['VideoUrl'])){
- $newone['MsgInfo'] = $val['MsgBody'][0]['MsgContent']['VideoUrl'];
- }
- }else{
- //其他
- continue;
- }
- }else{
- continue;
- }
- $newMsgList[] = $newone;
- }
- }
- }
- return $newMsgList;
- }
- //读取json并分析,group
- private function readjson_group($json_path = ''){
- $newMsgList = [];
- $json_content = file_get_contents($json_path);
- $json_content = json_decode($json_content,true);
- if(!empty($json_content) && is_array($json_content) && isset($json_content['MsgList'])){
- $MsgList = $json_content['MsgList'];
- if(!empty($MsgList)){
- foreach($MsgList as $key => $val)
- {
- $newone = [
- //'key' => $key,辅助查找
- 'ClientIP' => isset($val['ClientIP']) ? $val['ClientIP'] : '',
- 'From_Account' => isset($val['From_Account']) ? intval($val['From_Account']) : 0,
- 'GroupId' => isset($val['GroupId']) ? intval($val['GroupId']) : 0,
- //'MsgBody' => isset($val['MsgBody']) ? json_encode($val['MsgBody']) : '',
- 'MsgFromPlatform' => isset($val['MsgFromPlatform']) ? $val['MsgFromPlatform'] : '',
- 'MsgSeq' => isset($val['MsgSeq']) ? $val['MsgSeq'] : '',
- 'MsgTimestamp' => isset($val['MsgTimestamp']) ? $val['MsgTimestamp'] : '',
- ];
- //解析数据类型
- if(isset($val['MsgBody'][0]['MsgType'])){
- $newone['MsgType'] = $val['MsgBody'][0]['MsgType'];
- //文本
- if($val['MsgBody'][0]['MsgType'] == 'TIMTextElem'){
- $newone['MsgInfo'] = '';
- if(isset($val['MsgBody'][0]['MsgContent']['Text'])){
- //继续解析
- $text = json_decode($val['MsgBody'][0]['MsgContent']['Text'],true);
- if(isset($text['type']) && $text['type'] == 1){
- //TYPE_NORMAL =1;//普通消息
- $newone['MsgInfo'] = isset($text['content']) ? $text['content'] : '';
- $newMsgList[] = $newone;
- }
- //其他$text['type']的值都是房间内的,礼物的,表情等,不需要记录
- }
- }
- //房间内的不需要显示的内容
- if($val['MsgBody'][0]['MsgType'] == 'TIMCustomElem'){
- }
- }
- //大循环结束
- }
- }
- }
- return $newMsgList;
- }
- //请求im的签名
- private function usersig($sdkappid,$key,$identifier){
- $api = new TLSSigAPIv2($sdkappid,$key );
- $sig = $api->genUserSig($identifier);
- return $sig;
- }
- ////////////////////////////////////////////////////////
- //精秀支付订单支付状态查询。 一分钟一次,没用到
- public function auto_pay_queue()
- {
- // 处理次数
- $queue_times = 6;
- //查询订单信息 十分钟未处理的
- $order_info = Db::name('pay_order')
- ->where('queue_status', 1)
- ->where('status',0)
- ->where('queue_times','<',$queue_times)
- ->where('createtime','<',time() - 600)
- ->order('queue_times','asc')
- ->find();
- if (!$order_info){
- return '无订单';
- }
- // 增加处理次数
- Db::name('pay_order')->where('id',$order_info['id'])->update(['queue_times' => $order_info['queue_times'] + 1]);
- // 查询订单是否支付成功
- $pay = new JingXiuPayUtil();
- if (!$pay->getOrder($order_info['pay_no'])){
- $this->error($pay->getMessage());
- }
- //PROCESSING=订单待支付,SUCCESS=订单支付成功,TIME_OUT=订单已过期,FAIL=订单支付失败,CLOSE=订单关闭,CALL_FAIL=支付成功未返回
- $params = $pay->getData();
- if (empty($params['order_status']) || $params['order_status'] != 'SUCCESS' || empty($params['out_trade_no'])) {
- $status = $params['order_status'] ?? 'error';
- return '支付信息有误'.$status.':'.$params['out_trade_no'];
- }
- // 开始处理业务逻辑
- $pay_no = $params['out_trade_no'];
- //查询订单信息
- $order_info = Db::name('pay_order')->where('pay_no', $pay_no)->find();
- if (!$order_info) {
- return '订单信息不存在'.$pay_no;
- }
- if ($order_info['status'] == 3){
- return '订单信息_已删除'.$pay_no;
- }
- if ($order_info['status'] == 1 || $order_info['status'] == 2) {
- return '充值入账更新余额失败_status已更新过'.$pay_no;
- }
- $extendType = '';
- //区分vip 还是 冲金币
- $is_g = stripos($pay_no, 'P');
- if ($is_g !== false) {
- $extendType = 'gold';
- } else {
- $is_v = stripos($pay_no, 'V');
- if ($is_v !== false) {
- $extendType = 'vip';
- }
- }
- //status已更新过
- $result = Db::name('pay_order')->where('pay_no', $pay_no)->where('status',$order_info['status'])->setField(['status' => 2]);
- if (!$result) {
- return '充值入账更新余额失败_status更新状态2不成功'.$pay_no;
- }
- //你可以在此编写订单逻辑
- $payEvent = Db::name('pay_event')->where('pay_no', $pay_no)->find();
- $args = json_decode($payEvent['args'] ?? '', true);
- $rechargeM = new \app\common\model\Recharge();
- if ($extendType == 'gold') {
- $payRes = $rechargeM->goldpaysucc($pay_no, $args);
- $payTypeStr = '充值';
- } elseif ($extendType == 'vip') {
- $payRes = $rechargeM->vippaysucc($pay_no, $args);
- $payTypeStr = 'vip';
- } else {
- $payRes = false;
- $payTypeStr = '未知支付类型';
- }
- if (!$payRes) {
- return '更新失败'.$payTypeStr.$pay_no;
- }
- return '订单处理成功'.$payTypeStr.$pay_no;
- }
- //发放代理奖励。没用到
- public function issuingreward() {
- set_time_limit(0);
- //本周开始时间
- $week_start_time = strtotime(date('Y-m-d')) - ((date('w')==0?7:date('w'))-1)*86400;
- $where['user_id'] = ['gt', 0];
- $where['issuingtime'] = ['elt', $week_start_time];
- $mt_admin = Db::name('admin');
- $list = $mt_admin->field('id, user_id, issuingtime')->where($where)->limit(100)->select();
- if (!$list) {
- echo 'mei shu ju';
- die;
- }
- //上周开始时间 上周结束时间
- $starttime = $week_start_time - 604800;
- $endtime = $week_start_time - 1;
- $money_map['log_type'] = ['in', [21, 22, 23, 54, 60, 82]];
- $money_map['createtime'] = ['between', [$starttime, $endtime]];
- $mt_user_money_log = Db::name('user_money_log');
- $mt_user = Db::name('user');
- $mt_extend_reward = Db::name('extend_reward');
- $issuingtime = time();
- foreach ($list as $k => &$v) {
- //开启事务
- Db::startTrans();
- //修改记录状态
- $rs = $mt_admin->where(['id' => $v['id'], 'issuingtime' => $v['issuingtime']])->setField('issuingtime', $issuingtime);
- if (!$rs) {
- Db::rollback();
- continue;
- }
- $user_ids = $mt_user->where(['intro_uid' => $v['user_id']])->column('id');
- if ($user_ids) {
- $money_map['user_id'] = ['in', $user_ids];
- //周收益
- $week_sum_money = $mt_user_money_log->where($money_map)->sum('change_value');
- $week_sum_money = $week_sum_money > 0 ? $week_sum_money : 0;
- if ($week_sum_money) {
- //查询推广奖励等级
- $extend_reward_info = $mt_extend_reward->where(['total_flow' => ['elt', $week_sum_money]])->order('id desc')->find();
- if ($extend_reward_info) {
- //额外提成
- $extra_moeny = number_format($week_sum_money * $extend_reward_info['extra_reward'] / 100, 2, '.', '');
- if ($extra_moeny > 0) {
- //发放额外提出
- $wallet_rs = model('wallet')->lockChangeAccountRemain($v['user_id'],0,'money',$extra_moeny,91,'额外提成奖励');
- if($wallet_rs['status'] === false){
- Db::rollback();
- } else {
- //发送系统消息
- \app\common\model\Message::addMessage($v['user_id'],'额外提成奖励','上周额外提成奖励已经发放');
- }
- }
- }
- }
- }
- Db::commit();
- }
- echo 'wan bi';
- die;
- }
- //自动推荐。 没用到
- public function auto_userRecommend()
- {
- $payMoney = config('site.recommend_pay_money');//财富值
- $getMoney = config('site.recommend_get_money');//魅力值
- if ($payMoney <= 0 && $getMoney <= 0) {
- echo 'empty';exit;
- }
- $where = 'u.is_recommend = 0 ';
- $whereAppend = '';
- if ($payMoney > 0) {
- $whereAppend = $where.' and uw.pay_money >= '.$payMoney;
- }
- if ($getMoney > 0) {
- $whereAppend = $where.' and uw.get_money >= '.$getMoney;
- }
- if ($payMoney >0 && $getMoney >0) {
- $whereAppend = $where. ' and (uw.pay_money >= '.$payMoney.' or uw.get_money >= '.$getMoney.')' ;
- }
- $user = Db::name('user')->alias('u')->field('u.id')
- ->join('user_wallet uw','uw.user_id = u.id','LEFT')
- ->where($whereAppend)
- ->select();
- if (empty($user)) {
- echo 'update 0';exit;
- }
- $userIds = array_column($user,'id');
- $userWhere['id'] = ['in',$userIds];
- $userUpdateData['is_recommend'] = 1;
- $userUpdateData['updatetime'] = time();
- $userRes = Db::name('user')->where($userWhere)->update($userUpdateData);
- echo 'update '.$userRes;exit;
- }
- }
|