123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- <?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 Timedtask
- {
- /**
- * @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){
- if ($this->redisCreateSetNx($id)){
- $account ='1b8f045e1a2ccec82021e20bdac05e4e43e0c44e2d4086b726082b1bab320a0d';
- $addr = Db::name('store_collection')->where('id',$id)->value('cover');
- for ($i=0;$i<$set_count;$i++){
- if ($i<$set_count){
- $url =getIpAddress()."IssueAddr?account=".$account."&nftId=".$id."&tokenUrl=".$addr;
- $res=curlRequest($url);
- $result=json_decode($res,true);
- if (isset($result) && $result['code']=='200'){
- $data['goods_id']=$id;
- $data['hash']=$result['result']['txHash'];
- $data['tokenid'] = $result['result']['tokenId'];
- if (Db::name('hash')->insert($data)){
- //存入reids list
- $redis_data = ['hash'=>$data['hash'],'tokenid'=>$data['tokenid'],'create_at'=>date('Y-m-d H:i:s')];
- $redis->rPush('collectionHash_'.$id,json_encode($redis_data));
- $redis->Decr('castingHash_'.$id); //减一
- }
- }
- }else{
- $redis->del('hash_'.$id);
- }
- }
- }
- }else{
- $redis->del('hash_'.$id);
- }
- }
- /**
- * 发放hash
- * @url /api/Timedtask/sendHash
- */
- public function send_my(){
- var_dump(12313);exit();
- }
- /**
- * 发放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('collectors_hash','eq','')
- ->where('type',1)
- ->chunk(50,function ($list){
- foreach ($list as &$v){
- if ($v['status']==1){
- $from = '1b8f045e1a2ccec82021e20bdac05e4e43e0c44e2d4086b726082b1bab320a0d';
- }elseif ($v['status']==3){
- $from = Db::name('store_member')->where('id',$v['to_mid'])->value('wallet_address');
- }
- $to = Db::name('store_member')->where('id',$v['mid'])->value('wallet_address');
- $url=getIpAddress()."transferFrom?from=".$from."&to=".$to."&tokenId=".$v['ddcid'];
- $res =curlRequest($url);
- $result=json_decode($res,true);
- //dump($result);exit();
- if (isset($result['code']) && $result['code']=='200'){
- Db::name('store_order_info')->where('id',$v['id'])
- ->update(['collectors_hash'=>$result['result']['txHash'],'collectors_hash_time'=>date('Y-m-d H:i:s')]);
- }
- }
- },'id','asc');
- }
- /**
- * @title 创建链账户
- * @desc 创建链账户
- * @author Gavin
- * @url /api/Timedtask/createAddress
- * @method GET
- */
- public function createAddress(){
- set_time_limit(0);
- $member = Db::name('store_member')
- ->where('wallet_address','eq','')
- ->field('id,phone,wallet_address')
- ->order('id asc')
- ->limit(500)
- ->select();
- foreach ($member as &$v){
- $phone = $v['phone'];
- $wallet_address = getWalletAddress($phone);
- if ($wallet_address){
- Db::name('store_member')->where('id',$v['id'])->update(['accountName'=>$phone,'wallet_address'=>$wallet_address]);
- }else{
- $phone = $v['phone'].rand(10000,99999);
- $wallet_address = getWalletAddress($phone);
- if ($wallet_address){
- Db::name('store_member')->where('id',$v['id'])->update(['accountName'=>$phone,'wallet_address'=>$wallet_address]);
- }
- }
- }
- }
- /**
- * redis 加锁
- */
- function redisCreateSetNx($id){
- $redis = new Redis();
- $key = 'hash_'.$id;
- $exptime = 5;
- $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 更新用户钱包地址
- */
- function update_wallet(){
- $member = Db::name('store_member')->field('id,phone')->where('id','>',600)->select();
- $member_id = array();
- foreach ($member as $value){
- $url = '120.76.248.160:8088/queryAccount?accountName='.$value['phone'];
- $res=curlRequest($url);
- if ($res){
- $res = json_decode($res,true);
- if ($res['code']=='400'){
- //$member_id[] = $value['id'];
- Db::name('store_member')->where('id',$value['id'])->update(array('wallet_address'=>''));
- }
- }
- }
- //var_dump($member_id);exit();
- }
- /**
- * @title 商品赋能赠送
- * @desc 商品赋能赠送
- * @author QC
- * @url /api/Timedtask/powerSend
- * @method GET
- */
- public function powerSend()
- {
- set_time_limit(0);
- $list = Db::name('collection_power_send')->where(['is_task'=>0])->order('id asc')->limit(1)->select();
- foreach ($list as $v) {
- Db::name('collection_power_send')->where('id',$v['id'])->update(['is_task'=>1]);
- $goods_info = Db::name('store_collection')->find($v['send_goods']);
- $insert_array = [];
- $where = [];
- $where[] = ['o.c_id','=',$v['coll_id']];
- $where[] = ['o.resale_status','=',1];
- $where[] = ['o.is_destruction','=',1];
- $user_list = Db::name('store_order_info')->alias('o')
- ->field('o.*,u.name,u.phone,u.headimg,u.wallet_address')
- ->leftJoin('store_member u','u.id=o.mid')
- ->where($where)->order('o.id desc')->select();
- foreach ($user_list as $user_coll) {
- $rank = getRanking($v['send_goods']);
- $inventory = Db::name('store_collect')->where('id',$v['send_goods'])->value('inventory');
- $tag = getTag($v['send_goods'],$rank,$inventory);
- saveRanking($v['send_goods']);
- $company = '象链艺术有限公司';
- $hash = getCompanyHash($v['send_goods']);
- $company_hash = $hash['hash'];
- $company_hash_time = $hash['create_at'];
- $tokenid = $hash['tokenid'];
- Db::name('hash')->where('hash',$hash['hash'])->update(['status'=>1]);
- $collectors_hash = '';
- $insert_array[] = [
- 'order_no'=>get_order_sn(),
- 'tag'=>$tag,
- 'mid'=>$user_coll['mid'],
- 'c_id'=>$v['send_goods'],
- 'name'=>$goods_info['name'],
- 'cover'=>$goods_info['cover'],
- 'pro_info'=>json_encode($goods_info,true),
- 'company'=>$company,
- 'company_hash'=>$company_hash ? $company_hash : '',
- 'company_hash_time'=>$company_hash_time ,
- 'ddcid'=>$tokenid,
- 'collectors_hash'=>$collectors_hash,
- //'collectors_hash_time'=>'',
- 'power_id'=>$v['power_id']
- ];
- }
- if(!empty($insert_array)) Db::name('store_order_info')->insertAll($insert_array);
- }
- }
- /**
- * @title 锁定会员多久不能操作[商城或是二级市场3天内未支付订单超过3笔,锁定该会员一天不能操作]
- * @desc 锁定会员多久不能操作[商城或是二级市场3天内未支付订单超过3笔,锁定该会员一天不能操作]
- * @author QC
- * @url /api/Timedtask/lockUser
- * @method GET
- */
- public function lockUser()
- {
- $sel_time = date('Y-m-d H:i:s',strtotime('-3 days'));
- $store_order = Db::name('store_order')
- ->field('mid,count(id) num')
- ->where([['status','=',2],['create_at','> time',$sel_time]])
- ->group('mid')
- ->having('num > 3')
- ->select();
- $secondary_order = Db::name('store_order_info_order')
- ->field('mid,count(id) num')
- ->where([['status','=',2],['create_at','> time',$sel_time]])
- ->group('mid')
- ->having('num > 3')
- ->select();
- if(!empty($store_order)) Db::name('store_member')->whereIn('id',implode(',',array_column($store_order,'mid')))
- ->update(['lock_st'=>time(),'lock_end'=>time()+86400]);
- if(!empty($secondary_order)) Db::name('store_member')->whereIn('id',implode(',',array_column($secondary_order,'mid')))
- ->update(['lock_st'=>time(),'lock_end'=>time()+86400]);
- }
- function collectionDraw($goods_id= 0)
- {
- $goods_list = Db::table('store_collection')->field('id,sell_time')
- ->field('id,sell_time,apply_st,apply_end,draw_num')
- ->where(['status'=>1,'is_task'=>0,'issue_mode'=>2,'is_deleted'=>0])
- ->where('apply_end','< time',date('Y-m-d H:i:s'))
- ->when($goods_id,function ($query)use($goods_id){ if($goods_id > 0) $query->where('id',$goods_id);})
- ->select();
- $redis = new Redis();
- $draw_list = [];
- foreach ($goods_list as $goods_info) {
- Db::table('store_collection')->where('id',$goods_info['id'])->update(['is_task'=>1]);
- if(!$goods_info['draw_num']) continue;
- $apply_list = Db::name('store_collection_draw')->where(['coll_id'=>$goods_info['id'],'is_ballot'=>0])->column('id,code,user_id');
- if(empty($apply_list)) continue;
- shuffle($apply_list);
- $ballot_list = array_slice($apply_list,0,$goods_info['draw_num']);
- $draw_list[$goods_info['id']] = $ballot_list;
- Db::name('store_collection_draw')->where('coll_id',$goods_info['id'])->whereIn('id',array_column($ballot_list,'id'))->update(['is_ballot'=>1]);
- Db::name('store_collection_draw')->where('coll_id',$goods_info['id'])->whereNotIn('id',array_column($ballot_list,'id'))->update(['is_ballot'=>2]);
- $redis->set('lucky_draw_'.$goods_info['id'],json_encode(array_column($ballot_list,'user_id')));
- }
- return $draw_list;
- }
- }
|