OfflineWithdraw.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace app\admin\controller\offline;
  3. use app\common\controller\Backend;
  4. use app\common\logic\BscApi;
  5. use app\common\logic\KangApi;
  6. use app\common\logic\MyBscApi;
  7. use app\common\model\LedgerWalletModel;
  8. use app\common\model\OfflineWithdrawRecordModel;
  9. use app\common\model\UserModel;
  10. use Exception;
  11. use fast\Action;
  12. use fast\Asset;
  13. use fast\WithdrawStatus;
  14. use think\Cache;
  15. use think\Db;
  16. use think\Env;
  17. use think\exception\DbException;
  18. use think\Model;
  19. use app\api\controller\Offline as OfflineApi;
  20. use think\response\Json;
  21. /**
  22. * 提现记录管理
  23. *
  24. * @icon fa fa-circle-o
  25. */
  26. class OfflineWithdraw extends Backend
  27. {
  28. /**
  29. * OfflineWithdrawRecord模型对象
  30. * @var \app\admin\model\OfflineWithdrawRecord
  31. */
  32. protected $model = null;
  33. protected $urls = null;
  34. protected $api_key = null;
  35. protected $api_secret= null;
  36. protected $address = null;
  37. protected $bnb_fee = 0.01;//最少手续费
  38. public function _initialize()
  39. {
  40. parent::_initialize();
  41. $this->model = new \app\admin\model\OfflineWithdrawRecord;
  42. $this->address = Env::get('rental.out_address');
  43. }
  44. /**
  45. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  46. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  47. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  48. */
  49. /**
  50. * 查看
  51. *
  52. * @return string|Json
  53. * @throws \think\Exception
  54. * @throws DbException
  55. */
  56. public function index()
  57. {
  58. //设置过滤方法
  59. $this->request->filter(['strip_tags', 'trim']);
  60. if (false === $this->request->isAjax()) {
  61. return $this->view->fetch();
  62. }
  63. //如果发送的来源是 Selectpage,则转发到 Selectpage
  64. if ($this->request->request('keyField')) {
  65. return $this->selectpage();
  66. }
  67. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  68. $list = $this->model->alias('r')
  69. ->join('user u','r.user_id=u.id')
  70. ->where($where)
  71. ->field('r.*,u.address')
  72. ->order($sort, $order)
  73. ->paginate($limit);
  74. foreach ($list as $k => $v) {
  75. $list[$k]['address'] = (new UserModel())->getById($v['user_id'])['address'];
  76. $list[$k]['symbol'] = strtoupper($v['symbol']);
  77. }
  78. $result = ['total' => $list->total(), 'rows' => $list->items(), 'total_revenue'=> 669];
  79. return json($result);
  80. }
  81. /**
  82. * 提现审核
  83. * @param $ids
  84. * @return string|void
  85. * @throws \think\Exception
  86. * @throws \think\exception\DbException
  87. */
  88. public function approve($ids = null)
  89. {
  90. $row = $this->model->get($ids);
  91. if (!$row) {
  92. $this->error(__('No Results were found'));
  93. }
  94. $adminIds = $this->getDataLimitAdminIds();
  95. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  96. $this->error(__('You have no permission'));
  97. }
  98. $user = (new UserModel())->getById($row['user_id']);
  99. if(empty($user)){
  100. $this->error("用户信息不存在");
  101. }
  102. if (false === $this->request->isPost()) {
  103. $row['address'] = !empty($user) ? $user['address'] : "";
  104. $this->view->assign('row', $row);
  105. return $this->view->fetch();
  106. }
  107. $params = $this->request->post('row/a');
  108. if (empty($params)) {
  109. $this->error(__('Parameter %s can not be empty', ''));
  110. }
  111. $params = $this->preExcludeFields($params);
  112. $result = false;
  113. Db::startTrans();
  114. try {
  115. //是否采用模型验证
  116. if ($this->modelValidate) {
  117. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  118. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  119. $row->validateFailException()->validate($validate);
  120. }
  121. $approved = in_array($params['status'], [WithdrawStatus::StatusApprove, WithdrawStatus::StatusManual]); // true:通过 false:拒绝 $params['status'] ==
  122. $update = [
  123. 'status' => $params['status'],
  124. 'tx_hash' => $approved ? $params['tx_hash'] : $row['tx_hash'],
  125. 'update_time' => time(),
  126. ];
  127. //自动动打款调用接口
  128. if ($params['status'] == WithdrawStatus::StatusApprove) {
  129. if($row['symbol'] != 'usdt'){
  130. throw new Exception("自动打款只支持USDT提现订单");
  131. }
  132. //(new OfflineWithdrawRecordModel())->where('id', $row->id)->update(['status' => 1]);
  133. $req = (new MyBscApi())->createWithdraw($row->id);
  134. //$req = (new KangApi())->createWithdraw($row->id);
  135. if ($req['code'] == 0){
  136. throw new Exception("打款失败:" . $req['msg']);
  137. }
  138. $update['status'] = OfflineWithdrawRecordModel::StatusConfirm;
  139. }
  140. // 仅处理"待处理"状态的订单,"已抢单"的暂不处理
  141. $result = (new OfflineWithdrawRecordModel())->where('id', $row['id'])->where('status', OfflineWithdrawRecordModel::StatusDefault)->update($update);
  142. if (empty($result)) {
  143. throw new Exception("修改订单状态失败");
  144. }
  145. // 拒绝时,将提交的金额退回到用户余额里
  146. if ($params['status'] == OfflineWithdrawRecordModel::StatusReturn) {
  147. (new LedgerWalletModel())->changeWalletAccount($row['user_id'], Asset::USDT, $row['amount'], Action::UsdtWithdrawReturn);
  148. }
  149. Db::commit();
  150. } catch (\Exception $e) {
  151. Db::rollback();
  152. $this->error($e->getMessage());
  153. }
  154. if (false === $result) {
  155. $this->error(__('No rows were updated'));
  156. }
  157. $this->success();
  158. }
  159. //获取余额
  160. public function getBalance()
  161. {
  162. $reqBalance = 0;
  163. $reqFee = 0;
  164. $reqAmount = (new OfflineWithdrawRecordModel())->where('status', 'in', '0,5')->sum('real_amount');
  165. //手续费
  166. $fee = (new BscApi())->getBnbBalance($this->address);
  167. if ($fee['code'] == 1) $reqFee = round($fee['data'], 6);
  168. //Usdt余额
  169. $balan = (new BscApi)->getTokenBalance($this->address);
  170. if ($fee['code'] == 1) $reqBalance = round($balan['data'], 6);
  171. return json(['reqBalance'=>$reqBalance, 'reqFee'=>$reqFee, 'reqAmount'=>$reqAmount]);
  172. }
  173. }