Timedtask.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace app\api\controller;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. use think\cache\driver\Redis;
  7. use think\Db;
  8. use think\Exception;
  9. /**
  10. * @title 定时任务
  11. * Class Timedtask
  12. * @controller Timedtask
  13. * @group base
  14. */
  15. class Timedtask
  16. {
  17. /**
  18. * @title 取消订单定时任务
  19. * @desc 未支付的自动取消
  20. * @author Gavin
  21. * @url /api/Timedtask/cancelGoodsOrder
  22. * @method GET
  23. */
  24. public function cancelGoodsOrder(){
  25. $CancelTime = getCancelTime();
  26. if ($CancelTime<=0){
  27. die;
  28. }
  29. $redis = new Redis();
  30. $users = $redis->hkeys('buyUserInfo');
  31. if ($users){
  32. foreach ($users as &$value){
  33. $key = 'order_not_pay_'.$value;
  34. $len = $redis->hGetLen($key);
  35. if ($len){
  36. $list = $redis->hGetvals($key);
  37. foreach ($list as &$a){
  38. $info = json_decode($a,true);
  39. $cancel_time = strtotime($info['create_at'])+($CancelTime*60);
  40. if ($cancel_time<time()){
  41. $info['status'] = 2;
  42. $info['cancel_at'] = date('Y-m-d H:i:s');
  43. Db::name('store_order')->insert($info);
  44. //加上库存
  45. addCollectionInventory($info['c_id'],$info['num']);
  46. //减少用户购买数量
  47. DecrByCount($info['mid'],$info['c_id'],$info['num']);
  48. //删除数据
  49. $redis->hdel($key,$info['order_no']);
  50. }
  51. }
  52. }else{
  53. $redis->hdel('buyUserInfo',$value);
  54. }
  55. }
  56. }
  57. }
  58. /**
  59. * 藏品铸造hash 定时任务
  60. * @url /api/Timedtask/castingHash
  61. */
  62. public function castingHash(){
  63. set_time_limit(0);
  64. $redis = new Redis();
  65. $list = Db::name('store_collection')->where('is_deleted',0)->select();
  66. $id = 0;
  67. $set_count = 0;
  68. foreach ($list as &$v){
  69. $count = $redis->get('castingHash_'.$v['id']);
  70. if ($count && $count>0){
  71. $id = $v['id'];
  72. $set_count = $count;
  73. break;
  74. }
  75. }
  76. if ($id){
  77. for ($i=0;$i<$set_count;$i++){
  78. $url ="http://47.111.246.47:8088/creatGoods?commodityId=".$id;
  79. $res=curlRequest($url);
  80. $result=json_decode($res,true);
  81. if (isset($result) && $result['code']=='200'){
  82. $data['goods_id']=$id;
  83. $data['hash']=$result['result']['hash'];
  84. if (Db::name('hash')->insert($data)){
  85. $redis->Decr('castingHash_'.$id); //减一
  86. //存入reids list
  87. $redis_data = ['hash'=>$data['hash'],'create_at'=>date('Y-m-d H:i:s')];
  88. $redis->rPush('collectionHash_'.$id,json_encode($redis_data));
  89. }
  90. }
  91. }
  92. }
  93. }
  94. /**
  95. * 发放hash
  96. * @url /api/Timedtask/sendHash
  97. */
  98. public function sendHash(){
  99. set_time_limit(0);
  100. Db::name('store_order_info')
  101. ->whereNotNull('company_hash')
  102. ->whereIn('status','1,3')
  103. ->where('company_hash','neq','')
  104. ->where('collectors_hash','eq','')
  105. ->chunk('20',function ($list){
  106. foreach ($list as &$v){
  107. if ($v['status']==1){
  108. $from = 'wyg';
  109. }elseif ($v['status']==3){
  110. $from = Db::name('store_member')->where('id',$v['to_mid'])->value('accountName');
  111. }
  112. $to = Db::name('store_member')->where('id',$v['mid'])->value('accountName');
  113. $url="http://47.111.246.47:8088/transfer?fromAccountName=$from&commodityHash=".$v['company_hash']."&toAccountName=".$to;
  114. $res =curlRequest($url);
  115. $result=json_decode($res,true);
  116. if (isset($result) && $result['code']=='200'){
  117. Db::name('store_order_info')->where('id',$v['id'])
  118. ->update(['collectors_hash'=>$result['result']['hash'],'collectors_hash_time'=>date('Y-m-d H:i:s')]);
  119. }
  120. }
  121. },'id','desc');
  122. }
  123. /**
  124. * @title 创建链账户
  125. * @desc 创建链账户
  126. * @author Gavin
  127. * @url /api/Timedtask/createAddress
  128. * @method GET
  129. */
  130. public function createAddress(){
  131. set_time_limit(0);
  132. $member = Db::name('store_member')->where('wallet_address','eq','')->field('id,phone,wallet_address')->order('id asc')->limit(30)->select();
  133. foreach ($member as &$v){
  134. $phone = $v['phone'];
  135. $wallet_address = getWalletAddress($phone);
  136. //dump($laddress);
  137. if ($wallet_address){
  138. Db::name('store_member')->where('id',$v['id'])->update(['accountName'=>$phone,'wallet_address'=>$wallet_address]);
  139. }else{
  140. $phone = $v['phone'].rand(10000,99999);
  141. $wallet_address = getWalletAddress($phone);
  142. if ($wallet_address){
  143. Db::name('store_member')->where('id',$v['id'])->update(['accountName'=>$phone,'wallet_address'=>$wallet_address]);
  144. }
  145. }
  146. }
  147. }
  148. }