Pledge.php 9.8 KB

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