TeacLogin.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\api\logic;
  3. use app\common\model\ProductTeac;
  4. use fast\Http;
  5. use think\Loader;
  6. use Exception;
  7. use finfo;
  8. use think\Cache;
  9. use think\Env;
  10. use think\Log;
  11. /**
  12. * Teac交易
  13. */
  14. class TeacLogin
  15. {
  16. /**
  17. * 发布求购出售信息
  18. */
  19. public static function setCreateTrade(int $uid, float $price, int $stock, int $typeId = ProductTeac::Buying):float
  20. {
  21. $rows = ProductTeac::where('status', ProductTeac::Normal)->find();
  22. if($rows) throw new Exception(__("你有未完成的求购订单、不能重复发布"));
  23. //添加订单信息
  24. return ProductTeac::setUserCreateOrder($uid, $typeId, $price, $stock, bcmul($price, $stock, 2));
  25. }
  26. /**
  27. * 发布出售
  28. * @$address 查询地址
  29. * @$from_to 是查询付款交易(from),还剩收款交易(to),默认from
  30. */
  31. public function setCreateSellOrder(string $from, string $to, int $start_block, int $end_block = 99999999)
  32. {
  33. if(empty($from) && empty($to)){
  34. return _error('钱包地址不能都为空');
  35. }
  36. }
  37. /**
  38. * 获取哈希地址成功状态
  39. * @param $orderInfo
  40. * @return array|string
  41. */
  42. public function getHashStatus($tx_hash):array
  43. {
  44. if (empty($tx_hash)) {
  45. return _error('hash值不能为空');
  46. }
  47. $url = "https://api.bscscan.com/api?module=transaction&action=gettxreceiptstatus";
  48. $url .= "&apikey=" . $this->bsc_api_key;
  49. $url .= "&txhash=" . $tx_hash;
  50. $body = Http::get($url);
  51. if (empty($body)) {
  52. return _error('状态api返回内容为空');
  53. }
  54. // 转成数组
  55. $rsArr = json_decode($body, true);
  56. if (empty($rsArr) || !is_array($rsArr)) {
  57. return _error('状态api返回数据异常,json转换失败');
  58. }
  59. if ($rsArr['status'] != '1') {
  60. return _error('状态api返回status不为1,当前值为:' . $rsArr['status']);
  61. }
  62. if ($rsArr['result']['status'] != 1) {
  63. return _error('状态api返回result中的status不为1,当前值为:' . $rsArr['result']['status']);
  64. }
  65. return _success();
  66. }
  67. /**
  68. * 根据时间戳获取最近的区块高度
  69. * api接口返回数据格式
  70. * @param $orderInfo
  71. * @return array|string
  72. */
  73. public function getBlockNoByTime($time):array
  74. {
  75. if(!Cache::has('block_' . $time)){
  76. if (empty($time)) {
  77. return _error('时间戳不能为空');
  78. }
  79. //https://api.bscscan.com/api?module=block&action=getblocknobytime&timestamp=1601510400&closest=before&apikey=YourApiKeyToken
  80. $url = "https://api.bscscan.com/api?module=block&action=getblocknobytime&";
  81. $url .= "&closest=before";//closest 值还有个参数 after 控制返回接近时间戳之前还是之后的区块高度
  82. $url .= "&apikey=" . $this->bsc_api_key;
  83. $url .= "&timestamp=" . $time;
  84. dump($url);
  85. $body = Http::get($url);
  86. if (empty($body)) {
  87. return _error('获取区块高度api返回内容为空');
  88. }
  89. // 转成数组
  90. $rsArr = json_decode($body, true);
  91. if (empty($rsArr) || !is_array($rsArr)) {
  92. return _error('获取区块高度api返回数据异常,json转换失败');
  93. }
  94. if ($rsArr['status'] != '1') {
  95. return _error($rsArr['message'] . ' -- ' . $rsArr['result']);
  96. }
  97. Cache::set('block_' . $time, _success($rsArr['result']), 3600);
  98. }
  99. //获取开始预约前的区块高度
  100. return Cache::get('block_' . $time);
  101. }
  102. public function getBlcokNoByCache($time){
  103. if(!Cache::has('block')){
  104. $get_block = (new BscApi())->getBlockNoByTime($start_time);
  105. if($get_block['code'] == 0){
  106. Log::info($get_block['msg'] . date('Y-m-d H:i:s'));
  107. dump('获取区块高度有误');
  108. dump($get_block);
  109. return;
  110. }
  111. Cache::set('block', $get_block['data'], 3600);
  112. }
  113. //获取开始预约前的区块高度
  114. $start_block = Cache::get('block');
  115. }
  116. }