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'); } }