Pledge.php 9.5 KB

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