WalletController.php 5.6 KB

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