Pledge.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\ProductLists;
  5. use app\common\model\LedgerWalletModel;
  6. use app\common\model\ProductPledges;
  7. use Exception;
  8. use app\common\model\UserModel;
  9. use think\Db;
  10. use app\api\logic\PledgeLogic;
  11. //质押抵扣
  12. class Pledge extends Api
  13. {
  14. protected string $lan = '';
  15. protected $pay = [1=>'token', 2=>'teac'];
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->lan = $this->request->getLan();
  20. }
  21. //质押列表
  22. public function list(ProductPledges $productPledges, PledgeLogic $pledgeLogic)
  23. {
  24. $type_id = $this->request->param('type_id', 0, 'intval');
  25. if(empty($type_id)) $this->error(__("参数有误,无可用产品"));
  26. $list = $productPledges
  27. ->where('status', $productPledges::Normal)
  28. ->where('to_lang', $this->lan)->where('type_id', $type_id)
  29. ->field('id,title,day_num,income_reta,product_id,type_id')
  30. ->order('weigh desc')
  31. ->paginate($this->pageSize);
  32. $list = $pledgeLogic::getByProductIdList($list, $this->lan);
  33. $this->success('', $list);
  34. }
  35. /*
  36. * 质押详情
  37. */
  38. public function detail(ProductPledges $productPledges, ProductLists $productList)
  39. {
  40. $id = $this->request->param('id', 0, 'intval');
  41. if(empty($id)) $this->error(__("参数有误,无可用产品"));
  42. //质押详情
  43. $pledges = $productPledges::get($id);
  44. if (!empty($pledges)) {
  45. $pledges->product_list = $productList::getBySynthesisProduct($pledges->product_id, $pledges->type_id, $this->lan);
  46. }
  47. $this->success('', $pledges);
  48. }
  49. /*
  50. * 持有商品列表
  51. */
  52. public function holdProductList(PledgeLogic $pledgeLogic)
  53. {
  54. $product_id = $this->request->param('product_id', 0, 'intval');
  55. if(empty($product_id)) $this->error(__("参数有误,无可用产品"));
  56. $pledges = $pledgeLogic::getHoldProductList($this->auth->id, $product_id);
  57. $this->success('', $pledges);
  58. }
  59. /*
  60. * 质押存储
  61. */
  62. public function create(ProductPledges $productPledges, PledgeLogic $pledgeLogic, LedgerWalletModel $ledgerWalletModel)
  63. {
  64. $pledge_id = $this->request->post('pledge_id', 0, 'intval');
  65. $order_no = $this->request->post('order_no/a', '');
  66. $pay_type = $this->request->post('pay_type/d', 1);//支付方式 1 茶宝 2 Teac
  67. if(empty($pledge_id) || empty($order_no) || in_array($pay_type, [1,2]) == false) $this->error(__("参数有误,无可用产品"));
  68. $pledge = $productPledges::get($pledge_id);
  69. $count = count($order_no);//选择商品数量
  70. if($pledge->type_id == $productPledges::Combin && count(explode(',', $pledge->product_id)) != $count) $this->error(__("质押商品数量与订单数量不匹配"));
  71. //单品最大三个限制
  72. if($pledge->type_id == $productPledges::Single && $count > config('pledge_single_max')) $this->error(__("单品最大三个限制"));
  73. if (empty($pledge)) $this->error(__("质抵活动不存在"));
  74. if (empty($pledge->status) )$this->error(__("质抵活动已结束"));
  75. //余额判断
  76. $user = $ledgerWalletModel::getWallet($this->auth->id);
  77. if($user[$this->pay[$pay_type]] < $pledge[$this->pay[$pay_type]]) $this->error(__("余额不足"));
  78. Db::startTrans();
  79. try {
  80. // 质抵押订单
  81. $pledgeLogic::setPledgeOrder($pledge, $order_no, $this->auth->id, $count, $this->pay[$pay_type], $pledge[$this->pay[$pay_type]]);
  82. // 提交事务
  83. Db::commit();
  84. } catch (Exception $e) {
  85. // 回滚事务
  86. Db::rollback();
  87. $this->error($e->getMessage(), null, $e->getCode());
  88. }
  89. $this->success('ok');
  90. }
  91. /*
  92. * 我的茶矿
  93. */
  94. public function teamine(PledgeLogic $pledgeLogic)
  95. {
  96. try {
  97. // 质抵押订单
  98. $res = $pledgeLogic::getPledgeOrderList($this->auth->id);
  99. } catch (Exception $e) {
  100. $this->error($e->getMessage(), null, $e->getCode());
  101. }
  102. $this->success('ok', $res);
  103. }
  104. /*
  105. * 收取茶矿
  106. */
  107. public function collect(PledgeLogic $pledgeLogic)
  108. {
  109. if(cache('collect_'.$this->auth->id)) $this->error(__("正在挖矿中,请稍后再试"));
  110. Db::startTrans();
  111. try {
  112. // 质抵押订单
  113. $res = $pledgeLogic::getPledgeCollect($this->auth->id);
  114. //请求限制
  115. cache('collect_'.$this->auth->id, time(), 300);
  116. // 提交事务
  117. Db::commit();
  118. } catch (Exception $e) {
  119. // 回滚事务
  120. Db::rollback();
  121. $this->error($e->getMessage(), null, $e->getCode());
  122. }
  123. $this->success('ok', $res);
  124. }
  125. /*
  126. * 解除质押
  127. */
  128. public function remove(PledgeLogic $pledgeLogic)
  129. {
  130. $pledge_id = $this->request->post('pledge_id', 0, 'intval');
  131. if(empty($pledge_id)) $this->error(__("参数有误,无可用产品"));
  132. Db::startTrans();
  133. try {
  134. // 质抵押订单
  135. $res = $pledgeLogic::setPledgeRemove($pledge_id, $this->auth->id);
  136. // 提交事务
  137. Db::commit();
  138. } catch (Exception $e) {
  139. // 回滚事务
  140. Db::rollback();
  141. $this->error($e->getMessage(), null, $e->getCode());
  142. }
  143. $this->success('ok', $res);
  144. }
  145. //存储续费
  146. }