Pledge.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\ProductLists;
  5. use app\common\model\ProductOrder;
  6. use app\common\model\ProductPledges;
  7. use Exception;
  8. use app\common\model\UserModel;
  9. use think\Db;
  10. use app\common\logic\PledgeLogic;
  11. //质押抵扣
  12. class Pledge extends Api
  13. {
  14. protected string $lan = '';
  15. public function _initialize()
  16. {
  17. parent::_initialize();
  18. $this->lan = $this->request->getLan();
  19. }
  20. //质押列表
  21. public function list(ProductPledges $productPledges, PledgeLogic $pledgeLogic)
  22. {
  23. $type_id = $this->request->param('type_id', 0, 'intval');
  24. if(empty($type_id)) $this->error(__("参数有误,无可用产品"));
  25. $list = $productPledges
  26. ->where('status', $productPledges::Normal)
  27. ->where('to_lang', $this->lan)->where('type_id', $type_id)
  28. ->field('id,title,day_num,income_reta,product_id')
  29. ->order('weigh desc')
  30. ->paginate($this->pageSize);
  31. $list = $pledgeLogic::getByProductIdList($list, $this->lan);
  32. $this->success('', $list);
  33. }
  34. /*
  35. * 质押详情
  36. */
  37. public function detail(ProductPledges $productPledges, ProductLists $productList)
  38. {
  39. $id = $this->request->param('id', 0, 'intval');
  40. if(empty($id)) $this->error(__("参数有误,无可用产品"));
  41. //质押详情
  42. $pledges = $productPledges::get($id);
  43. if (!empty($pledges)) {
  44. $pledges->product_list = $productList::getBySynthesisProduct($pledges->product_id, $this->lan);
  45. }
  46. $this->success('', $pledges);
  47. }
  48. /*
  49. * 持有商品列表
  50. */
  51. public function holdProductList(PledgeLogic $pledgeLogic)
  52. {
  53. $product_id = $this->request->param('product_id', 0, 'intval');
  54. if(empty($product_id)) $this->error(__("参数有误,无可用产品"));
  55. $pledges = $pledgeLogic::getHoldProductList($this->auth->id, $product_id);
  56. $this->success('', $pledges);
  57. }
  58. /*
  59. * 质押存储
  60. */
  61. public function create(ProductPledges $productPledges, PledgeLogic $pledgeLogic)
  62. {
  63. $pledge_id = $this->request->post('pledge_id', 0, 'intval');
  64. $order_no = $this->request->post('order_no/a', '');
  65. if(empty($pledge_id) || empty($order_no)) $this->error(__("参数有误,无可用产品"));
  66. $pledge = $productPledges::get($pledge_id);
  67. if($pledge->type_id == $productPledges::Combin && count(explode(',', $pledge->product_id)) != count($order_no)) $this->error(__("质押商品数量与订单数量不匹配"));
  68. if (empty($pledge)) $this->error(__("质抵活动不存在"));
  69. if (empty($pledge->status) )$this->error(__("质抵活动已结束"));
  70. Db::startTrans();
  71. try {
  72. // 质抵押订单
  73. $pledgeLogic::setPledgeOrder($pledge, $order_no, $this->auth->id);
  74. // 提交事务
  75. Db::commit();
  76. } catch (Exception $e) {
  77. // 回滚事务
  78. Db::rollback();
  79. $this->error($e->getMessage(), null, $e->getCode());
  80. }
  81. $this->success('ok');
  82. }
  83. /*
  84. * 我的茶矿
  85. */
  86. public function teamine(PledgeLogic $pledgeLogic)
  87. {
  88. try {
  89. // 质抵押订单
  90. $res = $pledgeLogic::getPledgeOrderList($this->auth->id);
  91. } catch (Exception $e) {
  92. $this->error($e->getMessage(), null, $e->getCode());
  93. }
  94. $this->success('ok', $res);
  95. }
  96. /*
  97. * 收取茶矿
  98. */
  99. public function collect(PledgeLogic $pledgeLogic)
  100. {
  101. if(cache('collect_'.$this->auth->id)) $this->error(__("正在挖矿中,请稍后再试"));
  102. Db::startTrans();
  103. try {
  104. // 质抵押订单
  105. $res = $pledgeLogic::getPledgeCollect($this->auth->id);
  106. //请求限制
  107. cache('collect_'.$this->auth->id, time(), 300);
  108. // 提交事务
  109. Db::commit();
  110. } catch (Exception $e) {
  111. // 回滚事务
  112. Db::rollback();
  113. $this->error($e->getMessage(), null, $e->getCode());
  114. }
  115. $this->success('ok', $res);
  116. }
  117. /*
  118. * 解除质押
  119. */
  120. public function remove(PledgeLogic $pledgeLogic)
  121. {
  122. $pledge_id = $this->request->post('pledge_id', 0, 'intval');
  123. if(empty($pledge_id)) $this->error(__("参数有误,无可用产品"));
  124. Db::startTrans();
  125. try {
  126. // 质抵押订单
  127. $res = $pledgeLogic::setPledgeRemove($pledge_id, $this->auth->id);
  128. // 提交事务
  129. Db::commit();
  130. } catch (Exception $e) {
  131. // 回滚事务
  132. Db::rollback();
  133. $this->error($e->getMessage(), null, $e->getCode());
  134. }
  135. $this->success('ok', $res);
  136. }
  137. }