TeacLogin.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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)
  23. {
  24. $rows = TeacTrade::where('status', TeacTrade::Normal)->find();
  25. if($rows) throw new Exception(__("你有未完成的求购订单、不能重复发布"));
  26. //添加订单信息
  27. return TeacTrade::setUserCreateOrder($uid, TeacTrade::Buying, $price, $stock, bcmul($price, $stock, 2));
  28. }
  29. /**
  30. * 根据钱包地址查询交易记录
  31. * @$address 查询地址
  32. * @$from_to 是查询付款交易(from),还剩收款交易(to),默认from
  33. */
  34. public function getTransactionRecordsByAddress(string $from, string $to, int $start_block, int $end_block = 99999999)
  35. {
  36. if(empty($from) && empty($to)){
  37. return _error('钱包地址不能都为空');
  38. }
  39. $from = strtolower($from);
  40. $to = strtolower($to);
  41. $address = $from;
  42. if(empty($from)){
  43. $address =$to;
  44. }
  45. // 读取远程页面地址
  46. $url = "https://api.bscscan.com/api?module=account&action=tokentx&contractaddress=" . $this->contract_address;
  47. $url .= "&page=1&offset=10000&endblock=" . $end_block ."&apikey=" . $this->bsc_api_key;
  48. $url .= "&startblock=" . $start_block . "&address=" . $address;
  49. Log::log($url);
  50. $body = Http::get($url);
  51. if (empty($body)) {
  52. Log::log('api返回内容为空');
  53. return _error('api返回内容为空');
  54. }
  55. // 转成数组
  56. $rsArr = json_decode($body, true);
  57. if (empty($rsArr) || !is_array($rsArr)) {
  58. Log::log('api返回数据异常,json decode失败');
  59. return _error('api返回数据异常');
  60. }
  61. if ($rsArr['status'] != '1') {
  62. if($rsArr['message'] == 'No transactions found'){
  63. Log::log('无交易记录:' . $rsArr['message']);
  64. return _success();
  65. }
  66. Log::log('api返回status不为1,错误信息:' . $rsArr['message']);
  67. return _error('api返回status不为1,错误信息:' . $rsArr['message']);
  68. }
  69. return _success($success_arr);
  70. }
  71. /**
  72. * 获取哈希地址成功状态
  73. * @param $orderInfo
  74. * @return array|string
  75. */
  76. public function getHashStatus($tx_hash):array
  77. {
  78. if (empty($tx_hash)) {
  79. return _error('hash值不能为空');
  80. }
  81. $url = "https://api.bscscan.com/api?module=transaction&action=gettxreceiptstatus";
  82. $url .= "&apikey=" . $this->bsc_api_key;
  83. $url .= "&txhash=" . $tx_hash;
  84. $body = Http::get($url);
  85. if (empty($body)) {
  86. return _error('状态api返回内容为空');
  87. }
  88. // 转成数组
  89. $rsArr = json_decode($body, true);
  90. if (empty($rsArr) || !is_array($rsArr)) {
  91. return _error('状态api返回数据异常,json转换失败');
  92. }
  93. if ($rsArr['status'] != '1') {
  94. return _error('状态api返回status不为1,当前值为:' . $rsArr['status']);
  95. }
  96. if ($rsArr['result']['status'] != 1) {
  97. return _error('状态api返回result中的status不为1,当前值为:' . $rsArr['result']['status']);
  98. }
  99. return _success();
  100. }
  101. /**
  102. * 根据时间戳获取最近的区块高度
  103. * api接口返回数据格式
  104. * @param $orderInfo
  105. * @return array|string
  106. */
  107. public function getBlockNoByTime($time):array
  108. {
  109. if(!Cache::has('block_' . $time)){
  110. if (empty($time)) {
  111. return _error('时间戳不能为空');
  112. }
  113. //https://api.bscscan.com/api?module=block&action=getblocknobytime&timestamp=1601510400&closest=before&apikey=YourApiKeyToken
  114. $url = "https://api.bscscan.com/api?module=block&action=getblocknobytime&";
  115. $url .= "&closest=before";//closest 值还有个参数 after 控制返回接近时间戳之前还是之后的区块高度
  116. $url .= "&apikey=" . $this->bsc_api_key;
  117. $url .= "&timestamp=" . $time;
  118. dump($url);
  119. $body = Http::get($url);
  120. if (empty($body)) {
  121. return _error('获取区块高度api返回内容为空');
  122. }
  123. // 转成数组
  124. $rsArr = json_decode($body, true);
  125. if (empty($rsArr) || !is_array($rsArr)) {
  126. return _error('获取区块高度api返回数据异常,json转换失败');
  127. }
  128. if ($rsArr['status'] != '1') {
  129. return _error($rsArr['message'] . ' -- ' . $rsArr['result']);
  130. }
  131. Cache::set('block_' . $time, _success($rsArr['result']), 3600);
  132. }
  133. //获取开始预约前的区块高度
  134. return Cache::get('block_' . $time);
  135. }
  136. public function getBlcokNoByCache($time){
  137. if(!Cache::has('block')){
  138. $get_block = (new BscApi())->getBlockNoByTime($start_time);
  139. if($get_block['code'] == 0){
  140. Log::info($get_block['msg'] . date('Y-m-d H:i:s'));
  141. dump('获取区块高度有误');
  142. dump($get_block);
  143. return;
  144. }
  145. Cache::set('block', $get_block['data'], 3600);
  146. }
  147. //获取开始预约前的区块高度
  148. $start_block = Cache::get('block');
  149. }
  150. }