Timedtask1.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 Timedtask1
  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. * @title 二级市场未支付的自动取消
  60. * @desc 二级市场未支付的自动取消
  61. * @author Gavin
  62. * @url /api/Timedtask/SecondaryancelGoodsOrder
  63. * @method GET
  64. */
  65. public function SecondaryancelGoodsOrder(){
  66. $CancelTime = getConfigValue('secondary_cancel_time');
  67. if ($CancelTime<=0){
  68. die;
  69. }
  70. $list = Db::name('store_order_info_order')->where('status',0)->select();
  71. foreach ($list as &$v){
  72. $cancel_time = strtotime($v['create_at'])+($CancelTime*60);
  73. if ($cancel_time<time()){
  74. $info['status'] = 2;
  75. $info['cancel_at'] = date('Y-m-d H:i:s');
  76. Db::name('store_order_info_order')->where('id',$v['id'])->update($info);
  77. $cancle = [
  78. 'mid'=>$v['mid'],
  79. 'order_id'=>$v['id']
  80. ];
  81. Db::name('store_order_info_cancel_log')->insert($cancle);
  82. $time = date('Y-m-d H:i:s',time()-(60*60));
  83. $count = Db::name('store_order_info_cancel_log')->where('mid',$v['mid'])->where('create_at','gt',$time)->count();
  84. if ($count>2){
  85. $buy_time = date('Y-m-d H:i:s',time()+(24*60*60));
  86. Db::name('store_member')->where('id',$v['mid'])->update(['buy_time'=>$buy_time]);
  87. }
  88. }
  89. }
  90. }
  91. /**
  92. * 藏品铸造hash 定时任务
  93. * @url /api/Timedtask/castingHash
  94. */
  95. public function castingHash(){
  96. set_time_limit(0);
  97. $redis = new Redis();
  98. $list = Db::name('store_collection')->where('is_deleted',0)->select();
  99. $id = 0;
  100. $set_count = 0;
  101. foreach ($list as &$v){
  102. $count = $redis->get('castingHash_'.$v['id']);
  103. if ($count && $count>0){
  104. $id = $v['id'];
  105. $set_count = $count;
  106. break;
  107. }
  108. }
  109. if ($id){
  110. $address = '0x3153052307c15c46abb7b3667cdbcd48a7e2a341';
  111. for ($i=0;$i<$set_count;$i++){
  112. if ($this->redisNonceSetNx()){
  113. $str=$id.'-'.rand(100000000,999999999);
  114. $url='http://47.111.246.47:8083/ddc/createHashAutoNonce?address='.$address.'&ddcURI='.$str;
  115. $res=curlRequest($url);
  116. $result=json_decode($res,true);
  117. if($result['code']){
  118. continue;
  119. }else{
  120. $data['goods_id']=$id;
  121. $data['hash']=$res;
  122. $data['ddcURI'] = $str;
  123. if (Db::name('hash')->insert($data)){
  124. $redis->del('noncenx');
  125. $redis->Decr('castingHash_'.$id); //减一
  126. }
  127. }
  128. }
  129. }
  130. }
  131. }
  132. /**
  133. * 铸造hash判断回执 定时任务
  134. * @url /api/Timedtask/transactionReceipt
  135. */
  136. public function transactionReceipt(){
  137. set_time_limit(0);
  138. Db::name('hash')->where('success',0)
  139. ->chunk(50,function ($list){
  140. $redis = new Redis();
  141. foreach ($list as &$v){
  142. $url = 'http://47.111.246.47:8083/ddc/getTransactionReceipt?hash='.$v['hash'];
  143. $res=curlRequest($url);
  144. Db::name('hash')->where('id',$v['id'])->update(['result'=>$res]);
  145. $result3=json_decode($res,true);
  146. if (isset($result3['status']) && $result3['status']=='0x1'){
  147. $url4='http://47.111.246.47:8083/ddc/createDdcid?hash='.$v['hash'];
  148. $ddcid=curlRequest($url4);
  149. $result4=json_decode($ddcid,true);
  150. if($result4['code']){
  151. }else{
  152. $update_data['success'] = 1;
  153. $update_data['ddcid'] = $ddcid;
  154. if (Db::name('hash')->where('id',$v['id'])->update($update_data)){
  155. //存入reids list
  156. $redis_data = ['hash'=>$v['hash'],'ddcid'=>$ddcid,'create_at'=>date('Y-m-d H:i:s')];
  157. $redis->rPush('collectionHash_'.$v['goods_id'],json_encode($redis_data));
  158. }
  159. }
  160. }
  161. }
  162. },'id', 'asc');
  163. }
  164. /**
  165. * 发放hash
  166. * @url /api/Timedtask/sendHash
  167. */
  168. public function sendHash(){
  169. set_time_limit(0);
  170. Db::name('store_order_info')
  171. ->whereNotNull('company_hash')
  172. ->whereIn('status','1,3')
  173. ->where('company_hash','neq','')
  174. ->where('type',1)
  175. ->where('collectors_hash','eq','')
  176. ->chunk('20',function ($list){
  177. $from = '0x3153052307c15c46abb7b3667cdbcd48a7e2a341';
  178. $redis = new Redis();
  179. foreach ($list as &$v){
  180. if ($v['status']==1){
  181. $mid = $v['mid'];
  182. }elseif ($v['status']==3){
  183. $from = Db::name('store_member')->where('id',$v['to_mid'])->value('wallet_address');
  184. $mid = $v['to_mid'];
  185. }
  186. $to = Db::name('store_member')->where('id',$mid)->value('wallet_address');
  187. if (empty($to) || $to == ''){
  188. continue;
  189. }
  190. if ($this->redisNonceSetNx()){
  191. $ddcid = $v['ddcid'];
  192. $url = "http://47.111.246.47:8083/ddc/transferAutoNonce?from=$from&to=$to&ddcid=".$ddcid;
  193. $res=curlRequest($url);
  194. $result=json_decode($res,true);
  195. if($result['code']){
  196. continue;
  197. }else{
  198. Db::name('store_order_info')
  199. ->where('id',$v['id'])
  200. ->update(['collectors_hash'=>$res,'collectors_hash_time'=>date('Y-m-d H:i:s')]);
  201. $redis->del('noncenx');
  202. }
  203. }
  204. }
  205. },'id','desc');
  206. }
  207. /**
  208. * @title 创建链账户
  209. * @desc 创建链账户
  210. * @author Gavin
  211. * @url /api/Timedtask/createAddress
  212. * @method GET
  213. */
  214. public function createAddress(){
  215. set_time_limit(0);
  216. $member = Db::name('store_member')
  217. ->whereNull('wallet_address')
  218. ->field('id,offline_account,phone,wallet_address')->order('id asc')->limit(30)->select();
  219. foreach ($member as &$v){
  220. $url = 'http://47.111.246.47:8083/ddc/createAccount';
  221. $offlineaccount = file_get_contents($url);
  222. echo $offlineaccount;
  223. $offline_account = json_decode($offlineaccount,true);
  224. $address = $offline_account['address'];
  225. $phone =$v['phone'];
  226. //$phone .= rand(10000,99999);
  227. $url = "http://47.111.246.47:8083/ddc/createAddress?name=".$phone."&account=".$address;
  228. $res=curlRequest($url);
  229. $laddress = json_decode($res,true);
  230. dump($laddress);
  231. if ($laddress['code']==0){
  232. $wallet_address = $laddress['data']['opbChainClientAddress'];
  233. Db::name('store_member')->where('id',$v['id'])
  234. ->update(['accountName'=>$phone,'wallet_address'=>$wallet_address,'offline_account'=>$offlineaccount]);
  235. }
  236. }
  237. }
  238. /**
  239. * redis 加锁
  240. */
  241. function redisCreateSetNx($id){
  242. $redis = new Redis();
  243. $key = 'hash_'.$id;
  244. $exptime = 450;
  245. $is_lock = $redis->setnx($key,time()+$exptime);
  246. if ($is_lock){
  247. return true;
  248. }else{
  249. //加锁失败的情况下,判断锁是否已经存在,如果存在切已经过期,删除锁,重新加锁
  250. $val = $redis->get($key);
  251. if ($val && $val<time()){
  252. $redis->del($key);
  253. }
  254. return $redis->setnx($key,time()+$exptime);
  255. }
  256. }
  257. /**
  258. * redis nonce加锁
  259. */
  260. function redisNonceSetNx(){
  261. $redis = new Redis();
  262. $key = 'noncenx';
  263. $exptime = 10;
  264. $is_lock = $redis->setnx($key,time()+$exptime);
  265. if ($is_lock){
  266. return true;
  267. }else{
  268. //加锁失败的情况下,判断锁是否已经存在,如果存在切已经过期,删除锁,重新加锁
  269. $val = $redis->get($key);
  270. if ($val && $val<time()){
  271. $redis->del($key);
  272. }
  273. return $redis->setnx($key,time()+$exptime);
  274. }
  275. }
  276. }