Market.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\LedgerTokenChangeModel;
  4. use app\common\controller\Api;
  5. use app\common\model\AnnouncementModel;
  6. use app\common\model\UserCollect;
  7. use app\common\model\ProductBuying;
  8. use app\common\model\ParametersModel;
  9. use app\common\model\ProductOrder;
  10. use app\common\model\UserModel;
  11. use app\common\model\LedgerWalletModel;
  12. use app\common\model\ProductTransfer;
  13. use app\api\logic\MarketLogic;
  14. use fast\Action;
  15. use fast\Asset;
  16. use fast\Http;
  17. use fast\RechargeStatus;
  18. use think\Db;
  19. use think\Exception;
  20. class Market extends Api
  21. {
  22. //用户收藏
  23. public function collect(UserCollect $userCollect)
  24. {
  25. $params = $this->request->post();
  26. $validate = \think\Loader::validate('Market');
  27. if(!$validate->scene('collect')->check($params)) $this->error($validate->getError());
  28. $userCollect::setUserCollect($this->auth->id, $params['market_id']);
  29. $this->success("ok");
  30. }
  31. /*
  32. * 相关公告
  33. */
  34. public function announcement(AnnouncementModel $announcementModel)
  35. {
  36. $params = $this->request->post();
  37. $validate = \think\Loader::validate('Market');
  38. if(!$validate->scene('announcement')->check($params)) $this->error($validate->getError());
  39. $announcement = $announcementModel
  40. ->where('find_in_set(:id,product_id)',['id'=>$params['product_id']])
  41. ->where('status', $announcementModel::Normal)
  42. ->where('to_lang', $this->request->getLan())
  43. ->order('id desc')
  44. ->paginate($this->pageSize);
  45. $this->success('ok', $announcement);
  46. }
  47. //获取产品最大求购价格
  48. public function getBuyingMaxPrice(ProductBuying $productBuying)
  49. {
  50. $params = $this->request->post();
  51. $validate = \think\Loader::validate('Market');
  52. if(!$validate->scene('announcement')->check($params)) $this->error($validate->getError());
  53. $this->success('ok', $productBuying::getProductBuyingMaxPrice($params['product_id']));
  54. }
  55. //锁定寄售
  56. public function setTransferLock(ProductTransfer $productTransfer, MarketLogic $marketLogic)
  57. {
  58. $params = $this->request->post();
  59. $validate = \think\Loader::validate('Market');
  60. if(!$validate->scene('transferlock')->check($params)) $this->error($validate->getError());
  61. Db::startTrans();
  62. try {
  63. $marketLogic::setTransferLock($productTransfer, config('market_transfer.lock_time'), $params['transfer_id']);
  64. // 提交事务
  65. Db::commit();
  66. } catch (Exception $e) {
  67. // 回滚事务
  68. Db::rollback();
  69. $this->error($e->getMessage(), null, $e->getCode());
  70. }
  71. $this->success('ok');
  72. }
  73. //求购列表
  74. public function buyingList(ProductBuying $productBuying)
  75. {
  76. $params = $this->request->post();
  77. $validate = \think\Loader::validate('Market');
  78. if(!$validate->scene('announcement')->check($params)) $this->error($validate->getError());
  79. $this->success('ok', $productBuying::getBuyingList($params['product_id']));
  80. }
  81. /**
  82. * 发起求购
  83. * @return void
  84. */
  85. public function setBuying(LedgerWalletModel $ledgerWalletModel, ProductBuying $productBuying)
  86. {
  87. $params = $this->request->post();
  88. $validate = \think\Loader::validate('Market');
  89. if(!$validate->scene('buying')->check($params)) $this->error($validate->getError());
  90. if($productBuying::getProductBuyingCount($this->auth->id, $params['product_id']) > config('market_buying.max_buying_num')) $this->error('您的求购次数已达上限');
  91. $chabao = $ledgerWalletModel::getWalletChaBao($this->auth->id);
  92. $total = bcmul($params['min_price'], $params['num'], 6); // 所需茶宝
  93. if($chabao < $total) $this->error('您的茶币不足');
  94. // 启动事务
  95. Db::startTrans();
  96. try {
  97. // 记录订单
  98. $productBuying::setCreateBuying($this->auth->id, $params['product_id'], $params['num'], $params['min_price'], $total);
  99. // 扣除茶币
  100. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$total, LedgerTokenChangeModel::BuySellg, $this->auth->id);
  101. $ledgerWalletModel->changeWalletOnly($this->auth->id, Asset::BUYING, $total); //冻结金额
  102. // 提交事务
  103. Db::commit();
  104. } catch (Exception $e) {
  105. // 回滚事务
  106. Db::rollback();
  107. return $e->getMessage();
  108. }
  109. $this->success('ok');
  110. }
  111. //出售求购
  112. public function sellBuying(LedgerWalletModel $ledgerWalletModel, ProductBuying $productBuying)
  113. {
  114. $params = $this->request->post();
  115. $validate = \think\Loader::validate('Market');
  116. if(!$validate->scene('sellbuying')->check($params)) $this->error($validate->getError());
  117. $buying = $productBuying::get($params['id']);
  118. if(empty($buying)) $this->error('订单不存在');
  119. if($buying->user_id!= $this->auth->id) $this->error('无权操作');
  120. if($buying->status != ProductBuying::Normal) $this->error('订单已完成');
  121. $total = bcmul($buying->min_price, $buying->num, 6); // 所需茶宝
  122. $chabao = $ledgerWalletModel::getWalletChaBao($this->auth->id);
  123. if($chabao < $total) $this->error('您的茶币不足');
  124. // 启动事务
  125. }
  126. //取消求购
  127. public function cancelBuying(ProductBuying $productBuying)
  128. {
  129. $params = $this->request->post();
  130. $validate = \think\Loader::validate('Market');
  131. if(!$validate->scene('cancelbuying')->check($params)) $this->error($validate->getError());
  132. $buying = $productBuying::get($params['id']);
  133. if(empty($buying)) $this->error('订单不存在');
  134. if($buying->user_id!= $this->auth->id) $this->error('无权操作');
  135. if($buying->status != ProductBuying::Normal) $this->error('订单已完成');
  136. $buying->status = ProductBuying::Close;
  137. $buying->save();
  138. }
  139. //获取配置信息
  140. public function getMarketConfig()
  141. {
  142. $this->success('ok', config('market_transfer'));
  143. }
  144. }