OfflineWithdraw.php 6.0 KB

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