Timedtask.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. use function AlibabaCloud\Client\value;
  10. /**
  11. * @title 定时任务
  12. * Class Timedtask
  13. * @controller Timedtask
  14. * @group base
  15. */
  16. class Timedtask
  17. {
  18. /**
  19. * @title 取消订单定时任务
  20. * @desc 未支付的自动取消
  21. * @author Gavin
  22. * @url /api/Timedtask/cancelGoodsOrder
  23. * @method GET
  24. */
  25. public function cancelGoodsOrder(){
  26. $CancelTime = getCancelTime();
  27. if ($CancelTime<=0){
  28. die(1);
  29. }
  30. $redis = new Redis();
  31. $users = $redis->hkeys('buyUserInfo');
  32. if ($users){
  33. foreach ($users as &$value){
  34. $key = 'order_not_pay_'.$value;
  35. $len = $redis->hGetLen($key);
  36. if ($len){
  37. $list = $redis->hGetvals($key);
  38. foreach ($list as &$a){
  39. $info = json_decode($a,true);
  40. $cancel_time = strtotime($info['create_at'])+($CancelTime*60);
  41. if ($cancel_time<time()){
  42. $info['status'] = 2;
  43. $info['cancel_at'] = date('Y-m-d H:i:s');
  44. Db::name('store_order')->insert($info);
  45. //加上库存
  46. addCollectionInventory($info['c_id'],$info['num']);
  47. //减少用户购买数量
  48. DecrByCount($info['mid'],$info['c_id'],$info['num']);
  49. //删除数据
  50. $redis->hdel($key,$info['order_no']);
  51. }
  52. }
  53. }else{
  54. $redis->hdel('buyUserInfo',$value);
  55. }
  56. }
  57. }
  58. }
  59. /**
  60. * @title 二级市场未支付的自动取消
  61. * @desc 二级市场未支付的自动取消
  62. * @author Gavin
  63. * @url /api/Timedtask/SecondaryancelGoodsOrder
  64. * @method GET
  65. */
  66. public function SecondaryancelGoodsOrder(){
  67. $CancelTime = getConfigValue('secondary_cancel_time');
  68. if ($CancelTime<=0){
  69. die;
  70. }
  71. $list = Db::name('store_order_info_order')->where('status',0)->select();
  72. foreach ($list as &$v){
  73. $cancel_time = strtotime($v['create_at'])+($CancelTime*60);
  74. if ($cancel_time<time()){
  75. $info['status'] = 2;
  76. $info['cancel_at'] = date('Y-m-d H:i:s');
  77. Db::name('store_order_info_order')->where('id',$v['id'])->update($info);
  78. $cancle = [
  79. 'mid'=>$v['mid'],
  80. 'order_id'=>$v['id']
  81. ];
  82. Db::name('store_order_info_cancel_log')->insert($cancle);
  83. $time = date('Y-m-d H:i:s',time()-(60*60));
  84. $count = Db::name('store_order_info_cancel_log')->where('mid',$v['mid'])->where('create_at','gt',$time)->count();
  85. if ($count>2){
  86. $buy_time = date('Y-m-d H:i:s',time()+(24*60*60));
  87. Db::name('store_member')->where('id',$v['mid'])->update(['buy_time'=>$buy_time]);
  88. }
  89. }
  90. }
  91. }
  92. /**
  93. * @title 创建链账户
  94. * @desc 创建链账户
  95. * @author Gavin
  96. * @url /api/Timedtask/createAddress
  97. * @method GET
  98. */
  99. public function createAddress(){
  100. set_time_limit(0);
  101. $member = Db::name('store_member')
  102. ->where('wallet_address','eq','')
  103. //->whereNull('wallet_address')
  104. ->field('id,offline_account,phone,wallet_address')
  105. ->order('id asc')
  106. ->limit(30)
  107. ->select();
  108. foreach ($member as &$v){
  109. $name = $v['phone'];
  110. $operationId = $v['phone'];
  111. $operationId .= rand(10000,99999);
  112. $url = getIpAddress()."customNFT/createAccount?name=".$name."&operationId=".$operationId;
  113. $offlineaccount = file_get_contents($url);
  114. $offline_account = json_decode($offlineaccount,true);
  115. if (isset($offline_account) && isset($offline_account['account'])){
  116. Db::name('store_member')->where('id',$v['id'])
  117. ->update(['accountName'=>$operationId,'wallet_address'=>$offline_account['account'],'offline_account'=>$offlineaccount]);
  118. }
  119. }
  120. }
  121. /**
  122. * 查询链上架回执
  123. * @url /api/Timedtask/setNftCheck
  124. */
  125. public function setNftCheck(){
  126. Db::name('hash2')->where('success',0)->chunk(30,function ($list){
  127. foreach ($list as &$v){
  128. $result =$this->checkhashSuccess($v['operationId']);
  129. $data['result'] = json_encode($result,true);
  130. if (isset($result) && $result['status']==1){
  131. $data['success'] = 1;
  132. $data['class_id'] =$result['class_id'];
  133. }
  134. Db::name('hash2')->where('id',$v['id'])->update($data);
  135. }
  136. },'id','asc');
  137. }
  138. /**
  139. * 创建nft
  140. * @url /api/Timedtask/createNft
  141. */
  142. public function createNft(){
  143. Db::name('store_order_info')
  144. ->whereIn('status','1,4,5')
  145. ->where('collectors_hash','eq','')
  146. ->whereNull('collectors_hash2')
  147. ->chunk(30,function ($list){
  148. foreach ($list as &$v){
  149. $name = get32Str(10);
  150. $operationId = $v['id'];
  151. $operationId .= rand(1000000,9999999);
  152. $to = Db::name('store_member')->where('id',$v['mid'])->value('wallet_address');
  153. $url = getIpAddress().'customNFT/createNft?classId='.$v['tokenid'].'&name='.$name.'&operationId='.$operationId.'&recipient='.$to;
  154. $res=curlRequest($url);
  155. $result=json_decode($res,true);
  156. if (isset($result) && isset($result['task_id'])){
  157. Db::name('store_order_info')->where('id',$v['id'])->update(['collectors_hash2'=>$result['task_id'],'nfttype'=>$operationId]);
  158. }
  159. }
  160. },'id','asc');
  161. }
  162. /**
  163. * 判断创建的nft是否成功
  164. * @url /api/Timedtask/checkcreateNft
  165. */
  166. public function checkcreateNft(){
  167. Db::name('store_order_info')
  168. ->whereIn('status','1,4,5')
  169. ->where('collectors_hash','eq','')
  170. ->whereNotNull('collectors_hash2')
  171. ->chunk(30,function ($list){
  172. foreach ($list as &$v){
  173. $result = $this->checkhashSuccess($v['collectors_hash2']);
  174. $data['result'] = json_encode($result,true);
  175. if (isset($result) && $result['status']==1){
  176. $data['tokenid'] = $result['class_id'];
  177. $data['collectors_hash'] = $result['tx_hash'];
  178. $data['nftid'] = $result['nft_id'];
  179. $data['collectors_hash_time'] = date('Y-m-d H:i:s');
  180. }
  181. Db::name('store_order_info')->where('id',$v['id'])->update($data);
  182. }
  183. },'id','asc');
  184. }
  185. /**
  186. *
  187. * @url /api/Timedtask/checkrepetition
  188. */
  189. public function checkrepetition(){
  190. $list = Db::query('select count(collectors_hash) as count,collectors_hash from store_order_info group by collectors_hash having count(*)>1');
  191. foreach ($list as &$v){
  192. $list2 = Db::name('store_order_info')->where('collectors_hash',$v['collectors_hash'])->select();
  193. foreach ($list2 as &$a){
  194. $result = json_decode($a['result'],true);
  195. if ($result['status']!=1){
  196. Db::name('store_order_info')->where('id',$a['id'])->update(['collectors_hash'=>'']);
  197. }
  198. }
  199. }
  200. }
  201. /**
  202. * 转移nft
  203. * @url /api/Timedtask/judgeHash
  204. */
  205. public function judgeHash(){
  206. Db::name('store_order_info')
  207. ->where('status',3)
  208. ->where('collectors_hash','eq','')
  209. ->whereNull('collectors_hash2')
  210. ->chunk(30,function ($list){
  211. foreach ($list as &$v){
  212. $operationId = $v['id'];
  213. $operationId .= rand(10000000,99999999);
  214. $add = Db::name('store_member')->where('id',$v['to_mid'])->value('wallet_address');
  215. $to = Db::name('store_member')->where('id',$v['mid'])->value('wallet_address');
  216. $url = getIpAddress().'customNFT/transfer?classId='.$v['tokenid'].'&NFTId='.$v['nftid'].'&operationId='.$operationId.'&owner='.$add.'&recipient='.$to;
  217. echo $url;
  218. $res=curlRequest($url);
  219. $result=json_decode($res,true);
  220. dump($result);
  221. if (isset($result) && isset($result['task_id'])){
  222. Db::name('store_order_info')->where('id',$v['id'])->update(['collectors_hash2'=>$result['task_id'],'nfttype'=>$operationId]);
  223. }
  224. }
  225. },'id','asc');
  226. }
  227. /**
  228. * 判断转移的nft是否成功
  229. * @url /api/Timedtask/checkjudgeNft
  230. */
  231. public function checkjudgeNft(){
  232. Db::name('store_order_info')
  233. ->where('status',3)
  234. ->where('collectors_hash','eq','')
  235. ->whereNotNull('collectors_hash2')
  236. ->chunk(30,function ($list){
  237. foreach ($list as &$v){
  238. $result = $this->checkhashSuccess($v['collectors_hash2']);
  239. $data['result'] = json_encode($result,true);
  240. if (isset($result) && $result['status']==1){
  241. $data['tokenid'] = $result['class_id'];
  242. $data['collectors_hash'] = $result['tx_hash'];
  243. $data['nftid'] = $result['nft_id'];
  244. }
  245. Db::name('store_order_info')->where('id',$v['id'])->update($data);
  246. }
  247. },'id','asc');
  248. }
  249. /**
  250. * 查询交易是否成功
  251. * @url /api/Timedtask/checkhashSuccess
  252. */
  253. public function checkhashSuccess($operationId){
  254. $url = getIpAddress().'customNFT/queryDealResult?operationId='.$operationId;
  255. $res=curlRequest($url);
  256. $result=json_decode($res,true);
  257. return $result;
  258. }
  259. /**
  260. * 判断转移的数据是否属于当前转移用户
  261. * @url /api/Timedtask/checkjudgeHashOwer
  262. */
  263. public function checkjudgeHashOwer(){
  264. $list = Db::name('store_order_info')
  265. ->where('status',3)
  266. ->where('collectors_hash','eq','')
  267. ->whereNull('collectors_hash2')
  268. ->order('id asc')
  269. ->limit(30)
  270. ->select();
  271. foreach ($list as &$v){
  272. if (time()>strtotime($v['collectors_hash_time'])+(1*60*60)){
  273. $url = getIpAddress().'customNFT/sNFT?classId='.$v['tokenid'].'&NFTId='.$v['nftid'];
  274. $res=json_decode(curlRequest($url),true);
  275. if (isset($res)){
  276. dump($res);
  277. $add = Db::name('store_member')->where('id',$v['to_mid'])->value('wallet_address');
  278. echo $add."<br />";
  279. if ($add != $res['owner']){
  280. $operationId = $v['id'];
  281. $operationId .= rand(10000000,99999999);
  282. $adds = $res['owner'];
  283. $tos = $add;
  284. $url = getIpAddress().'customNFT/transfer?classId='.$v['tokenid'].'&NFTId='.$v['nftid'].'&operationId='.$operationId.'&owner='.$adds.'&recipient='.$tos;
  285. $ress = json_decode(curlRequest($url),true);
  286. dump($ress);
  287. }
  288. }
  289. }
  290. }
  291. }
  292. /**
  293. * redis 加锁
  294. */
  295. function redisCreateSetNx($id){
  296. $redis = new Redis();
  297. $key = 'hash_'.$id;
  298. $exptime = 450;
  299. $is_lock = $redis->setnx($key,time()+$exptime);
  300. if ($is_lock){
  301. return true;
  302. }else{
  303. //加锁失败的情况下,判断锁是否已经存在,如果存在切已经过期,删除锁,重新加锁
  304. $val = $redis->get($key);
  305. if ($val && $val<time()){
  306. $redis->del($key);
  307. }
  308. return $redis->setnx($key,time()+$exptime);
  309. }
  310. }
  311. /**
  312. * redis nonce加锁
  313. */
  314. function redisNonceSetNx(){
  315. $redis = new Redis();
  316. $key = 'noncenx';
  317. $exptime = 10;
  318. $is_lock = $redis->setnx($key,time()+$exptime);
  319. if ($is_lock){
  320. return true;
  321. }else{
  322. //加锁失败的情况下,判断锁是否已经存在,如果存在切已经过期,删除锁,重新加锁
  323. $val = $redis->get($key);
  324. if ($val && $val<time()){
  325. $redis->del($key);
  326. }
  327. return $redis->setnx($key,time()+$exptime);
  328. }
  329. }
  330. /**
  331. * 预约藏品前十五分钟发送短信
  332. * /api/Timedtask/sendSms
  333. */
  334. public function sendSms(){
  335. Db::name('store_collection_remind')->where('is_send',0)->chunk(50,function ($list){
  336. foreach ($list as &$v){
  337. $sell_time = Db::name('store_collection')->where('id',$v['c_id'])->value('sell_time');
  338. $t = (strtotime($sell_time)-time())/60;
  339. if ($t<=15 && $t>0){
  340. $result = $this->accessKeyClient($v['mobile'],$v['mid'],$v['c_id']);
  341. if ($result['Code'] === 'OK') {
  342. Db::name('store_collection_remind')->where('id',$v['id'])->update(['is_send'=>1]);
  343. }
  344. }
  345. }
  346. },'id','asc');
  347. }
  348. function accessKeyClient($mobile,$mid,$cid)
  349. {
  350. $ali_accesskey = 'LTAI5tSTBuRP5AnPBHDz8gTF';
  351. $ali_accesskey_secret = '7RVjRKv8cCaKW4hMMVZ1SFPkqeIbn4';
  352. $templateCode = 'SMS_243370550';
  353. AlibabaCloud::accessKeyClient($ali_accesskey, $ali_accesskey_secret)
  354. ->regionId('cn-hangzhou')
  355. ->asDefaultClient();
  356. $user = getMemberInfoHash($mid); //获取用户信息
  357. $coll_info = getCollectionInfoHash($cid);
  358. $post = [
  359. 'name'=>$user['name'],
  360. 'goodname'=>$coll_info['name'],
  361. 'pubtime'=>$coll_info['sell_time']
  362. ];
  363. $TemplateParam = json_encode($post,true);
  364. try {
  365. $result = AlibabaCloud::rpc()
  366. ->product('Dysmsapi')
  367. // ->scheme('https') // https | http
  368. ->version('2017-05-25')
  369. ->action('SendSms')
  370. ->method('POST')
  371. ->host('dysmsapi.aliyuncs.com')
  372. ->options([
  373. 'query' => [
  374. 'PhoneNumbers' => $mobile,
  375. 'SignName' => '同质科技',
  376. 'TemplateCode' => $templateCode,
  377. 'TemplateParam' => $TemplateParam
  378. ],
  379. ])
  380. ->request();
  381. $info = $result->toArray();
  382. return $info;
  383. } catch (ClientException $e) {
  384. echo $e->getErrorMessage() . PHP_EOL;
  385. } catch (ServerException $e) {
  386. echo $e->getErrorMessage() . PHP_EOL;
  387. }
  388. }
  389. }