Withdraw.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. $result = ['total' => $count, 'data' => $list];
  70. } catch (ValidationException $e) {
  71. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  72. } catch (Exception $e) {
  73. return $this->error(intval($e->getCode()));
  74. }
  75. return $this->success($result);
  76. }
  77. /**
  78. * @api {post} /admin/withdraw/setStatus 通过|拒绝
  79. * @apiGroup 提现管理
  80. *
  81. * @apiUse result
  82. * @apiUse header
  83. * @apiVersion 1.0.0
  84. *
  85. * @apiParam {string} id 提现表ID
  86. * @apiParam {int} status 状态
  87. * - 1 通过
  88. * - 2 拒绝
  89. */
  90. public function setStatus()
  91. {
  92. DB::beginTransaction();
  93. try {
  94. request()->validate([
  95. 'id' => ['required', 'string', 'min:1'],
  96. 'status' => ['required', 'integer', 'min:1', 'max:2']
  97. ]);
  98. $id = request()->input('id');
  99. $status = request()->input('status');
  100. $w = WithdrawService::findOne(['id' => $id, 'status' => 0]);
  101. if (!$w) throw new Exception("数据不存在", HttpStatus::CUSTOM_ERROR);
  102. // 汇率
  103. $rate = $w->exchange_rate ?? 1;
  104. if ($status == 1) {
  105. $w->status = 1;
  106. $w->save();
  107. } else if ($status == 2) {
  108. $rate_rmb_amount = bcmul($w->amount, $rate, 2); // 提现金额 折合RMB
  109. $w->status = 2;
  110. $w->save();
  111. $wallet = WalletService::findOne(['member_id' => $w->member_id]);
  112. $afterBalance = bcadd($wallet->available_balance, $rate_rmb_amount, 10);
  113. BalanceLogService::addLog(
  114. $w->member_id,
  115. $w->rate_rmb_amount,
  116. $wallet->available_balance,
  117. $afterBalance,
  118. '提现',
  119. $w->id,
  120. ''
  121. );
  122. $wallet->available_balance = $afterBalance;
  123. $wallet->save();
  124. }
  125. $arr = ['⏳️申请中', '✅️成功', '❌️失败'];
  126. $text = "📢 提现结果通知\n\n";
  127. $temp = floatval($w->service_charge);
  128. $text .= "手续费:{$temp} USDT\n";
  129. $temp = floatval($w->amount);
  130. $text .= "提现金额:{$temp} USDT\n";
  131. $temp = floatval($w->to_account);
  132. $text .= "到账金额:{$temp} USDT\n";
  133. $text .= "提现地址:{$w->address}\n\n";
  134. $text .= "状态:{$arr[$w->status]}\n";
  135. if ($w->remark) $text .= "说明:{$w->remark}";
  136. $res = WithdrawService::notify([
  137. 'chat_id' => $w->member_id,
  138. 'text' => $text,
  139. ]);
  140. DB::commit();
  141. } catch (ValidationException $e) {
  142. DB::rollBack();
  143. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  144. } catch (Exception $e) {
  145. DB::rollBack();
  146. return $this->error(intval($e->getCode()), $e->getMessage());
  147. }
  148. return $this->success($res);
  149. }
  150. }