OfflineWithdraw.php 6.1 KB

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