| 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);
- });
- }
- /** 提现通道(总后台配置),格式同会员 GET /api/wallet/withdrawChannel。 */
- public function withdrawChannels(AgentWithdrawalService $service)
- {
- return $this->execute(fn () => $service->channels($this->currentAgent()->fresh()));
- }
- public function accounts(Request $request, AgentWithdrawalService $service)
- {
- return $this->execute(function () use ($request, $service) {
- $data = $request->validate(['type' => ['nullable', 'string', 'max:32']]);
- return $service->accounts($this->currentAgent(), $data['type'] ?? null);
- });
- }
- /** 新增/修改提现账户:type 为提现通道(默认 usdt),字段规则同会员绑卡。 */
- public function saveAccount(Request $request, AgentWithdrawalService $service)
- {
- return $this->execute(function () use ($request, $service) {
- $data = $request->validate([
- 'id' => ['nullable', 'integer'], 'type' => ['nullable', 'string', 'max:32'], 'name' => ['nullable', 'string', 'max:128'],
- 'network' => ['nullable', 'string', 'in:TRC20,ERC20,trc20,erc20'], 'account' => ['nullable', 'string', 'max:255'],
- 'card_no' => ['nullable', 'string', 'max:255'], 'bank_name' => ['nullable', 'string', 'max:128'],
- 'bank_branch' => ['nullable', 'string', 'max:255'], 'qr_image' => ['nullable', 'string', 'max:255'],
- 'payout_method' => ['nullable', 'string', 'in:bank,wechat,alipay'], 'real_name' => ['nullable', 'string', 'max:64'],
- 'status' => ['nullable', 'integer', 'in:0,1'],
- ]);
- return $service->saveAccount($this->currentAgent(), $data);
- });
- }
- 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', AgentWithdrawalService::OPEN_STATUSES)->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,processing,rejected,paid,failed'], 'start_date' => ['nullable', 'date_format:Y-m-d'],
- 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'], 'channel' => ['nullable', 'string', 'max:32'],
- ]);
- $query = AgentWithdrawal::query()->where('agent_id', $this->currentAgent()->id);
- if (!empty($data['channel'])) $query->where('account_type', $data['channel']);
- return $this->pageQuery($query, $data);
- });
- }
- public function requestWithdrawal(Request $request, AgentWithdrawalService $service)
- {
- return $this->execute(function () use ($request, $service) {
- $data = $request->validate([
- // USDT 账户传 usdt_amount(USDT 数量),其他通道传 amount(人民币,最多 2 位小数)
- 'account_id' => ['required', 'integer'],
- 'usdt_amount' => ['required_without:amount', 'nullable', 'numeric', 'min:0.0001', new DecimalNumber()],
- 'amount' => ['required_without:usdt_amount', 'nullable', 'numeric', 'min:0.01', new DecimalNumber(14, 2)],
- 'withdraw_password' => ['required', 'string'], 'remark' => ['nullable', 'string', 'max:500'],
- 'request_id' => ['required', 'string', 'max:64'],
- ]);
- $account = AgentWithdrawAccount::query()->where('agent_id', $this->currentAgent()->id)->findOrFail((int) $data['account_id']);
- $amount = $account->type === 'usdt' ? ($data['usdt_amount'] ?? null) : ($data['amount'] ?? null);
- if ($amount === null || $amount === '') {
- throw new \InvalidArgumentException($account->type === 'usdt' ? '请输入提现的 USDT 数量' : '请输入提现金额');
- }
- return $service->request($this->currentAgent(), (int) $data['account_id'], (string) $amount, $data['withdraw_password'], $data['request_id'], (string) ($data['remark'] ?? ''));
- });
- }
- /** 余额构成:总余额、保证金、盈亏、可提现。 */
- public function summary(AgentWalletService $wallets)
- {
- return $this->execute(fn () => $wallets->funds($agent = $this->currentAgent()->fresh()) + [
- 'recharge_locks' => $wallets->activeLocks($agent),
- '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');
- }
- }
|