| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- <?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\Models\Agent\AgentRecharge;
- use App\Services\Agent\AgentExchangeRate;
- use App\Services\Agent\AgentPaymentService;
- use App\Services\Agent\AgentRechargeService;
- use App\Services\Agent\AgentWalletService;
- use App\Services\Agent\AgentWithdrawalService;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- 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) {
- 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);
- });
- }
- /** 充值通道列表(总后台配置的全部通道,含 USDT、人工充值、808/NO/JD/三斤等),格式同会员 GET /api/wallet/channel。 */
- public function rechargeChannels(AgentPaymentService $service)
- {
- return $this->execute(fn () => ['list' => $service->channels()]);
- }
- /** 在线充值(三方代收)下单。 */
- public function createPayment(Request $request, AgentPaymentService $service)
- {
- return $this->execute(function () use ($request, $service) {
- $data = $request->validate([
- 'amount' => ['required', 'numeric', 'min:0.01', new DecimalNumber(14, 2)],
- 'payment_type' => ['required', 'string', 'max:50'],
- ]);
- return $service->createOnline($this->currentAgent(), (string) $data['amount'], (string) $data['payment_type']);
- });
- }
- /** 人工充值:提交金额和付款方式。 */
- public function createManualPayment(Request $request, AgentPaymentService $service)
- {
- return $this->execute(function () use ($request, $service) {
- $data = $request->validate([
- 'amount' => ['required', 'numeric', 'min:0.01', new DecimalNumber(14, 2)],
- 'payment_type' => ['required', 'integer'],
- ]);
- return $service->createManual($this->currentAgent(), (string) $data['amount'], (int) $data['payment_type']);
- });
- }
- /** 人工充值:收到收款信息后上传转账凭证。 */
- public function submitManualImage(Request $request, AgentPaymentService $service)
- {
- return $this->execute(function () use ($request, $service) {
- $data = $request->validate(['id' => ['required', 'integer'], 'image' => ['required', 'url', 'max:1000']]);
- return $service->submitImage($this->currentAgent(), (int) $data['id'], (string) $data['image']);
- });
- }
- /** 人工充值:撤回待处理 / 待提交凭证的订单。 */
- public function cancelManualPayment(Request $request, AgentPaymentService $service)
- {
- return $this->execute(function () use ($request, $service) {
- $data = $request->validate(['id' => ['required', 'integer']]);
- return $service->cancelManual($this->currentAgent(), (int) $data['id']);
- });
- }
- /** 人民币充值记录(在线 + 人工)。 */
- public function paymentOrders(Request $request, AgentPaymentService $service)
- {
- return $this->execute(function () use ($request, $service) {
- $data = $request->validate([
- 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
- 'type' => ['nullable', 'integer', 'in:1,3'], 'status' => ['nullable', 'integer', 'between:0,7'],
- 'order_no' => ['nullable', 'string', 'max:64'], 'start_date' => ['nullable', 'date_format:Y-m-d'],
- 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
- ]);
- return $service->paginate($data, $this->currentAgent());
- });
- }
- 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');
- }
- }
|