Pledge.php 9.9 KB

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