123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- <?php
- namespace app\api\controller;
- use AlibabaCloud\Client\AlibabaCloud;
- use AlibabaCloud\Client\Exception\ClientException;
- use AlibabaCloud\Client\Exception\ServerException;
- use think\cache\driver\Redis;
- use think\Db;
- use think\Exception;
- /**
- * @title 定时任务
- * Class Timedtask
- * @controller Timedtask
- * @group base
- */
- class Timedtask1
- {
- /**
- * @title 取消订单定时任务
- * @desc 未支付的自动取消
- * @author Gavin
- * @url /api/Timedtask/cancelGoodsOrder
- * @method GET
- */
- public function cancelGoodsOrder(){
- $CancelTime = getCancelTime();
- if ($CancelTime<=0){
- die;
- }
- $redis = new Redis();
- $users = $redis->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_time<time()){
- $info['status'] = 2;
- $info['cancel_at'] = date('Y-m-d H:i:s');
- Db::name('store_order')->insert($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_time<time()){
- $info['status'] = 2;
- $info['cancel_at'] = date('Y-m-d H:i:s');
- Db::name('store_order_info_order')->where('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 && $val<time()){
- $redis->del($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 && $val<time()){
- $redis->del($key);
- }
- return $redis->setnx($key,time()+$exptime);
- }
- }
- }
|