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) { return AgentWithdrawAccount::query()->where('agent_id', $this->currentAgent()->id) ->where('type', 'usdt')->orderByDesc('id')->get(); }); } public function saveAccount(Request $request) { return $this->execute(function () use ($request) { // 代理提现仅支持 USDT,地址按公链校验格式。 $data = $request->validate([ 'id' => ['nullable', 'integer'], 'name' => ['nullable', 'string', 'max:128'], 'network' => ['required', 'in:TRC20,ERC20'], 'account' => ['required', 'string', 'max:255', function ($attribute, $value, $fail) use ($request) { $pattern = $request->input('network') === 'ERC20' ? '/^0x[0-9a-fA-F]{40}$/D' : '/^T[1-9A-HJ-NP-Za-km-z]{33}$/D'; if (!preg_match($pattern, (string) $value)) $fail('USDT 地址格式错误'); }], 'status' => ['nullable', 'integer', 'in:0,1'], ]); $data['type'] = 'usdt'; $data['bank_name'] = ''; $isNew = empty($data['id']); foreach (['name', 'network'] as $field) { if ($isNew || array_key_exists($field, $data)) { $data[$field] = trim((string) ($data[$field] ?? '')); } } if ($isNew || array_key_exists('status', $data)) { $data['status'] = $data['status'] ?? 1; } $agent = $this->currentAgent(); $account = $isNew ? 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']]); DB::transaction(function () use ($data) { $account = AgentWithdrawAccount::query()->where('agent_id', $this->currentAgent()->id) ->lockForUpdate()->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'], 'usdt_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['usdt_amount'], $data['withdraw_password'], $data['request_id'], (string) ($data['remark'] ?? '')); }); } /** 余额构成:总余额、保证金、盈亏、可提现。 */ public function summary(AgentWalletService $wallets) { return $this->execute(fn () => $wallets->funds($this->currentAgent()->fresh()) + [ 'withdraw_exchange_rate' => AgentExchangeRate::withdraw(), 'recharge_exchange_rate' => AgentExchangeRate::recharge(), ]); } public function rechargeAddress(Request $request, AgentRechargeService $service) { return $this->execute(function () use ($request, $service) { $data = $request->validate(['net' => ['nullable', 'in:trc20,erc20,TRC20,ERC20']]); return $service->address($this->currentAgent(), (string) ($data['net'] ?? 'trc20')); }); } public function submitRecharge(Request $request, AgentRechargeService $service) { return $this->execute(function () use ($request, $service) { $data = $request->validate([ 'net' => ['required', 'in:trc20,erc20,TRC20,ERC20'], 'usdt_amount' => ['required', 'numeric', 'min:0.01', new DecimalNumber()], 'to_address' => ['required', 'string', 'max:64'], 'image' => ['required', 'url', 'max:1000'], ]); return $service->submitProof($this->currentAgent(), $data); }); } public function recharges(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', 'integer', 'in:0,1,2,3'], 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'], ]); $query = AgentRecharge::query()->where('agent_id', $this->currentAgent()->id); if (isset($data['status'])) $query->where('status', (int) $data['status']); unset($data['status']); return $this->pageQuery($query, $data); }); } 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'); } }