request->post('type_id/d', 0); if(!in_array($type_id, [TeacTrade::Buying, TeacTrade::Sell])) $this->error('类型错误'); $list = $teacTrade->where('status', TeacTrade::Normal)->where('type_id', $type_id ) ->order('create_time desc') ->paginate($this->pageSize); $this->success('ok', $list); } /* * 发布求购 */ public function setBuying(LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin) { $params = $this->request->post(); $validate = \think\Loader::validate('Teac'); if(!$validate->scene('buying')->check($params)) $this->error($validate->getError()); if(config('teac_trade.buy_min_num') > $params['stock']) $this->error('数量不能低于'.config('teac_trade.buy_min_num')); if(config('teac_trade.buy_min_price') > $params['price']) $this->error('价格不能低于'.config('teac_trade.buy_min_price')); $chabao = $ledgerWalletModel->getWalletChaBao($this->auth->id); if($chabao < bcmul($params['price'], $params['stock'], 2)) $this->error('您的钱包茶宝余额不足'); Db::startTrans(); try{ //新增求购信息 $order_price = $teacLogin::setCreateOrder($this->auth->id, $params['price'], $params['stock']); //冻结资产 $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$order_price, LedgerTokenChangeModel::Buying, $this->auth->id); Db::commit(); $this->success('订单创建成功'); }catch(Exception $e){ Db::rollback(); $this->error($e->getMessage()); } } //发布出售 public function setSell(LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin) { $params = $this->request->post(); $validate = \think\Loader::validate('Teac'); if(!$validate->scene('sell')->check($params)) $this->error($validate->getError()); if(config('teac_trade.sell_min_num') > $params['price']) $this->error('数量不能低于'.config('teac_trade.sell_min_num')); if(config('teac_trade.sell_min_price') > $params['price']) $this->error('价格不能低于'.config('teac_trade.sell_min_price')); $teac = $ledgerWalletModel->getWalletTeac($this->auth->id); if($teac < bcmul($params['price'], $params['stock'], 2)) $this->error('您的钱包Teac余额不足'); Db::startTrans(); try{ //新增求购信息 $order_price = $teacLogin::setCreateOrder($this->auth->id, $params['price'], $params['stock'], TeacTrade::Sell); //冻结资产 $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TEAC, -$order_price, LedgerTokenChangeModel::Buying, $this->auth->id); Db::commit(); $this->success('订单创建成功'); }catch(Exception $e){ Db::rollback(); $this->error($e->getMessage()); } } /** * 出售购买 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function setSellOrder() { $params = $this->request->post(); $validate = \think\Loader::validate('Teac'); if(!$validate->scene('sell_order')->check($params)) $this->error($validate->getError()); // 启动事务 Db::startTrans(); try { // 提交事务 Db::commit(); } catch (Exception $e) { // 回滚事务 Db::rollback(); return $e->getMessage(); } } /** * 求购出售 */ public function getBuyOrder() { $this->success('ok', config('teac_trade')); } /** * 获取配置 */ public function getConfig() { $this->success('ok', config('teac_trade')); } }