| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace app\api\logic;
- use app\common\model\ProductTeac;
- use fast\Http;
- use think\Loader;
- use Exception;
- use finfo;
- use think\Cache;
- use fast\Asset;
- use think\Log;
- use think\Lang;
- use app\common\model\LedgerTeacChangeModel;
- use app\common\model\LedgerTokenChangeModel;
- /**
- * Teac交易
- */
- class TeacLogin
- {
-
-
- /**
- * 发布求购出售信息
- */
- public static function setCreateTrade(int $uid, float $price, int $stock, int $typeId, float $frozen):object
- {
- $rows = ProductTeac::where('user_id', $uid)->where('type_id', $typeId)->where('status', ProductTeac::Normal)->find();
- if($rows) throw new Exception(__("你有未完成的求购订单、不能重复发布"));
- //添加订单信息
- return ProductTeac::setUserCreateOrder($uid, $typeId, $price, $stock, $frozen);
- }
- /**
- * 购买出售
- * @$uid 用户人id
- * @$fromUid 来源订单id
- */
- public static function setCreateSellOrder(int $uid, int $fromUid, float $chabao, int $num, float $fee)
- {
- $ledgerWalletModel = Loader::model('LedgerWalletModel');
- $total_price = bcmul($chabao, $num, 2); //总价
- $fee = bcmul($total_price, $fee, 2); //手续费
- //扣除用户茶宝
- $ledgerWalletModel->changeWalletAccount($uid, Asset::TOKEN, -$total_price, LedgerTokenChangeModel::BuySellg, $fromUid);
- //添加用户Teac
- $ledgerWalletModel->changeWalletAccount($uid, Asset::TEAC, $num, LedgerTokenChangeModel::BuySellg, $fromUid);
- //添加来源茶宝
- $ledgerWalletModel->changeWalletAccount($fromUid, Asset::TOKEN, bcsub($total_price, $fee, 2), LedgerTokenChangeModel::BuySellg, $uid);
- return true;
- }
- /**
- * 求购出售
- * @$uid 用户人id
- * @$fromUid 来源订单id
- */
- public static function setCreateBuyingOrder(int $uid, int $fromUid, float $chabao, int $num, float $fee)
- {
- $ledgerWalletModel = Loader::model('LedgerWalletModel');
- $total_price = bcmul($chabao, $num, 2); //总价
- $fee = bcmul($total_price, $fee, 2); //手续费
- //添加用户茶宝
- $ledgerWalletModel->changeWalletAccount($uid, Asset::TOKEN, $total_price, LedgerTokenChangeModel::BuySellg, $fromUid);
- //扣除用户Teac
- $ledgerWalletModel->changeWalletAccount($uid, Asset::TEAC, $num, LedgerTokenChangeModel::BuySellg, $fromUid);
- //扣除来源茶宝
- $ledgerWalletModel->changeWalletAccount($fromUid, Asset::TOKEN, bcsub($total_price, $fee, 2), LedgerTokenChangeModel::BuySellg, $uid);
- return true;
- }
- /**
- * 根据时间戳获取最近的区块高度
- * 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');
- }
-
-
- }
|