Market.php 7.8 KB

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