Withdraw.php 6.1 KB

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