Market.php 7.9 KB

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