| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- namespace app\api\logic;
- use app\api\controller\Teac;
- use app\common\model\ParametersModel;
- use app\common\model\ServersModel;
- use app\common\model\TeacTrade;
- use fast\Http;
- use think\Loader;
- use Exception;
- use finfo;
- use think\Cache;
- use think\Env;
- use think\Log;
- /**
- * Teac交易
- */
- class TeacLogin
- {
-
-
-
- /**
- * 发布求购信息
- */
- public static function setCreateOrder(int $uid, float $price, int $stock, int $typeId = TeacTrade::Buying):float
- {
-
- $rows = TeacTrade::where('status', TeacTrade::Normal)->find();
- if($rows) throw new Exception(__("你有未完成的求购订单、不能重复发布"));
- //添加订单信息
- return TeacTrade::setUserCreateOrder($uid, $typeId, $price, $stock, bcmul($price, $stock, 2));
- }
- /**
- * 发布出售
- * @$address 查询地址
- * @$from_to 是查询付款交易(from),还剩收款交易(to),默认from
- */
- public function setCreateSellOrder(string $from, string $to, int $start_block, int $end_block = 99999999)
- {
- if(empty($from) && empty($to)){
- return _error('钱包地址不能都为空');
- }
-
-
- }
- /**
- * 获取哈希地址成功状态
- * @param $orderInfo
- * @return array|string
- */
- public function getHashStatus($tx_hash):array
- {
- if (empty($tx_hash)) {
- return _error('hash值不能为空');
- }
- $url = "https://api.bscscan.com/api?module=transaction&action=gettxreceiptstatus";
- $url .= "&apikey=" . $this->bsc_api_key;
- $url .= "&txhash=" . $tx_hash;
- $body = Http::get($url);
- if (empty($body)) {
- return _error('状态api返回内容为空');
- }
- // 转成数组
- $rsArr = json_decode($body, true);
- if (empty($rsArr) || !is_array($rsArr)) {
- return _error('状态api返回数据异常,json转换失败');
- }
- if ($rsArr['status'] != '1') {
- return _error('状态api返回status不为1,当前值为:' . $rsArr['status']);
- }
- if ($rsArr['result']['status'] != 1) {
- return _error('状态api返回result中的status不为1,当前值为:' . $rsArr['result']['status']);
- }
- return _success();
- }
- /**
- * 根据时间戳获取最近的区块高度
- * api接口返回数据格式
- * @param $orderInfo
- * @return array|string
- */
- public function getBlockNoByTime($time):array
- {
- if(!Cache::has('block_' . $time)){
- if (empty($time)) {
- return _error('时间戳不能为空');
- }
- //https://api.bscscan.com/api?module=block&action=getblocknobytime×tamp=1601510400&closest=before&apikey=YourApiKeyToken
- $url = "https://api.bscscan.com/api?module=block&action=getblocknobytime&";
- $url .= "&closest=before";//closest 值还有个参数 after 控制返回接近时间戳之前还是之后的区块高度
- $url .= "&apikey=" . $this->bsc_api_key;
- $url .= "×tamp=" . $time;
- dump($url);
- $body = Http::get($url);
- if (empty($body)) {
- return _error('获取区块高度api返回内容为空');
- }
- // 转成数组
- $rsArr = json_decode($body, true);
- if (empty($rsArr) || !is_array($rsArr)) {
- return _error('获取区块高度api返回数据异常,json转换失败');
- }
- if ($rsArr['status'] != '1') {
- return _error($rsArr['message'] . ' -- ' . $rsArr['result']);
- }
- Cache::set('block_' . $time, _success($rsArr['result']), 3600);
- }
- //获取开始预约前的区块高度
- return Cache::get('block_' . $time);
- }
- public function getBlcokNoByCache($time){
- if(!Cache::has('block')){
- $get_block = (new BscApi())->getBlockNoByTime($start_time);
- if($get_block['code'] == 0){
- Log::info($get_block['msg'] . date('Y-m-d H:i:s'));
- dump('获取区块高度有误');
- dump($get_block);
- return;
- }
- Cache::set('block', $get_block['data'], 3600);
- }
- //获取开始预约前的区块高度
- $start_block = Cache::get('block');
- }
-
-
- }
|