Pledge.php 7.6 KB

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