| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace App\Http\Controllers\agent;
- use App\Http\Controllers\AgentApiController;
- use App\Models\Agent\AgentBalanceLog;
- use App\Models\Agent\AgentWithdrawAccount;
- use App\Models\Agent\AgentWithdrawal;
- use App\Services\Agent\AgentWithdrawalService;
- use Illuminate\Http\Request;
- use App\Rules\DecimalNumber;
- class WalletController extends AgentApiController
- {
- public function logs(Request $request)
- {
- return $this->execute(function () use ($request) {
- $data = $request->validate([
- 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
- 'type' => ['nullable', 'string', 'max:32'], 'start_date' => ['nullable', 'date_format:Y-m-d'],
- 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
- ]);
- return $this->pageQuery(AgentBalanceLog::query()->where('agent_id', $this->currentAgent()->id), $data);
- });
- }
- public function accounts(Request $request)
- {
- return $this->execute(function () use ($request) {
- $data = $request->validate(['type' => ['nullable', 'in:bank,alipay,wechat,usdt,other']]);
- return AgentWithdrawAccount::query()->where('agent_id', $this->currentAgent()->id)
- ->when(!empty($data['type']), fn($query) => $query->where('type', $data['type']))
- ->orderByDesc('id')->get();
- });
- }
- public function saveAccount(Request $request)
- {
- return $this->execute(function () use ($request) {
- $data = $request->validate([
- 'id' => ['nullable', 'integer'], 'type' => ['required', 'in:bank,alipay,wechat,usdt,other'],
- 'name' => ['nullable', 'string', 'max:128'], 'account' => ['required', 'string', 'max:255'],
- 'bank_name' => ['nullable', 'string', 'max:128'], 'network' => ['nullable', 'string', 'max:32'],
- 'details' => ['nullable', 'array'], 'status' => ['nullable', 'integer', 'in:0,1'],
- ]);
- $agent = $this->currentAgent();
- $account = empty($data['id']) ? new AgentWithdrawAccount() : AgentWithdrawAccount::query()->where('agent_id', $agent->id)->findOrFail($data['id']);
- $account->fill($data);
- $account->agent_id = $agent->id;
- $account->save();
- return $account;
- });
- }
- public function deleteAccount(Request $request)
- {
- return $this->execute(function () use ($request) {
- $data = $request->validate(['id' => ['required', 'integer']]);
- $account = AgentWithdrawAccount::query()->where('agent_id', $this->currentAgent()->id)->findOrFail($data['id']);
- if (AgentWithdrawal::query()->where('account_id', $account->id)->whereIn('status', ['pending', 'approved'])->exists()) {
- throw new \RuntimeException('该账户有处理中的提现,不能删除');
- }
- $account->delete();
- return [];
- });
- }
- public function withdrawals(Request $request)
- {
- return $this->execute(function () use ($request) {
- $data = $request->validate([
- 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
- 'status' => ['nullable', 'in:pending,approved,rejected,paid'], 'start_date' => ['nullable', 'date_format:Y-m-d'],
- 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
- ]);
- return $this->pageQuery(AgentWithdrawal::query()->where('agent_id', $this->currentAgent()->id), $data);
- });
- }
- public function requestWithdrawal(Request $request, AgentWithdrawalService $service)
- {
- return $this->execute(function () use ($request, $service) {
- $data = $request->validate([
- 'account_id' => ['required', 'integer'], 'amount' => ['required', 'numeric', 'min:0.0001', new DecimalNumber()],
- 'withdraw_password' => ['required', 'string'], 'remark' => ['nullable', 'string', 'max:500'],
- 'request_id' => ['required', 'string', 'max:64'],
- ]);
- return $service->request($this->currentAgent(), (int) $data['account_id'], (string) $data['amount'], $data['withdraw_password'], $data['request_id'], (string) ($data['remark'] ?? ''));
- });
- }
- private function pageQuery($query, array $data): array
- {
- $page = max(1, (int) ($data['page'] ?? 1));
- $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
- if (!empty($data['type'])) $query->where('type', $data['type']);
- if (!empty($data['status'])) $query->where('status', $data['status']);
- if (!empty($data['start_date'])) $query->where('created_at', '>=', $data['start_date'] . ' 00:00:00');
- if (!empty($data['end_date'])) $query->where('created_at', '<=', $data['end_date'] . ' 23:59:59');
- $total = (clone $query)->count();
- $list = $query->orderByDesc('id')->forPage($page, $limit)->get();
- return compact('total', 'page', 'limit', 'list');
- }
- }
|