hkeys('buyUserInfo'); if ($users){ foreach ($users as &$value){ $key = 'order_not_pay_'.$value; $len = $redis->hGetLen($key); if ($len){ $list = $redis->hGetvals($key); foreach ($list as &$a){ $info = json_decode($a,true); $cancel_time = strtotime($info['create_at'])+($CancelTime*60); if ($cancel_timeinsert($info); //加上库存 addCollectionInventory($info['c_id'],$info['num']); //减少用户购买数量 DecrByCount($info['mid'],$info['c_id'],$info['num']); //删除数据 $redis->hdel($key,$info['order_no']); } } }else{ $redis->hdel('buyUserInfo',$value); } } } } /** * @title 二级市场未支付的自动取消 * @desc 二级市场未支付的自动取消 * @author Gavin * @url /api/Timedtask/SecondaryancelGoodsOrder * @method GET */ public function SecondaryancelGoodsOrder(){ $CancelTime = getConfigValue('secondary_cancel_time'); if ($CancelTime<=0){ die; } $list = Db::name('store_order_info_order')->where('status',0)->select(); foreach ($list as &$v){ $cancel_time = strtotime($v['create_at'])+($CancelTime*60); if ($cancel_timewhere('id',$v['id'])->update($info); $cancle = [ 'mid'=>$v['mid'], 'order_id'=>$v['id'] ]; Db::name('store_order_info_cancel_log')->insert($cancle); $time = date('Y-m-d H:i:s',time()-(60*60)); $count = Db::name('store_order_info_cancel_log')->where('mid',$v['mid'])->where('create_at','gt',$time)->count(); if ($count>2){ $buy_time = date('Y-m-d H:i:s',time()+(24*60*60)); Db::name('store_member')->where('id',$v['mid'])->update(['buy_time'=>$buy_time]); } } } } /** * 藏品铸造hash 定时任务 * @url /api/Timedtask/castingHash */ public function castingHash(){ set_time_limit(0); $redis = new Redis(); $list = Db::name('store_collection')->where('is_deleted',0)->select(); $id = 0; $set_count = 0; foreach ($list as &$v){ $count = $redis->get('castingHash_'.$v['id']); if ($count && $count>0){ $id = $v['id']; $set_count = $count; break; } } if ($id){ $address = '0x3153052307c15c46abb7b3667cdbcd48a7e2a341'; for ($i=0;$i<$set_count;$i++){ if ($this->redisNonceSetNx()){ $str=$id.'-'.rand(100000000,999999999); $url='http://47.111.246.47:8083/ddc/createHashAutoNonce?address='.$address.'&ddcURI='.$str; $res=curlRequest($url); $result=json_decode($res,true); if($result['code']){ continue; }else{ $data['goods_id']=$id; $data['hash']=$res; $data['ddcURI'] = $str; if (Db::name('hash')->insert($data)){ $redis->del('noncenx'); $redis->Decr('castingHash_'.$id); //减一 } } } } } } /** * 铸造hash判断回执 定时任务 * @url /api/Timedtask/transactionReceipt */ public function transactionReceipt(){ set_time_limit(0); Db::name('hash')->where('success',0) ->chunk(50,function ($list){ $redis = new Redis(); foreach ($list as &$v){ $url = 'http://47.111.246.47:8083/ddc/getTransactionReceipt?hash='.$v['hash']; $res=curlRequest($url); Db::name('hash')->where('id',$v['id'])->update(['result'=>$res]); $result3=json_decode($res,true); if (isset($result3['status']) && $result3['status']=='0x1'){ $url4='http://47.111.246.47:8083/ddc/createDdcid?hash='.$v['hash']; $ddcid=curlRequest($url4); $result4=json_decode($ddcid,true); if($result4['code']){ }else{ $update_data['success'] = 1; $update_data['ddcid'] = $ddcid; if (Db::name('hash')->where('id',$v['id'])->update($update_data)){ //存入reids list $redis_data = ['hash'=>$v['hash'],'ddcid'=>$ddcid,'create_at'=>date('Y-m-d H:i:s')]; $redis->rPush('collectionHash_'.$v['goods_id'],json_encode($redis_data)); } } } } },'id', 'asc'); } /** * 发放hash * @url /api/Timedtask/sendHash */ public function sendHash(){ set_time_limit(0); Db::name('store_order_info') ->whereNotNull('company_hash') ->whereIn('status','1,3') ->where('company_hash','neq','') ->where('type',1) ->where('collectors_hash','eq','') ->chunk('20',function ($list){ $from = '0x3153052307c15c46abb7b3667cdbcd48a7e2a341'; $redis = new Redis(); foreach ($list as &$v){ if ($v['status']==1){ $mid = $v['mid']; }elseif ($v['status']==3){ $from = Db::name('store_member')->where('id',$v['to_mid'])->value('wallet_address'); $mid = $v['to_mid']; } $to = Db::name('store_member')->where('id',$mid)->value('wallet_address'); if (empty($to) || $to == ''){ continue; } if ($this->redisNonceSetNx()){ $ddcid = $v['ddcid']; $url = "http://47.111.246.47:8083/ddc/transferAutoNonce?from=$from&to=$to&ddcid=".$ddcid; $res=curlRequest($url); $result=json_decode($res,true); if($result['code']){ continue; }else{ Db::name('store_order_info') ->where('id',$v['id']) ->update(['collectors_hash'=>$res,'collectors_hash_time'=>date('Y-m-d H:i:s')]); $redis->del('noncenx'); } } } },'id','desc'); } /** * @title 创建链账户 * @desc 创建链账户 * @author Gavin * @url /api/Timedtask/createAddress * @method GET */ public function createAddress(){ set_time_limit(0); $member = Db::name('store_member') ->whereNull('wallet_address') ->field('id,offline_account,phone,wallet_address')->order('id asc')->limit(30)->select(); foreach ($member as &$v){ $url = 'http://47.111.246.47:8083/ddc/createAccount'; $offlineaccount = file_get_contents($url); echo $offlineaccount; $offline_account = json_decode($offlineaccount,true); $address = $offline_account['address']; $phone =$v['phone']; //$phone .= rand(10000,99999); $url = "http://47.111.246.47:8083/ddc/createAddress?name=".$phone."&account=".$address; $res=curlRequest($url); $laddress = json_decode($res,true); dump($laddress); if ($laddress['code']==0){ $wallet_address = $laddress['data']['opbChainClientAddress']; Db::name('store_member')->where('id',$v['id']) ->update(['accountName'=>$phone,'wallet_address'=>$wallet_address,'offline_account'=>$offlineaccount]); } } } /** * redis 加锁 */ function redisCreateSetNx($id){ $redis = new Redis(); $key = 'hash_'.$id; $exptime = 450; $is_lock = $redis->setnx($key,time()+$exptime); if ($is_lock){ return true; }else{ //加锁失败的情况下,判断锁是否已经存在,如果存在切已经过期,删除锁,重新加锁 $val = $redis->get($key); if ($val && $valdel($key); } return $redis->setnx($key,time()+$exptime); } } /** * redis nonce加锁 */ function redisNonceSetNx(){ $redis = new Redis(); $key = 'noncenx'; $exptime = 10; $is_lock = $redis->setnx($key,time()+$exptime); if ($is_lock){ return true; }else{ //加锁失败的情况下,判断锁是否已经存在,如果存在切已经过期,删除锁,重新加锁 $val = $redis->get($key); if ($val && $valdel($key); } return $redis->setnx($key,time()+$exptime); } } }