Market.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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()
  75. {
  76. }
  77. /**
  78. * 发起求购
  79. * @return void
  80. */
  81. public function setBuying(LedgerWalletModel $ledgerWalletModel, ProductBuying $productBuying, ProductOrder $productOrder)
  82. {
  83. $params = $this->request->post();
  84. $validate = \think\Loader::validate('Market');
  85. if(!$validate->scene('buying')->check($params)) $this->error($validate->getError());
  86. if($productBuying::getProductBuyingCount($this->auth->id, $params['product_id']) > config('market_buying.max_buying_num')) $this->error('您的求购次数已达上限');
  87. $chabao = $ledgerWalletModel::getWalletChaBao($this->auth->id);
  88. $total = bcmul($params['min_price'], $params['num'], 6); // 所需茶宝
  89. if($chabao < $total) $this->error('您的茶币不足');
  90. // 启动事务
  91. Db::startTrans();
  92. try {
  93. // 记录订单
  94. $productBuying::setCreateBuying($this->auth->id, $params['product_id'], $params['num'], $params['min_price'], $total);
  95. // 扣除茶币
  96. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$total, LedgerTokenChangeModel::BuySellg, $this->auth->id);
  97. $ledgerWalletModel->changeWalletOnly($this->auth->id, Asset::BUYING, $total); //冻结金额
  98. // 提交事务
  99. Db::commit();
  100. } catch (Exception $e) {
  101. // 回滚事务
  102. Db::rollback();
  103. return $e->getMessage();
  104. }
  105. $this->success('ok');
  106. }
  107. //获取配置信息
  108. public function getMarketConfig()
  109. {
  110. $this->success('ok', config('market_transfer'));
  111. }
  112. }