Teac.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\api\logic\TeacLogin;
  5. use app\common\model\LedgerTeacChangeModel;
  6. use app\common\model\LedgerTokenChangeModel;
  7. use app\common\model\UserTeac;
  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->with('users')
  26. ->where('product_teac.status', ProductTeac::Normal)->where('type_id', $type_id)
  27. ->order('create_time desc')
  28. ->paginate($this->pageSize);
  29. foreach ($list as &$item) {
  30. $item->users->avatar = !empty($item->users->avatar)? $item->users->avatar:$this->request->domain().'/assets/img/logo.png'; // 头像
  31. }
  32. $this->success('ok', $list);
  33. }
  34. /*
  35. * 发布求购
  36. */
  37. public function setBuying(LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin)
  38. {
  39. $params = $this->request->post();
  40. $validate = \think\Loader::validate('Teac');
  41. if(!$validate->scene('buying')->check($params)) $this->error($validate->getError());
  42. if(config('teac_trade.buy_min_num') > $params['stock']) $this->error('数量不能低于'.config('teac_trade.buy_min_num'));
  43. if(config('teac_trade.buy_min_price') > $params['price']) $this->error('价格不能低于'.config('teac_trade.buy_min_price'));
  44. $chabao = bcmul($params['price'], $params['stock'], 2) ;
  45. if($ledgerWalletModel->getWalletChaBao($this->auth->id) < $chabao) $this->error('您的钱包茶宝余额不足');
  46. Db::startTrans();
  47. try{
  48. //新增求购信息
  49. $teacLogin::setCreateTrade($this->auth->id, $params['price'], $params['stock'], ProductTeac::Buying, $chabao);
  50. //冻结茶宝
  51. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$chabao, LedgerTokenChangeModel::Buying, $this->auth->id);
  52. Db::commit();
  53. }catch(Exception $e){
  54. Db::rollback();
  55. $this->error($e->getMessage());
  56. }
  57. $this->success('ok');
  58. }
  59. //发布出售
  60. public function setSell(LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin)
  61. {
  62. $params = $this->request->post();
  63. $validate = \think\Loader::validate('Teac');
  64. if(!$validate->scene('sell')->check($params)) $this->error($validate->getError());
  65. if(config('teac_trade.sell_min_num') > $params['stock']) $this->error('数量不能低于'.config('teac_trade.sell_min_num'));
  66. if(config('teac_trade.sell_min_price') > $params['price']) $this->error('价格不能低于'.config('teac_trade.sell_min_price'));
  67. if($ledgerWalletModel->getWalletTeac($this->auth->id) < $params['stock']) $this->error('您的钱包Teac余额不足');
  68. Db::startTrans();
  69. try{
  70. //新增求购信息
  71. $teacLogin::setCreateTrade($this->auth->id, $params['price'], $params['stock'], ProductTeac::Sell, $params['stock']);
  72. //冻结Teac
  73. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TEAC, -$params['stock'], LedgerTokenChangeModel::BuySellg, $this->auth->id);
  74. Db::commit();
  75. }catch(Exception $e){
  76. Db::rollback();
  77. $this->error($e->getMessage());
  78. }
  79. $this->success('ok');
  80. }
  81. /**
  82. * 出售购买
  83. */
  84. public function setSellOrder(ProductTeac $productTeac, LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin, UserTeac $userTeac)
  85. {
  86. $params = $this->request->post();
  87. $validate = \think\Loader::validate('Teac');
  88. if(!$validate->scene('sell_order')->check($params)) $this->error($validate->getError());
  89. $row = $productTeac::where(['type_id'=> ProductTeac::Sell, 'id' => $params['teac_id']])->find();
  90. if(empty($row) || $row['status'] != ProductTeac::Normal) $this->error('订单不存在');
  91. if($params['num'] < $row['stock'] - $row['num']) $this->error('库存不足');
  92. $chabao = $ledgerWalletModel->getWalletChaBao($this->auth->id);
  93. $tatal = bcmul($row['price'], $params['num'], 2);
  94. if($chabao < bcmul($row['price'], $tatal, 2)) $this->error('您的钱包茶宝余额不足');
  95. // 启动事务
  96. Db::startTrans();
  97. try {
  98. $fee = config('teac_trade.sell_fee');
  99. // 出售购买
  100. $userTeac::setUserCreateOrder($this->auth->id, $row['id'], ProductTeac::Sell, $params['num'], $row['price'], $fee);
  101. //添加扣除相应茶宝Teac
  102. $teacLogin::setCreateSellOrder($this->auth->id, $row['user_id'], $row['price'], $params['num'], $fee);
  103. //修改状态
  104. if($row->stock - $row->num == $params['num']) $row->status = ProductTeac::Complete;
  105. $row->frozen -= $params['num'];
  106. $row->num += $params['num'];
  107. $row->save();
  108. // 提交事务
  109. Db::commit();
  110. } catch (Exception $e) {
  111. // 回滚事务
  112. Db::rollback();
  113. return $e->getMessage();
  114. }
  115. $this->success('ok');
  116. }
  117. /**
  118. * 求购出售
  119. */
  120. public function getBuyOrder(ProductTeac $productTeac, LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin, UserTeac $userTeac)
  121. {
  122. $params = $this->request->post();
  123. $validate = \think\Loader::validate('Teac');
  124. if(!$validate->scene('sell_order')->check($params)) $this->error($validate->getError());
  125. $row = $productTeac::where(['type_id'=> ProductTeac::Buying, 'id' => $params['teac_id']])->find();
  126. if(empty($row) || $row['status'] != ProductTeac::Normal) $this->error('订单不存在');
  127. if($params['num'] < $row['stock'] - $row['num']) $this->error('库存不足');
  128. $chabao = $ledgerWalletModel->getWalletChaBao($this->auth->id);
  129. $tatal = bcmul($row['price'], $params['num'], 2);
  130. if($chabao < bcmul($row['price'], $tatal, 2)) $this->error('您的钱包茶宝余额不足');
  131. // 启动事务
  132. Db::startTrans();
  133. try {
  134. $fee = config('teac_trade.sell_fee');
  135. $chabao = bcmul($row['price'], $params['num'], 2);
  136. // 出售购买
  137. $userTeac::setUserCreateOrder($this->auth->id, $row['id'], ProductTeac::Buying, $params['num'], $row['price'], $fee);
  138. //添加扣除相应茶宝Teac
  139. $teacLogin::setCreateBuyingOrder($this->auth->id, $row['user_id'], $row['price'], $params['num'], $fee);
  140. //修改状态
  141. if($row->stock - $row->num == $params['num']) $row->status = ProductTeac::Complete;
  142. $row->frozen -= $chabao;
  143. $row->num += $params['num'];
  144. $row->save();
  145. // 提交事务
  146. Db::commit();
  147. } catch (Exception $e) {
  148. // 回滚事务
  149. Db::rollback();
  150. return $e->getMessage();
  151. }
  152. $this->success('ok');
  153. }
  154. /**
  155. * 获取配置
  156. */
  157. public function getConfig()
  158. {
  159. $this->success('ok', config('teac_trade'));
  160. }
  161. }