WalletController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace App\Http\Controllers\agent;
  3. use App\Http\Controllers\AgentApiController;
  4. use App\Models\Agent\AgentBalanceLog;
  5. use App\Models\Agent\AgentWithdrawAccount;
  6. use App\Models\Agent\AgentWithdrawal;
  7. use App\Models\Agent\AgentRecharge;
  8. use App\Services\Agent\AgentExchangeRate;
  9. use App\Services\Agent\AgentRechargeService;
  10. use App\Services\Agent\AgentWalletService;
  11. use App\Services\Agent\AgentWithdrawalService;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Facades\DB;
  14. use App\Rules\DecimalNumber;
  15. class WalletController extends AgentApiController
  16. {
  17. public function logs(Request $request)
  18. {
  19. return $this->execute(function () use ($request) {
  20. $data = $request->validate([
  21. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  22. 'type' => ['nullable', 'string', 'max:32'], 'start_date' => ['nullable', 'date_format:Y-m-d'],
  23. 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  24. ]);
  25. return $this->pageQuery(AgentBalanceLog::query()->where('agent_id', $this->currentAgent()->id), $data);
  26. });
  27. }
  28. public function accounts(Request $request)
  29. {
  30. return $this->execute(function () use ($request) {
  31. return AgentWithdrawAccount::query()->where('agent_id', $this->currentAgent()->id)
  32. ->where('type', 'usdt')->orderByDesc('id')->get();
  33. });
  34. }
  35. public function saveAccount(Request $request)
  36. {
  37. return $this->execute(function () use ($request) {
  38. // 代理提现仅支持 USDT,地址按公链校验格式。
  39. $data = $request->validate([
  40. 'id' => ['nullable', 'integer'], 'name' => ['nullable', 'string', 'max:128'],
  41. 'network' => ['required', 'in:TRC20,ERC20'],
  42. 'account' => ['required', 'string', 'max:255', function ($attribute, $value, $fail) use ($request) {
  43. $pattern = $request->input('network') === 'ERC20' ? '/^0x[0-9a-fA-F]{40}$/D' : '/^T[1-9A-HJ-NP-Za-km-z]{33}$/D';
  44. if (!preg_match($pattern, (string) $value)) $fail('USDT 地址格式错误');
  45. }],
  46. 'status' => ['nullable', 'integer', 'in:0,1'],
  47. ]);
  48. $data['type'] = 'usdt';
  49. $data['bank_name'] = '';
  50. $isNew = empty($data['id']);
  51. foreach (['name', 'network'] as $field) {
  52. if ($isNew || array_key_exists($field, $data)) {
  53. $data[$field] = trim((string) ($data[$field] ?? ''));
  54. }
  55. }
  56. if ($isNew || array_key_exists('status', $data)) {
  57. $data['status'] = $data['status'] ?? 1;
  58. }
  59. $agent = $this->currentAgent();
  60. $account = $isNew ? new AgentWithdrawAccount() : AgentWithdrawAccount::query()->where('agent_id', $agent->id)->findOrFail($data['id']);
  61. $account->fill($data);
  62. $account->agent_id = $agent->id;
  63. $account->save();
  64. return $account;
  65. });
  66. }
  67. public function deleteAccount(Request $request)
  68. {
  69. return $this->execute(function () use ($request) {
  70. $data = $request->validate(['id' => ['required', 'integer']]);
  71. DB::transaction(function () use ($data) {
  72. $account = AgentWithdrawAccount::query()->where('agent_id', $this->currentAgent()->id)
  73. ->lockForUpdate()->findOrFail($data['id']);
  74. if (AgentWithdrawal::query()->where('account_id', $account->id)->whereIn('status', ['pending', 'approved'])->exists()) {
  75. throw new \RuntimeException('该账户有处理中的提现,不能删除');
  76. }
  77. $account->delete();
  78. });
  79. return [];
  80. });
  81. }
  82. public function withdrawals(Request $request)
  83. {
  84. return $this->execute(function () use ($request) {
  85. $data = $request->validate([
  86. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  87. 'status' => ['nullable', 'in:pending,approved,rejected,paid'], 'start_date' => ['nullable', 'date_format:Y-m-d'],
  88. 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  89. ]);
  90. return $this->pageQuery(AgentWithdrawal::query()->where('agent_id', $this->currentAgent()->id), $data);
  91. });
  92. }
  93. public function requestWithdrawal(Request $request, AgentWithdrawalService $service)
  94. {
  95. return $this->execute(function () use ($request, $service) {
  96. $data = $request->validate([
  97. 'account_id' => ['required', 'integer'], 'usdt_amount' => ['required', 'numeric', 'min:0.0001', new DecimalNumber()],
  98. 'withdraw_password' => ['required', 'string'], 'remark' => ['nullable', 'string', 'max:500'],
  99. 'request_id' => ['required', 'string', 'max:64'],
  100. ]);
  101. return $service->request($this->currentAgent(), (int) $data['account_id'], (string) $data['usdt_amount'], $data['withdraw_password'], $data['request_id'], (string) ($data['remark'] ?? ''));
  102. });
  103. }
  104. /** 余额构成:总余额、保证金、盈亏、可提现。 */
  105. public function summary(AgentWalletService $wallets)
  106. {
  107. return $this->execute(fn () => $wallets->funds($this->currentAgent()->fresh()) + [
  108. 'withdraw_exchange_rate' => AgentExchangeRate::withdraw(),
  109. 'recharge_exchange_rate' => AgentExchangeRate::recharge(),
  110. ]);
  111. }
  112. public function rechargeAddress(Request $request, AgentRechargeService $service)
  113. {
  114. return $this->execute(function () use ($request, $service) {
  115. $data = $request->validate(['net' => ['nullable', 'in:trc20,erc20,TRC20,ERC20']]);
  116. return $service->address($this->currentAgent(), (string) ($data['net'] ?? 'trc20'));
  117. });
  118. }
  119. public function submitRecharge(Request $request, AgentRechargeService $service)
  120. {
  121. return $this->execute(function () use ($request, $service) {
  122. $data = $request->validate([
  123. 'net' => ['required', 'in:trc20,erc20,TRC20,ERC20'],
  124. 'usdt_amount' => ['required', 'numeric', 'min:0.01', new DecimalNumber()],
  125. 'to_address' => ['required', 'string', 'max:64'],
  126. 'image' => ['required', 'url', 'max:1000'],
  127. ]);
  128. return $service->submitProof($this->currentAgent(), $data);
  129. });
  130. }
  131. public function recharges(Request $request)
  132. {
  133. return $this->execute(function () use ($request) {
  134. $data = $request->validate([
  135. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  136. 'status' => ['nullable', 'integer', 'in:0,1,2,3'], 'start_date' => ['nullable', 'date_format:Y-m-d'],
  137. 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  138. ]);
  139. $query = AgentRecharge::query()->where('agent_id', $this->currentAgent()->id);
  140. if (isset($data['status'])) $query->where('status', (int) $data['status']);
  141. unset($data['status']);
  142. return $this->pageQuery($query, $data);
  143. });
  144. }
  145. private function pageQuery($query, array $data): array
  146. {
  147. $page = max(1, (int) ($data['page'] ?? 1));
  148. $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
  149. if (!empty($data['type'])) $query->where('type', $data['type']);
  150. if (!empty($data['status'])) $query->where('status', $data['status']);
  151. if (!empty($data['start_date'])) $query->where('created_at', '>=', $data['start_date'] . ' 00:00:00');
  152. if (!empty($data['end_date'])) $query->where('created_at', '<=', $data['end_date'] . ' 23:59:59');
  153. $total = (clone $query)->count();
  154. $list = $query->orderByDesc('id')->forPage($page, $limit)->get();
  155. return compact('total', 'page', 'limit', 'list');
  156. }
  157. }