Pledge.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. $pledges['day_num'] = round($pledges['day_num'], 2);
  49. $this->success('', $pledges);
  50. }
  51. /*
  52. * 持有商品列表
  53. */
  54. public function holdProductList(PledgeLogic $pledgeLogic)
  55. {
  56. $product_id = $this->request->param('product_id', 0, 'intval');
  57. if(empty($product_id)) $this->error(__("参数有误,无可用产品"));
  58. $pledges = $pledgeLogic::getHoldProductList($this->auth->id, $product_id);
  59. $this->success('', $pledges);
  60. }
  61. /*
  62. * 质押存储
  63. */
  64. public function create(ProductPledges $productPledges, PledgeLogic $pledgeLogic, LedgerWalletModel $ledgerWalletModel)
  65. {
  66. $pledge_id = $this->request->post('pledge_id', 0, 'intval');
  67. $order_no = $this->request->post('order_no/a', '');
  68. $pay_type = $this->request->post('pay_type/d', 1);//支付方式 1 茶宝 2 Teac
  69. if(empty($pledge_id) || empty($order_no) || in_array($pay_type, [1,2]) == false) $this->error(__("参数有误,无可用产品"));
  70. $pledge = $productPledges::get($pledge_id);
  71. $count = count($order_no);//选择商品数量
  72. if($pledge->type_id == $productPledges::Combin && count(explode(',', $pledge->product_id)) != $count) $this->error(__("质押商品数量与订单数量不匹配"));
  73. //单品最大三个限制
  74. if($pledge->type_id == $productPledges::Single && $count > config('pledge_single_max')) $this->error(__("单品最大三个限制"));
  75. if (empty($pledge)) $this->error(__("质抵活动不存在"));
  76. if (empty($pledge->status) )$this->error(__("质抵活动已结束"));
  77. //余额判断
  78. $user = $ledgerWalletModel::getWallet($this->auth->id);
  79. if($pledge[$this->pay[$pay_type]] >0 && $user[$this->pay[$pay_type]] < $pledge[$this->pay[$pay_type]]) $this->error(__("余额不足"));
  80. Db::startTrans();
  81. try {
  82. // 质抵押订单
  83. $res = $pledgeLogic::setPledgeOrder($pledge, $order_no, $this->auth->id, $count, $this->pay[$pay_type], $pledge[$this->pay[$pay_type]]);
  84. // 提交事务
  85. Db::commit();
  86. } catch (Exception $e) {
  87. // 回滚事务
  88. Db::rollback();
  89. $this->error($e->getMessage(), null, $e->getCode());
  90. }
  91. if ($res === false) {
  92. $this->error(__("订单创建失败"));
  93. }
  94. $this->success('ok');
  95. }
  96. /*
  97. * 我的茶矿
  98. */
  99. public function teamine(PledgeLogic $pledgeLogic)
  100. {
  101. try {
  102. // 质抵押订单
  103. $res = $pledgeLogic::getPledgeOrderList($this->auth->id);
  104. } catch (Exception $e) {
  105. $this->error($e->getMessage(), null, $e->getCode());
  106. }
  107. $this->success('ok', $res);
  108. }
  109. /*
  110. * 收取茶矿
  111. */
  112. public function collect(PledgeLogic $pledgeLogic)
  113. {
  114. if(cache('collect_'.$this->auth->id)) $this->error(__("正在挖矿中,请稍后再试"));
  115. Db::startTrans();
  116. try {
  117. // 质抵押订单
  118. $res = $pledgeLogic::getPledgeCollect($this->auth->id);
  119. //请求限制
  120. cache('collect_'.$this->auth->id, time(), 300);
  121. // 提交事务
  122. Db::commit();
  123. } catch (Exception $e) {
  124. // 回滚事务
  125. Db::rollback();
  126. $this->error($e->getMessage(), null, $e->getCode());
  127. }
  128. $this->success('ok', $res);
  129. }
  130. /*
  131. * 解除质押
  132. */
  133. public function remove(PledgeLogic $pledgeLogic)
  134. {
  135. $pledge_id = $this->request->post('pledge_id', 0, 'intval');
  136. $order_id = $this->request->post('order_id/s', '');
  137. if(empty($pledge_id) || empty($order_id)) $this->error(__("参数有误,无可用产品"));
  138. Db::startTrans();
  139. try {
  140. // 质抵押订单
  141. $res = $pledgeLogic::setPledgeRemove($pledge_id, $order_id, $this->auth->id);
  142. // 提交事务
  143. Db::commit();
  144. } catch (Exception $e) {
  145. // 回滚事务
  146. Db::rollback();
  147. $this->error($e->getMessage(), null, $e->getCode());
  148. }
  149. $this->success('ok', $res);
  150. }
  151. //存储续费
  152. public function setRenew(UserPledge $userPledge, PledgeLogic $pledgeLogic, LedgerWalletModel $ledgerWalletModel)
  153. {
  154. $pledge_id = $this->request->post('pledge_id', 0, 'intval');
  155. $pay_type = $this->request->post('pay_type/d', 1);//支付方式 1 茶宝 2 Teac
  156. if(empty($pledge_id) || in_array($pay_type, [1,2]) == false) $this->error(__("参数有误,无可用产品"));
  157. //记录
  158. $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();
  159. if (empty($pledge)) $this->error(__("质抵活动不存在"));
  160. if ($pledge->status != $userPledge::Ongoing || empty($pledge->is_renew) || empty($pledge->p_status))$this->error(__("质抵活动已结束"));
  161. //余额判断
  162. $user = $ledgerWalletModel::getWallet($this->auth->id);
  163. if($pledge[$this->pay[$pay_type]] >0 && $user[$this->pay[$pay_type]] < $pledge[$this->pay[$pay_type]]) $this->error(__("余额不足"));
  164. Db::startTrans();
  165. try {
  166. // 质抵押订单
  167. $pledgeLogic::setPledgeOrderRenew($pledge, $this->auth->id, $this->pay[$pay_type]);
  168. // 提交事务
  169. Db::commit();
  170. } catch (Exception $e) {
  171. // 回滚事务
  172. Db::rollback();
  173. $this->error($e->getMessage(), null, $e->getCode());
  174. }
  175. $this->success('ok');
  176. }
  177. //添加用户存储产品
  178. public function addPledgeProduct(UserPledge $userPledge, PledgeLogic $pledgeLogic, ProductPledges $productPledges)
  179. {
  180. $order_no = $this->request->post('order_no/s', '');
  181. $pledge_id = $this->request->post('pledge_id/d', 0);//存储Id
  182. if(empty($order_no) || empty($pledge_id)) $this->error(__("参数有误,无可用产品"));
  183. $order_arr= explode(',', $order_no);
  184. $count = count($order_arr);
  185. $pledge = $userPledge::alias('a')->where('a.id', $pledge_id)->join('product_pledge p', 'p.id = a.pledge_id')->field('a.*,p.type_id,p.status as p_status')->find();
  186. if (empty($pledge) || $pledge->status != $userPledge::Ongoing || empty($pledge->p_status) || $pledge->type_id != $productPledges::Single) $this->error(__("产品质抵活动不存在不存在"));
  187. if(($count + $pledge->num) > config('pledge_single_max')) $this->error(__("产品数量超出限制"));
  188. Db::startTrans();
  189. try {
  190. // 质抵押订单
  191. $pledgeLogic::setPledgeProductAdd($pledge, $this->auth->id, $count, $order_arr);
  192. // 提交事务
  193. Db::commit();
  194. } catch (Exception $e) {
  195. // 回滚事务
  196. Db::rollback();
  197. $this->error($e->getMessage(), null, $e->getCode());
  198. }
  199. $this->success('ok');
  200. }
  201. }