Teac.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\api\logic\TeacLogin;
  5. use app\common\model\ParametersModel;
  6. use app\common\model\LedgerTokenChangeModel;
  7. use app\common\model\UserModel;
  8. use app\common\model\LedgerWalletModel;
  9. use fast\Action;
  10. use fast\Asset;
  11. use Exception;
  12. use fast\RechargeStatus;
  13. use think\Db;
  14. use app\common\model\ProductTeac;
  15. //Teac交易
  16. class Teac extends Api
  17. {
  18. /*
  19. * 求购列表
  20. */
  21. public function getTeacList(ProductTeac $productTeac)
  22. {
  23. $type_id = $this->request->post('type_id/d', 0);
  24. if(!in_array($type_id, [ProductTeac::Buying, ProductTeac::Sell])) $this->error('类型错误');
  25. $list = $productTeac->where('status', ProductTeac::Normal)->where('type_id', $type_id)
  26. ->order('create_time desc')
  27. ->paginate($this->pageSize);
  28. $this->success('ok', $list);
  29. }
  30. /*
  31. * 发布求购
  32. */
  33. public function setBuying(LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin)
  34. {
  35. $params = $this->request->post();
  36. $validate = \think\Loader::validate('Teac');
  37. if(!$validate->scene('buying')->check($params)) $this->error($validate->getError());
  38. if(config('teac_trade.buy_min_num') > $params['stock']) $this->error('数量不能低于'.config('teac_trade.buy_min_num'));
  39. if(config('teac_trade.buy_min_price') > $params['price']) $this->error('价格不能低于'.config('teac_trade.buy_min_price'));
  40. $chabao = $ledgerWalletModel->getWalletChaBao($this->auth->id);
  41. if($chabao < bcmul($params['price'], $params['stock'], 2)) $this->error('您的钱包茶宝余额不足');
  42. Db::startTrans();
  43. try{
  44. //新增求购信息
  45. $order_price = $teacLogin::setCreateTrade($this->auth->id, $params['price'], $params['stock']);
  46. //冻结资产
  47. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$order_price, LedgerTokenChangeModel::Buying, $this->auth->id);
  48. Db::commit();
  49. $this->success('订单创建成功');
  50. }catch(Exception $e){
  51. Db::rollback();
  52. $this->error($e->getMessage());
  53. }
  54. }
  55. //发布出售
  56. public function setSell(LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin)
  57. {
  58. $params = $this->request->post();
  59. $validate = \think\Loader::validate('Teac');
  60. if(!$validate->scene('sell')->check($params)) $this->error($validate->getError());
  61. if(config('teac_trade.sell_min_num') > $params['price']) $this->error('数量不能低于'.config('teac_trade.sell_min_num'));
  62. if(config('teac_trade.sell_min_price') > $params['price']) $this->error('价格不能低于'.config('teac_trade.sell_min_price'));
  63. $teac = $ledgerWalletModel->getWalletTeac($this->auth->id);
  64. if($teac < bcmul($params['price'], $params['stock'], 2)) $this->error('您的钱包Teac余额不足');
  65. Db::startTrans();
  66. try{
  67. //新增求购信息
  68. $order_price = $teacLogin::setCreateTrade($this->auth->id, $params['price'], $params['stock'], TeacTrade::Sell);
  69. //冻结资产
  70. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TEAC, -$order_price, LedgerTokenChangeModel::Buying, $this->auth->id);
  71. Db::commit();
  72. $this->success('订单创建成功');
  73. }catch(Exception $e){
  74. Db::rollback();
  75. $this->error($e->getMessage());
  76. }
  77. }
  78. /**
  79. * 出售购买
  80. * @return void
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. * @throws \think\exception\DbException
  84. */
  85. public function setSellOrder(ProductTeac $productTeac, LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin)
  86. {
  87. $params = $this->request->post();
  88. $validate = \think\Loader::validate('Teac');
  89. if(!$validate->scene('sell_order')->check($params)) $this->error($validate->getError());
  90. $row = $productTeac::get($params['id']);
  91. if(empty($row)) $this->error('订单不存在');
  92. if($row['status'] != ProductTeac::Normal) $this->error('订单已完成');
  93. // 启动事务
  94. Db::startTrans();
  95. try {
  96. // 提交事务
  97. Db::commit();
  98. } catch (Exception $e) {
  99. // 回滚事务
  100. Db::rollback();
  101. return $e->getMessage();
  102. }
  103. }
  104. /**
  105. * 求购出售
  106. */
  107. public function getBuyOrder()
  108. {
  109. $this->success('ok', config('teac_trade'));
  110. }
  111. /**
  112. * 获取配置
  113. */
  114. public function getConfig()
  115. {
  116. $this->success('ok', config('teac_trade'));
  117. }
  118. }