WalletController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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\Services\Agent\AgentWithdrawalService;
  8. use Illuminate\Http\Request;
  9. use App\Rules\DecimalNumber;
  10. class WalletController extends AgentApiController
  11. {
  12. public function logs(Request $request)
  13. {
  14. return $this->execute(function () use ($request) {
  15. $data = $request->validate([
  16. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  17. 'type' => ['nullable', 'string', 'max:32'], 'start_date' => ['nullable', 'date_format:Y-m-d'],
  18. 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  19. ]);
  20. return $this->pageQuery(AgentBalanceLog::query()->where('agent_id', $this->currentAgent()->id), $data);
  21. });
  22. }
  23. public function accounts(Request $request)
  24. {
  25. return $this->execute(function () use ($request) {
  26. $data = $request->validate(['type' => ['nullable', 'in:bank,alipay,wechat,usdt,other']]);
  27. return AgentWithdrawAccount::query()->where('agent_id', $this->currentAgent()->id)
  28. ->when(!empty($data['type']), fn($query) => $query->where('type', $data['type']))
  29. ->orderByDesc('id')->get();
  30. });
  31. }
  32. public function saveAccount(Request $request)
  33. {
  34. return $this->execute(function () use ($request) {
  35. $data = $request->validate([
  36. 'id' => ['nullable', 'integer'], 'type' => ['required', 'in:bank,alipay,wechat,usdt,other'],
  37. 'name' => ['nullable', 'string', 'max:128'], 'account' => ['required', 'string', 'max:255'],
  38. 'bank_name' => ['nullable', 'string', 'max:128'], 'network' => ['nullable', 'string', 'max:32'],
  39. 'details' => ['nullable', 'array'], 'status' => ['nullable', 'integer', 'in:0,1'],
  40. ]);
  41. $agent = $this->currentAgent();
  42. $account = empty($data['id']) ? new AgentWithdrawAccount() : AgentWithdrawAccount::query()->where('agent_id', $agent->id)->findOrFail($data['id']);
  43. $account->fill($data);
  44. $account->agent_id = $agent->id;
  45. $account->save();
  46. return $account;
  47. });
  48. }
  49. public function deleteAccount(Request $request)
  50. {
  51. return $this->execute(function () use ($request) {
  52. $data = $request->validate(['id' => ['required', 'integer']]);
  53. $account = AgentWithdrawAccount::query()->where('agent_id', $this->currentAgent()->id)->findOrFail($data['id']);
  54. if (AgentWithdrawal::query()->where('account_id', $account->id)->whereIn('status', ['pending', 'approved'])->exists()) {
  55. throw new \RuntimeException('该账户有处理中的提现,不能删除');
  56. }
  57. $account->delete();
  58. return [];
  59. });
  60. }
  61. public function withdrawals(Request $request)
  62. {
  63. return $this->execute(function () use ($request) {
  64. $data = $request->validate([
  65. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  66. 'status' => ['nullable', 'in:pending,approved,rejected,paid'], 'start_date' => ['nullable', 'date_format:Y-m-d'],
  67. 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  68. ]);
  69. return $this->pageQuery(AgentWithdrawal::query()->where('agent_id', $this->currentAgent()->id), $data);
  70. });
  71. }
  72. public function requestWithdrawal(Request $request, AgentWithdrawalService $service)
  73. {
  74. return $this->execute(function () use ($request, $service) {
  75. $data = $request->validate([
  76. 'account_id' => ['required', 'integer'], 'amount' => ['required', 'numeric', 'min:0.0001', new DecimalNumber()],
  77. 'withdraw_password' => ['required', 'string'], 'remark' => ['nullable', 'string', 'max:500'],
  78. 'request_id' => ['required', 'string', 'max:64'],
  79. ]);
  80. return $service->request($this->currentAgent(), (int) $data['account_id'], (string) $data['amount'], $data['withdraw_password'], $data['request_id'], (string) ($data['remark'] ?? ''));
  81. });
  82. }
  83. private function pageQuery($query, array $data): array
  84. {
  85. $page = max(1, (int) ($data['page'] ?? 1));
  86. $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
  87. if (!empty($data['type'])) $query->where('type', $data['type']);
  88. if (!empty($data['status'])) $query->where('status', $data['status']);
  89. if (!empty($data['start_date'])) $query->where('created_at', '>=', $data['start_date'] . ' 00:00:00');
  90. if (!empty($data['end_date'])) $query->where('created_at', '<=', $data['end_date'] . ' 23:59:59');
  91. $total = (clone $query)->count();
  92. $list = $query->orderByDesc('id')->forPage($page, $limit)->get();
  93. return compact('total', 'page', 'limit', 'list');
  94. }
  95. }