Withdraw.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Services\BalanceLogService;
  6. use App\Services\TopUpService;
  7. use App\Services\WalletService;
  8. use App\Services\WithdrawService;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Validation\ValidationException;
  11. use Exception;
  12. use App\Models\Withdraw as WithdrawModel;
  13. use App\Models\Config;
  14. class Withdraw extends Controller
  15. {
  16. /**
  17. * @api {get} /admin/withdraw 提现列表
  18. * @apiGroup 提现管理
  19. *
  20. * @apiUse result
  21. * @apiUse header
  22. * @apiVersion 1.0.0
  23. *
  24. * @apiParam {int} [page=1]
  25. * @apiParam {int} [limit=10]
  26. * @apiParam {string} [member_id] tg会员ID
  27. * @apiParam {int} [status] 状态
  28. * - 0 申请中
  29. * - 1 通过
  30. * - 2 拒绝
  31. * @apiSuccess (data) {Object} data
  32. * @apiSuccess (data) {int} data.total 数量
  33. * @apiSuccess (data) {Object[]} data.data 列表
  34. * @apiSuccess (data) {int} data.data.id
  35. * @apiSuccess (data) {int} data.data.member_id tg会员id
  36. * @apiSuccess (data) {string} data.data.amount 提现金额
  37. * @apiSuccess (data) {string} data.data.service_charge 手续费
  38. * @apiSuccess (data) {string} data.data.to_account 到账金额
  39. * @apiSuccess (data) {string} data.data.after_balance 提现后余额
  40. * @apiSuccess (data) {string} data.data.address 钱包地址
  41. * @apiSuccess (data) {string} data.data.updated_at
  42. * @apiSuccess (data) {string} data.data.created_at
  43. * @apiSuccess (data) {int} data.data.status 状态
  44. * - 0 申请中
  45. * - 1 通过
  46. * - 2 拒绝
  47. * @apiSuccess (data) {string} data.data.remark
  48. *
  49. *
  50. */
  51. public function index()
  52. {
  53. try {
  54. $params = request()->validate([
  55. 'page' => ['nullable', 'integer', 'min:1'],
  56. 'limit' => ['nullable', 'integer', 'min:1'],
  57. 'member_id' => ['nullable', 'string', 'min:1'],
  58. 'status' => ['nullable', 'integer', 'min:0', 'max:2']
  59. ]);
  60. $page = request()->input('page', 1);
  61. $limit = request()->input('limit', 10);
  62. $query = WithdrawModel::query();
  63. $where = WithdrawService::getWhere($params);
  64. $count = $query->where($where)->count();
  65. $list = $query->where($where)
  66. ->with(['member'])
  67. ->orderBy('created_at', 'desc')
  68. ->forpage($page, $limit)->get();
  69. foreach ($list as &$item){
  70. $item['exchange_rate'] = floatval($item['service_charge']);
  71. }
  72. $result = ['total' => $count, 'data' => $list];
  73. } catch (ValidationException $e) {
  74. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  75. } catch (Exception $e) {
  76. return $this->error(intval($e->getCode()));
  77. }
  78. return $this->success($result);
  79. }
  80. /**
  81. * @api {post} /admin/withdraw/setStatus 通过|拒绝
  82. * @apiGroup 提现管理
  83. *
  84. * @apiUse result
  85. * @apiUse header
  86. * @apiVersion 1.0.0
  87. *
  88. * @apiParam {string} id 提现表ID
  89. * @apiParam {int} status 状态
  90. * - 1 通过
  91. * - 2 拒绝
  92. */
  93. public function setStatus()
  94. {
  95. DB::beginTransaction();
  96. try {
  97. request()->validate([
  98. 'id' => ['required', 'string', 'min:1'],
  99. 'status' => ['required', 'integer', 'min:1', 'max:2']
  100. ]);
  101. $id = request()->input('id');
  102. $status = request()->input('status');
  103. $w = WithdrawService::findOne(['id' => $id, 'status' => 0]);
  104. if (!$w) throw new Exception("数据不存在", HttpStatus::CUSTOM_ERROR);
  105. // 汇率
  106. $rate = $w->exchange_rate ?? 1;
  107. if ($status == 1) {
  108. $w->status = 1;
  109. $w->save();
  110. } else if ($status == 2) {
  111. $rate_rmb_amount = bcmul($w->amount, $rate, 2); // 提现金额 折合RMB
  112. $w->status = 2;
  113. $w->save();
  114. $wallet = WalletService::findOne(['member_id' => $w->member_id]);
  115. $afterBalance = bcadd($wallet->available_balance, $rate_rmb_amount, 10);
  116. BalanceLogService::addLog(
  117. $w->member_id,
  118. $w->rate_rmb_amount,
  119. $wallet->available_balance,
  120. $afterBalance,
  121. '提现',
  122. $w->id,
  123. ''
  124. );
  125. $wallet->available_balance = $afterBalance;
  126. $wallet->save();
  127. }
  128. $arr = ['⏳️申请中', '✅️成功', '❌️失败'];
  129. $text = "📢 提现结果通知\n\n";
  130. $temp = floatval($w->service_charge);
  131. $text .= "手续费:{$temp} USDT\n";
  132. $temp = floatval($w->amount);
  133. $text .= "提现金额:{$temp} USDT\n";
  134. $temp = floatval($w->to_account);
  135. $text .= "到账金额:{$temp} USDT\n";
  136. $text .= "提现地址:{$w->address}\n\n";
  137. $text .= "状态:{$arr[$w->status]}\n";
  138. if ($w->remark) $text .= "说明:{$w->remark}";
  139. $res = WithdrawService::notify([
  140. 'chat_id' => $w->member_id,
  141. 'text' => $text,
  142. ]);
  143. DB::commit();
  144. } catch (ValidationException $e) {
  145. DB::rollBack();
  146. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  147. } catch (Exception $e) {
  148. DB::rollBack();
  149. return $this->error(intval($e->getCode()), $e->getMessage());
  150. }
  151. return $this->success($res);
  152. }
  153. }