Withdraw.php 5.1 KB

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