Agent.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Http\Controllers\AgentApiController;
  4. use App\Models\Agent\Agent as AgentModel;
  5. use App\Models\Agent\AgentLoginLog;
  6. use App\Models\Agent\AgentWithdrawal;
  7. use App\Models\Agent\AgentRebateRecord;
  8. use App\Models\EgameItem;
  9. use App\Models\User;
  10. use App\Services\Agent\AgentDomainService;
  11. use App\Services\Agent\AgentReportService;
  12. use App\Services\Agent\AgentService;
  13. use App\Services\Agent\AgentWalletService;
  14. use App\Services\Agent\AgentWithdrawalService;
  15. use App\Services\Agent\AgentPlanApplicationService;
  16. use App\Services\Agent\AgentRechargeService;
  17. use App\Models\Agent\AgentSetting;
  18. use App\Models\Agent\AgentRecharge;
  19. use App\Models\Agent\AgentProfitEntry;
  20. use App\Services\Agent\AgentProfitCommissionProfileService;
  21. use Illuminate\Http\Request;
  22. use Illuminate\Validation\Rule;
  23. use Illuminate\Support\Facades\DB;
  24. use App\Rules\DecimalNumber;
  25. use App\Constants\Util;
  26. use App\Services\ThirdGameBalanceService;
  27. class Agent extends AgentApiController
  28. {
  29. public function index(Request $request, AgentDomainService $domains)
  30. {
  31. return $this->execute(function () use ($request, $domains) {
  32. $data = $request->validate([
  33. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  34. 'username' => ['nullable', 'string', 'max:64'], 'real_name' => ['nullable', 'string', 'max:128'],
  35. 'type' => ['nullable', 'integer', 'in:1,2'], 'status' => ['nullable', 'integer', 'in:0,1'],
  36. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  37. ]);
  38. $page = max(1, (int) ($data['page'] ?? 1));
  39. $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
  40. $query = AgentModel::query()->with(['token:id,agent_id,expires_at,last_used_at', 'profitCommissionProfile'])->withCount(['members']);
  41. if (!empty($data['username'])) $query->where('username', 'like', '%' . $data['username'] . '%');
  42. if (!empty($data['real_name'])) $query->where('real_name', 'like', '%' . $data['real_name'] . '%');
  43. if (!empty($data['type'])) $query->where('type', $data['type']);
  44. if (isset($data['status'])) $query->where('status', $data['status']);
  45. if (!empty($data['start_date'])) $query->where('created_at', '>=', $data['start_date'] . ' 00:00:00');
  46. if (!empty($data['end_date'])) $query->where('created_at', '<=', $data['end_date'] . ' 23:59:59');
  47. $total = (clone $query)->count();
  48. $list = $query->orderByDesc('id')->forPage($page, $limit)->get()->map(fn(AgentModel $agent) => $this->formatAgent($agent, $domains));
  49. return compact('total', 'page', 'limit', 'list');
  50. });
  51. }
  52. public function show(Request $request, AgentDomainService $domains)
  53. {
  54. return $this->execute(function () use ($request, $domains) {
  55. $data = $request->validate(['id' => ['required', 'integer', 'exists:agents,id']]);
  56. $agent = AgentModel::query()->with(['token:id,agent_id,expires_at,last_used_at', 'profitCommissionProfile'])->withCount(['members'])->findOrFail($data['id']);
  57. return $this->formatAgent($agent, $domains);
  58. });
  59. }
  60. public function store(Request $request, AgentService $service)
  61. {
  62. return $this->execute(function () use ($request, $service) {
  63. $data = $request->validate($this->rules());
  64. return DB::transaction(function () use ($data, $service) {
  65. $agent = $service->create($data, (int) request()->user->id);
  66. if (bccomp((string) ($data['initial_balance'] ?? 0), '0', 4) > 0) {
  67. app(AgentWalletService::class)->adjust(
  68. (int) $agent->id,
  69. (string) $data['initial_balance'],
  70. 'credit',
  71. '新增代理初始额度',
  72. 'agent-initial:' . $agent->id,
  73. 'admin',
  74. (int) request()->user->id
  75. );
  76. $agent = $agent->fresh(['profitCommissionProfile']);
  77. }
  78. return $agent;
  79. });
  80. });
  81. }
  82. public function update(Request $request, AgentService $service)
  83. {
  84. return $this->execute(function () use ($request, $service) {
  85. $data = $request->validate(array_merge(['id' => ['required', 'integer', 'exists:agents,id']], $this->rules(true)));
  86. return DB::transaction(function () use ($service, $data) {
  87. return $service->update(AgentModel::query()->findOrFail($data['id']), $data);
  88. });
  89. });
  90. }
  91. public function domains(Request $request, AgentDomainService $service)
  92. {
  93. return $this->execute(function () use ($request, $service) {
  94. $data = $request->validate([
  95. 'id' => ['required', 'integer', 'exists:agents,id'],
  96. 'pc' => ['nullable', 'string', 'max:255'], 'h5' => ['nullable', 'string', 'max:255'],
  97. ]);
  98. return $service->save(AgentModel::query()->findOrFail($data['id']), $data);
  99. });
  100. }
  101. public function adjust(Request $request, AgentWalletService $wallets)
  102. {
  103. return $this->execute(function () use ($request, $wallets) {
  104. $data = $request->validate([
  105. 'agent_id' => ['required', 'integer', 'exists:agents,id'], 'type' => ['required', 'in:credit,debit,margin_credit,margin_refund'],
  106. 'amount' => ['required', 'numeric', 'min:0.0001', new DecimalNumber()], 'remark' => ['required', 'string', 'max:500'],
  107. 'request_id' => ['required', 'string', 'max:64'],
  108. ]);
  109. return $wallets->adjust(
  110. (int) $data['agent_id'], (string) $data['amount'], $data['type'], $data['remark'],
  111. 'admin:' . request()->user->id . ':' . $data['request_id'], 'admin', (int) request()->user->id
  112. );
  113. });
  114. }
  115. public function quota(Request $request, ThirdGameBalanceService $thirdGame)
  116. {
  117. return $this->execute(function () use ($request, $thirdGame) {
  118. $data = $request->validate([
  119. 'agent_id' => ['required', 'integer', 'exists:agents,id'],
  120. 'refresh' => ['nullable', 'boolean'],
  121. ]);
  122. $agent = AgentModel::query()->findOrFail($data['agent_id']);
  123. $provider = $thirdGame->agentBalances((int) $agent->id, $request->boolean('refresh'));
  124. if (empty($provider['ok'])) {
  125. throw new \RuntimeException((string) ($provider['msg'] ?? '三方平台余额查询失败'));
  126. }
  127. $balances = is_array($provider['balances'] ?? null) ? $provider['balances'] : [];
  128. $platforms = EgameItem::query()->where('item_type', EgameItem::TYPE_PLATFORM)->where('game_type', 0)
  129. ->where('status', EgameItem::STATUS_ENABLED)->orderByDesc('sort')->get(['plat_type', 'name', 'logo'])
  130. ->map(function (EgameItem $platform) use ($balances) {
  131. $key = strtolower((string) $platform->plat_type);
  132. $platform->logo = Util::ensureUrl($platform->logo);
  133. $platform->setAttribute('balance', $balances[$key] ?? '0.0000000000');
  134. $platform->setAttribute('account_opened', array_key_exists($key, $balances) ? 1 : 0);
  135. return $platform;
  136. });
  137. return [
  138. 'agent_id' => (int) $agent->id,
  139. 'player_id' => (string) $provider['player_id'],
  140. 'currency' => (string) $provider['currency'],
  141. 'balance' => (string) $agent->balance,
  142. 'frozen_balance' => (string) $agent->frozen_balance,
  143. 'quota_mode' => 'provider',
  144. 'provider_wallet_supported' => true,
  145. 'platforms' => $platforms,
  146. ];
  147. });
  148. }
  149. public function bindMember(Request $request)
  150. {
  151. return $this->execute(function () use ($request) {
  152. $data = $request->validate([
  153. 'user_id' => ['required', 'integer', 'exists:users,id'],
  154. 'agent_id' => ['nullable', 'integer', 'exists:agents,id'],
  155. ]);
  156. $user = User::query()->findOrFail($data['user_id']);
  157. $user->agent_id = $data['agent_id'] ?? null;
  158. $user->save();
  159. return ['user_id' => (int) $user->id, 'agent_id' => $user->agent_id === null ? null : (int) $user->agent_id];
  160. });
  161. }
  162. public function loginLogs(Request $request)
  163. {
  164. return $this->execute(function () use ($request) {
  165. $data = $request->validate([
  166. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  167. 'agent_id' => ['nullable', 'integer'], 'username' => ['nullable', 'string', 'max:64'],
  168. 'ip' => ['nullable', 'string', 'max:64'], 'device' => ['nullable', 'in:mobile,pc'],
  169. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  170. ]);
  171. $query = AgentLoginLog::query();
  172. foreach (['agent_id', 'username', 'ip', 'device'] as $field) if (!empty($data[$field])) $query->where($field, $data[$field]);
  173. return $this->paginate($query, $data, 'login_at', true, 'login_at');
  174. });
  175. }
  176. public function withdrawals(Request $request)
  177. {
  178. return $this->execute(function () use ($request) {
  179. $data = $request->validate([
  180. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  181. 'agent_id' => ['nullable', 'integer'], 'order_no' => ['nullable', 'string', 'max:40'],
  182. 'status' => ['nullable', 'in:pending,approved,rejected,paid'],
  183. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  184. ]);
  185. $query = AgentWithdrawal::query()->with('agent:id,username,real_name');
  186. foreach (['agent_id', 'order_no', 'status'] as $field) if (!empty($data[$field])) $query->where($field, $data[$field]);
  187. return $this->paginate($query, $data);
  188. });
  189. }
  190. public function auditWithdrawal(Request $request, AgentWithdrawalService $service)
  191. {
  192. return $this->execute(function () use ($request, $service) {
  193. $data = $request->validate([
  194. 'id' => ['required', 'integer', 'exists:agent_withdrawals,id'],
  195. 'action' => ['required', 'in:approve,reject,paid'], 'remark' => ['nullable', 'string', 'max:500'],
  196. ]);
  197. return $service->audit((int) $data['id'], $data['action'], (int) request()->user->id, (string) ($data['remark'] ?? ''));
  198. });
  199. }
  200. public function reports(Request $request, AgentReportService $reports)
  201. {
  202. return $this->execute(function () use ($request, $reports) {
  203. $data = $request->validate([
  204. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:20'],
  205. 'username' => ['nullable', 'string', 'max:64'], 'start_date' => ['required', 'date_format:Y-m-d'],
  206. 'end_date' => ['required', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  207. ]);
  208. return $reports->reportRows(null, $data);
  209. });
  210. }
  211. public function commissions(Request $request)
  212. {
  213. return $this->execute(function () use ($request) {
  214. $data = $request->validate([
  215. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  216. 'agent_id' => ['nullable', 'integer'], 'order_no' => ['nullable', 'string', 'max:48'],
  217. 'username' => ['nullable', 'string', 'max:64'],
  218. 'platform' => ['nullable', 'string', 'max:32'],
  219. 'category' => ['nullable', 'string', 'max:32'],
  220. 'profit_status' => ['nullable', 'in:pending,credited'],
  221. 'settlement_date' => ['nullable', 'date_format:Y-m-d'],
  222. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  223. ]);
  224. $query = AgentRebateRecord::query()->with('agent:id,username');
  225. if (!empty($data['category'])) $query->where('platform', $data['category']);
  226. foreach (['agent_id', 'order_no', 'platform', 'profit_status'] as $field) {
  227. if (!empty($data[$field])) $query->where($field, $data[$field]);
  228. }
  229. if (!empty($data['username'])) {
  230. $query->whereHas('agent', fn($query) => $query->where('username', 'like', '%' . $data['username'] . '%'));
  231. }
  232. if (!empty($data['settlement_date'])) $query->where('settlement_date', $data['settlement_date']);
  233. if (!empty($data['start_date'])) $query->where('created_at', '>=', $data['start_date'] . ' 00:00:00');
  234. if (!empty($data['end_date'])) $query->where('created_at', '<=', $data['end_date'] . ' 23:59:59');
  235. return $this->paginate($query, $data, 'settlement_date', false);
  236. });
  237. }
  238. public function profitCommissionProfiles(Request $request, AgentProfitCommissionProfileService $service)
  239. {
  240. return $this->execute(function () use ($request, $service) {
  241. $data = $request->validate([
  242. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  243. 'name' => ['nullable', 'string', 'max:128'],
  244. ]);
  245. return $service->paginate($data);
  246. });
  247. }
  248. public function saveProfitCommissionProfile(Request $request, AgentProfitCommissionProfileService $service)
  249. {
  250. return $this->execute(function () use ($request, $service) {
  251. $data = $request->validate([
  252. 'id' => ['nullable', 'integer', 'exists:agent_profit_commission_profiles,id'],
  253. 'name' => ['required', 'string', 'max:128'],
  254. 'margin_amount' => ['required', 'numeric', 'min:0', new DecimalNumber()],
  255. 'f1_lottery_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  256. 'f1_sport_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  257. 'live_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  258. 'slot_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  259. 'sport_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  260. 'esports_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  261. 'fishing_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  262. 'chess_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  263. ]);
  264. return $service->save($data, (int) request()->user->id);
  265. });
  266. }
  267. public function deleteProfitCommissionProfile(Request $request, AgentProfitCommissionProfileService $service)
  268. {
  269. return $this->execute(function () use ($request, $service) {
  270. $data = $request->validate(['id' => ['required', 'integer', 'exists:agent_profit_commission_profiles,id']]);
  271. $service->delete((int) $data['id']);
  272. return [];
  273. });
  274. }
  275. /** 代理全局配置:停止分成阈值(方案保证金的百分比)、入股价格与可购买区间。 */
  276. public function settings()
  277. {
  278. return $this->execute(fn () => AgentSetting::current());
  279. }
  280. public function saveSettings(Request $request)
  281. {
  282. return $this->execute(function () use ($request) {
  283. $data = $request->validate([
  284. 'margin_stop_percent' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  285. 'equity_enabled' => ['required', 'boolean'],
  286. 'equity_price_per_percent' => ['required', 'numeric', 'min:0', new DecimalNumber()],
  287. 'equity_min_percent' => ['required', 'numeric', 'gt:0', 'max:100', new DecimalNumber(3, 4)],
  288. 'equity_max_percent' => ['required', 'numeric', 'gte:equity_min_percent', 'max:100', new DecimalNumber(3, 4)],
  289. ]);
  290. $setting = AgentSetting::current();
  291. $setting->fill($data + ['updated_by' => (int) request()->user->id]);
  292. $setting->equity_enabled = $request->boolean('equity_enabled') ? 1 : 0;
  293. $setting->save();
  294. return $setting->fresh();
  295. });
  296. }
  297. public function planApplications(Request $request, AgentPlanApplicationService $service)
  298. {
  299. return $this->execute(function () use ($request, $service) {
  300. $data = $request->validate([
  301. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  302. 'agent_id' => ['nullable', 'integer'], 'username' => ['nullable', 'string', 'max:64'],
  303. 'status' => ['nullable', 'in:pending,approved,rejected'],
  304. ]);
  305. return $service->paginate($data);
  306. });
  307. }
  308. public function auditPlanApplication(Request $request, AgentPlanApplicationService $service)
  309. {
  310. return $this->execute(function () use ($request, $service) {
  311. $data = $request->validate([
  312. 'id' => ['required', 'integer'], 'status' => ['required', 'in:approved,rejected'],
  313. 'reject_reason' => ['required_if:status,rejected', 'nullable', 'string', 'max:500'],
  314. ]);
  315. return $service->audit((int) $data['id'], $data['status'], (int) request()->user->id, (string) ($data['reject_reason'] ?? ''));
  316. });
  317. }
  318. public function recharges(Request $request)
  319. {
  320. return $this->execute(function () use ($request) {
  321. $data = $request->validate([
  322. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  323. 'agent_id' => ['nullable', 'integer'], 'order_no' => ['nullable', 'string', 'max:40'],
  324. 'txid' => ['nullable', 'string', 'max:100'], 'type' => ['nullable', 'integer', 'in:1,2'],
  325. 'status' => ['nullable', 'integer', 'in:0,1,2,3'],
  326. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  327. ]);
  328. $query = AgentRecharge::query()->with('agent:id,username,real_name,type');
  329. foreach (['agent_id', 'order_no', 'txid', 'type'] as $field) if (!empty($data[$field])) $query->where($field, $data[$field]);
  330. if (isset($data['status'])) $query->where('status', (int) $data['status']);
  331. return $this->paginate($query, $data);
  332. });
  333. }
  334. public function auditRecharge(Request $request, AgentRechargeService $service)
  335. {
  336. return $this->execute(function () use ($request, $service) {
  337. $data = $request->validate([
  338. 'id' => ['required', 'integer'], 'status' => ['required', 'integer', 'in:1,2'],
  339. 'remark' => ['required_if:status,2', 'nullable', 'string', 'max:500'],
  340. ]);
  341. return $service->audit((int) $data['id'], (int) $data['status'], (int) request()->user->id, (string) ($data['remark'] ?? ''));
  342. });
  343. }
  344. public function profitEntries(Request $request)
  345. {
  346. return $this->execute(function () use ($request) {
  347. $data = $request->validate([
  348. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  349. 'agent_id' => ['nullable', 'integer'], 'user_id' => ['nullable', 'integer'], 'category' => ['nullable', 'string', 'max:32'],
  350. 'status' => ['nullable', 'in:counted,skipped_inactive,skipped_no_plan,skipped_category'],
  351. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  352. ]);
  353. $query = AgentProfitEntry::query();
  354. foreach (['agent_id', 'user_id', 'category', 'status'] as $field) if (!empty($data[$field])) $query->where($field, $data[$field]);
  355. if (!empty($data['start_date'])) $query->where('settle_date', '>=', $data['start_date']);
  356. if (!empty($data['end_date'])) $query->where('settle_date', '<=', $data['end_date']);
  357. $page = max(1, (int) ($data['page'] ?? 1));
  358. $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
  359. $total = (clone $query)->count();
  360. $list = $query->orderByDesc('id')->forPage($page, $limit)->get()->each->makeVisible('realtime_balance_after');
  361. return compact('total', 'page', 'limit', 'list');
  362. });
  363. }
  364. private function rules(bool $update = false): array
  365. {
  366. return [
  367. 'language' => ['sometimes', 'required', 'in:zh,en,vi'],
  368. 'type' => [$update ? 'nullable' : 'required', 'integer', 'in:1,2'],
  369. 'username' => $update
  370. ? ['sometimes', 'string', 'min:4', 'max:64', 'alpha_dash', Rule::unique('agents', 'username')->ignore((int) request()->input('id'))]
  371. : ['required', 'string', 'min:4', 'max:64', 'alpha_dash', 'unique:agents,username'],
  372. 'password' => [$update ? 'nullable' : 'required', 'string', 'min:6', 'max:100'],
  373. 'withdraw_password' => ['nullable', 'string', 'min:6', 'max:100'], 'real_name' => ['nullable', 'string', 'max:128'],
  374. 'deposit_fee_rate' => ['nullable', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  375. 'withdraw_fee_rate' => ['nullable', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  376. 'status' => ['nullable', 'integer', 'in:0,1'],
  377. 'initial_balance' => $update ? ['prohibited'] : ['nullable', 'numeric', 'min:0', new DecimalNumber()],
  378. ];
  379. }
  380. private function paginate($query, array $data, string $orderBy = 'id', bool $filterCreatedAt = true, string $dateColumn = 'created_at'): array
  381. {
  382. $page = max(1, (int) ($data['page'] ?? 1));
  383. $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
  384. if ($filterCreatedAt && !empty($data['start_date'])) $query->where($dateColumn, '>=', $data['start_date'] . ' 00:00:00');
  385. if ($filterCreatedAt && !empty($data['end_date'])) $query->where($dateColumn, '<=', $data['end_date'] . ' 23:59:59');
  386. $total = (clone $query)->count();
  387. $list = $query->orderByDesc($orderBy)->orderByDesc('id')->forPage($page, $limit)->get();
  388. return compact('total', 'page', 'limit', 'list');
  389. }
  390. private function formatAgent(AgentModel $agent, AgentDomainService $domains): array
  391. {
  392. $token = $agent->token;
  393. $online = $token
  394. && $token->expires_at->isFuture()
  395. && $token->last_used_at
  396. && $token->last_used_at->gte(now()->subMinutes(config('agent.online_minutes', 5)));
  397. $data = $agent->toArray();
  398. unset($data['token']);
  399. $wallets = app(AgentWalletService::class);
  400. return array_merge($data, $domains->format($agent), $wallets->funds($agent), [
  401. // 总后台可以看到实时保证金;代理端不返回。
  402. 'realtime_balance' => (string) $agent->realtime_balance,
  403. 'realtime_margin' => $agent->isJoint() ? $wallets->effectiveMargin($agent) : null,
  404. 'login_status' => $online ? 1 : 0,
  405. 'login_status_text' => $online ? '在线' : '离线',
  406. 'last_active_at' => $token?->last_used_at?->toDateTimeString(),
  407. ]);
  408. }
  409. }