TeacLogin.php 4.5 KB

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