Pledge.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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($pledge[$this->pay[$pay_type]] >0 && $user[$this->pay[$pay_type]] < $pledge[$this->pay[$pay_type]]) $this->error(__("余额不足"));
  79. Db::startTrans();
  80. try {
  81. // 质抵押订单
  82. $res = $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. if ($res === false) {
  91. $this->error(__("订单创建失败"));
  92. }
  93. $this->success('ok');
  94. }
  95. /*
  96. * 我的茶矿
  97. */
  98. public function teamine(PledgeLogic $pledgeLogic)
  99. {
  100. try {
  101. // 质抵押订单
  102. $res = $pledgeLogic::getPledgeOrderList($this->auth->id);
  103. } catch (Exception $e) {
  104. $this->error($e->getMessage(), null, $e->getCode());
  105. }
  106. $this->success('ok', $res);
  107. }
  108. /*
  109. * 收取茶矿
  110. */
  111. public function collect(PledgeLogic $pledgeLogic)
  112. {
  113. if(cache('collect_'.$this->auth->id)) $this->error(__("正在挖矿中,请稍后再试"));
  114. Db::startTrans();
  115. try {
  116. // 质抵押订单
  117. $res = $pledgeLogic::getPledgeCollect($this->auth->id);
  118. //请求限制
  119. cache('collect_'.$this->auth->id, time(), 300);
  120. // 提交事务
  121. Db::commit();
  122. } catch (Exception $e) {
  123. // 回滚事务
  124. Db::rollback();
  125. $this->error($e->getMessage(), null, $e->getCode());
  126. }
  127. $this->success('ok', $res);
  128. }
  129. /*
  130. * 解除质押
  131. */
  132. public function remove(PledgeLogic $pledgeLogic)
  133. {
  134. $pledge_id = $this->request->post('pledge_id', 0, 'intval');
  135. $order_id = $this->request->post('order_id/s', '');
  136. if(empty($pledge_id) || empty($order_id)) $this->error(__("参数有误,无可用产品"));
  137. Db::startTrans();
  138. try {
  139. // 质抵押订单
  140. $res = $pledgeLogic::setPledgeRemove($pledge_id, $order_id, $this->auth->id);
  141. // 提交事务
  142. Db::commit();
  143. } catch (Exception $e) {
  144. // 回滚事务
  145. Db::rollback();
  146. $this->error($e->getMessage(), null, $e->getCode());
  147. }
  148. $this->success('ok', $res);
  149. }
  150. //存储续费
  151. public function setRenew(UserPledge $userPledge, PledgeLogic $pledgeLogic, LedgerWalletModel $ledgerWalletModel)
  152. {
  153. $pledge_id = $this->request->post('pledge_id', 0, 'intval');
  154. $pay_type = $this->request->post('pay_type/d', 1);//支付方式 1 茶宝 2 Teac
  155. if(empty($pledge_id) || in_array($pay_type, [1,2]) == false) $this->error(__("参数有误,无可用产品"));
  156. //记录
  157. $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();
  158. if (empty($pledge)) $this->error(__("质抵活动不存在"));
  159. if ($pledge->status != $userPledge::Ongoing || empty($pledge->is_renew) || empty($pledge->p_status))$this->error(__("质抵活动已结束"));
  160. //余额判断
  161. $user = $ledgerWalletModel::getWallet($this->auth->id);
  162. if($pledge[$this->pay[$pay_type]] >0 && $user[$this->pay[$pay_type]] < $pledge[$this->pay[$pay_type]]) $this->error(__("余额不足"));
  163. Db::startTrans();
  164. try {
  165. // 质抵押订单
  166. $pledgeLogic::setPledgeOrderRenew($pledge, $this->auth->id, $this->pay[$pay_type]);
  167. // 提交事务
  168. Db::commit();
  169. } catch (Exception $e) {
  170. // 回滚事务
  171. Db::rollback();
  172. $this->error($e->getMessage(), null, $e->getCode());
  173. }
  174. $this->success('ok');
  175. }
  176. //添加用户存储产品
  177. public function addPledgeProduct(UserPledge $userPledge, PledgeLogic $pledgeLogic, ProductPledges $productPledges)
  178. {
  179. $order_no = $this->request->post('order_no/s', '');
  180. $pledge_id = $this->request->post('pledge_id/d', 0);//存储Id
  181. if(empty($order_no) || empty($pledge_id)) $this->error(__("参数有误,无可用产品"));
  182. $order_arr= explode(',', $order_no);
  183. $count = count($order_arr);
  184. $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();
  185. if (empty($pledge) || $pledge->status != $userPledge::Ongoing || empty($pledge->p_status) || $pledge->type_id != $productPledges::Single) $this->error(__("产品质抵活动不存在不存在"));
  186. if(($count + $pledge->num) > config('pledge_single_max')) $this->error(__("产品数量超出限制"));
  187. Db::startTrans();
  188. try {
  189. // 质抵押订单
  190. $pledgeLogic::setPledgeProductAdd($pledge, $this->auth->id, $count, $order_arr);
  191. // 提交事务
  192. Db::commit();
  193. } catch (Exception $e) {
  194. // 回滚事务
  195. Db::rollback();
  196. $this->error($e->getMessage(), null, $e->getCode());
  197. }
  198. $this->success('ok');
  199. }
  200. }