Withdraw.php 7.4 KB

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