Agent.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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\AgentFlowCommissionProfileService;
  16. use App\Services\Agent\AgentProfitCommissionProfileService;
  17. use Illuminate\Http\Request;
  18. use Illuminate\Validation\Rule;
  19. use Illuminate\Support\Facades\DB;
  20. use App\Rules\DecimalNumber;
  21. use App\Constants\Util;
  22. use App\Services\ThirdGameBalanceService;
  23. class Agent extends AgentApiController
  24. {
  25. public function index(Request $request, AgentDomainService $domains)
  26. {
  27. return $this->execute(function () use ($request, $domains) {
  28. $data = $request->validate([
  29. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  30. 'username' => ['nullable', 'string', 'max:64'], 'real_name' => ['nullable', 'string', 'max:128'],
  31. 'parent_id' => ['nullable', 'integer'], 'status' => ['nullable', 'integer', 'in:0,1'],
  32. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  33. ]);
  34. $page = max(1, (int) ($data['page'] ?? 1));
  35. $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
  36. $query = AgentModel::query()->with(['parent:id,username', 'token:id,agent_id,expires_at,last_used_at', 'flowCommissionProfile', 'profitCommissionProfile'])->withCount(['children', 'members']);
  37. if (!empty($data['username'])) $query->where('username', 'like', '%' . $data['username'] . '%');
  38. if (!empty($data['real_name'])) $query->where('real_name', 'like', '%' . $data['real_name'] . '%');
  39. if (!empty($data['parent_id'])) $query->where('parent_id', $data['parent_id']);
  40. if (isset($data['status'])) $query->where('status', $data['status']);
  41. if (!empty($data['start_date'])) $query->where('created_at', '>=', $data['start_date'] . ' 00:00:00');
  42. if (!empty($data['end_date'])) $query->where('created_at', '<=', $data['end_date'] . ' 23:59:59');
  43. $total = (clone $query)->count();
  44. $list = $query->orderByDesc('id')->forPage($page, $limit)->get()->map(fn(AgentModel $agent) => $this->formatAgent($agent, $domains));
  45. return compact('total', 'page', 'limit', 'list');
  46. });
  47. }
  48. public function show(Request $request, AgentDomainService $domains)
  49. {
  50. return $this->execute(function () use ($request, $domains) {
  51. $data = $request->validate(['id' => ['required', 'integer', 'exists:agents,id']]);
  52. $agent = AgentModel::query()->with(['parent:id,username', 'token:id,agent_id,expires_at,last_used_at', 'flowCommissionProfile', 'profitCommissionProfile'])->withCount(['children', 'members'])->findOrFail($data['id']);
  53. return $this->formatAgent($agent, $domains);
  54. });
  55. }
  56. public function store(Request $request, AgentService $service)
  57. {
  58. return $this->execute(function () use ($request, $service) {
  59. $data = $request->validate($this->rules());
  60. return DB::transaction(function () use ($data, $service) {
  61. $agent = $service->create($data, (int) request()->user->id);
  62. if (bccomp((string) ($data['initial_balance'] ?? 0), '0', 4) > 0) {
  63. app(AgentWalletService::class)->adjust(
  64. (int) $agent->id,
  65. (string) $data['initial_balance'],
  66. 'credit',
  67. '新增代理初始额度',
  68. 'agent-initial:' . $agent->id,
  69. 'admin',
  70. (int) request()->user->id
  71. );
  72. $agent = $agent->fresh(['flowCommissionProfile', 'profitCommissionProfile']);
  73. }
  74. return $agent;
  75. });
  76. });
  77. }
  78. public function update(Request $request, AgentService $service)
  79. {
  80. return $this->execute(function () use ($request, $service) {
  81. $data = $request->validate(array_merge(['id' => ['required', 'integer', 'exists:agents,id']], $this->rules(true)));
  82. return $service->update(
  83. AgentModel::query()->findOrFail($data['id']),
  84. $data,
  85. null,
  86. (int) request()->user->id
  87. );
  88. });
  89. }
  90. public function domains(Request $request, AgentDomainService $service)
  91. {
  92. return $this->execute(function () use ($request, $service) {
  93. $data = $request->validate([
  94. 'id' => ['required', 'integer', 'exists:agents,id'],
  95. 'pc' => ['nullable', 'string', 'max:255'], 'h5' => ['nullable', 'string', 'max:255'],
  96. ]);
  97. return $service->save(AgentModel::query()->findOrFail($data['id']), $data);
  98. });
  99. }
  100. public function adjust(Request $request, AgentWalletService $wallets)
  101. {
  102. return $this->execute(function () use ($request, $wallets) {
  103. $data = $request->validate([
  104. 'agent_id' => ['required', 'integer', 'exists:agents,id'], 'type' => ['required', 'in:credit,debit'],
  105. 'amount' => ['required', 'numeric', 'min:0.0001', new DecimalNumber()], 'remark' => ['required', 'string', 'max:500'],
  106. 'request_id' => ['required', 'string', 'max:64'],
  107. ]);
  108. return $wallets->adjust(
  109. (int) $data['agent_id'], (string) $data['amount'], $data['type'], $data['remark'],
  110. 'admin:' . request()->user->id . ':' . $data['request_id'], 'admin', (int) request()->user->id
  111. );
  112. });
  113. }
  114. public function quota(Request $request, ThirdGameBalanceService $thirdGame)
  115. {
  116. return $this->execute(function () use ($request, $thirdGame) {
  117. $data = $request->validate([
  118. 'agent_id' => ['required', 'integer', 'exists:agents,id'],
  119. 'refresh' => ['nullable', 'boolean'],
  120. ]);
  121. $agent = AgentModel::query()->findOrFail($data['agent_id']);
  122. $provider = $thirdGame->agentBalances((int) $agent->id, $request->boolean('refresh'));
  123. if (empty($provider['ok'])) {
  124. throw new \RuntimeException((string) ($provider['msg'] ?? '三方平台余额查询失败'));
  125. }
  126. $balances = is_array($provider['balances'] ?? null) ? $provider['balances'] : [];
  127. $platforms = EgameItem::query()->where('item_type', EgameItem::TYPE_PLATFORM)->where('game_type', 0)
  128. ->where('status', EgameItem::STATUS_ENABLED)->orderByDesc('sort')->get(['plat_type', 'name', 'logo'])
  129. ->map(function (EgameItem $platform) use ($balances) {
  130. $key = strtolower((string) $platform->plat_type);
  131. $platform->logo = Util::ensureUrl($platform->logo);
  132. $platform->setAttribute('balance', $balances[$key] ?? '0.0000000000');
  133. $platform->setAttribute('account_opened', array_key_exists($key, $balances) ? 1 : 0);
  134. return $platform;
  135. });
  136. return [
  137. 'agent_id' => (int) $agent->id,
  138. 'player_id' => (string) $provider['player_id'],
  139. 'currency' => (string) $provider['currency'],
  140. 'balance' => (string) $agent->balance,
  141. 'frozen_balance' => (string) $agent->frozen_balance,
  142. 'quota_mode' => 'provider',
  143. 'provider_wallet_supported' => true,
  144. 'platforms' => $platforms,
  145. ];
  146. });
  147. }
  148. public function bindMember(Request $request)
  149. {
  150. return $this->execute(function () use ($request) {
  151. $data = $request->validate([
  152. 'user_id' => ['required', 'integer', 'exists:users,id'],
  153. 'agent_id' => ['nullable', 'integer', 'exists:agents,id'],
  154. ]);
  155. $user = User::query()->findOrFail($data['user_id']);
  156. $user->agent_id = $data['agent_id'] ?? null;
  157. $user->save();
  158. return ['user_id' => (int) $user->id, 'agent_id' => $user->agent_id === null ? null : (int) $user->agent_id];
  159. });
  160. }
  161. public function loginLogs(Request $request)
  162. {
  163. return $this->execute(function () use ($request) {
  164. $data = $request->validate([
  165. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  166. 'agent_id' => ['nullable', 'integer'], 'username' => ['nullable', 'string', 'max:64'],
  167. 'ip' => ['nullable', 'string', 'max:64'], 'device' => ['nullable', 'in:mobile,pc'],
  168. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  169. ]);
  170. $query = AgentLoginLog::query();
  171. foreach (['agent_id', 'username', 'ip', 'device'] as $field) if (!empty($data[$field])) $query->where($field, $data[$field]);
  172. return $this->paginate($query, $data, 'login_at');
  173. });
  174. }
  175. public function withdrawals(Request $request)
  176. {
  177. return $this->execute(function () use ($request) {
  178. $data = $request->validate([
  179. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  180. 'agent_id' => ['nullable', 'integer'], 'order_no' => ['nullable', 'string', 'max:40'],
  181. 'status' => ['nullable', 'in:pending,approved,rejected,paid'],
  182. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  183. ]);
  184. $query = AgentWithdrawal::query()->with('agent:id,username,real_name');
  185. foreach (['agent_id', 'order_no', 'status'] as $field) if (!empty($data[$field])) $query->where($field, $data[$field]);
  186. return $this->paginate($query, $data);
  187. });
  188. }
  189. public function auditWithdrawal(Request $request, AgentWithdrawalService $service)
  190. {
  191. return $this->execute(function () use ($request, $service) {
  192. $data = $request->validate([
  193. 'id' => ['required', 'integer', 'exists:agent_withdrawals,id'],
  194. 'action' => ['required', 'in:approve,reject,paid'], 'remark' => ['nullable', 'string', 'max:500'],
  195. ]);
  196. return $service->audit((int) $data['id'], $data['action'], (int) request()->user->id, (string) ($data['remark'] ?? ''));
  197. });
  198. }
  199. public function reports(Request $request, AgentReportService $reports)
  200. {
  201. return $this->execute(function () use ($request, $reports) {
  202. $data = $request->validate([
  203. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:20'],
  204. 'username' => ['nullable', 'string', 'max:64'], 'start_date' => ['required', 'date_format:Y-m-d'],
  205. 'end_date' => ['required', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  206. ]);
  207. return $reports->reportRows(null, $data);
  208. });
  209. }
  210. public function commissions(Request $request)
  211. {
  212. return $this->execute(function () use ($request) {
  213. $data = $request->validate([
  214. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  215. 'agent_id' => ['nullable', 'integer'], 'order_no' => ['nullable', 'string', 'max:48'],
  216. 'username' => ['nullable', 'string', 'max:64'],
  217. 'platform' => ['nullable', 'string', 'max:32'],
  218. 'flow_status' => ['nullable', 'in:pending,credited'],
  219. 'profit_status' => ['nullable', 'in:pending,credited'],
  220. 'settlement_date' => ['nullable', 'date_format:Y-m-d'],
  221. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  222. ]);
  223. $query = AgentRebateRecord::query()->with('agent:id,username');
  224. foreach (['agent_id', 'order_no', 'platform', 'flow_status', 'profit_status'] as $field) {
  225. if (!empty($data[$field])) $query->where($field, $data[$field]);
  226. }
  227. if (!empty($data['username'])) {
  228. $query->whereHas('agent', fn($query) => $query->where('username', 'like', '%' . $data['username'] . '%'));
  229. }
  230. if (!empty($data['settlement_date'])) $query->where('settlement_date', $data['settlement_date']);
  231. if (!empty($data['start_date'])) $query->where('created_at', '>=', $data['start_date'] . ' 00:00:00');
  232. if (!empty($data['end_date'])) $query->where('created_at', '<=', $data['end_date'] . ' 23:59:59');
  233. return $this->paginate($query, $data, 'settlement_date', false);
  234. });
  235. }
  236. public function profitCommissionProfiles(Request $request, AgentProfitCommissionProfileService $service)
  237. {
  238. return $this->execute(function () use ($request, $service) {
  239. $data = $request->validate([
  240. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  241. 'name' => ['nullable', 'string', 'max:128'],
  242. ]);
  243. return $service->paginate($data);
  244. });
  245. }
  246. public function flowCommissionProfiles(Request $request, AgentFlowCommissionProfileService $service)
  247. {
  248. return $this->execute(function () use ($request, $service) {
  249. $data = $request->validate([
  250. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  251. 'name' => ['nullable', 'string', 'max:128'],
  252. ]);
  253. return $service->paginate($data);
  254. });
  255. }
  256. public function saveFlowCommissionProfile(Request $request, AgentFlowCommissionProfileService $service)
  257. {
  258. return $this->execute(function () use ($request, $service) {
  259. $id = (int)$request->input('id', 0);
  260. $data = $request->validate([
  261. 'id' => ['nullable', 'integer', 'exists:agent_flow_commission_profiles,id'],
  262. 'name' => ['required', 'string', 'max:128', Rule::unique('agent_flow_commission_profiles', 'name')->whereNull('owner_agent_id')->ignore($id)],
  263. 'live_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  264. 'slot_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  265. 'lottery_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  266. 'sport_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  267. 'esports_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  268. 'fishing_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  269. 'chess_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  270. 'is_default' => ['required', 'boolean'],
  271. ]);
  272. return $service->save($data, (int)request()->user->id);
  273. });
  274. }
  275. public function deleteFlowCommissionProfile(Request $request, AgentFlowCommissionProfileService $service)
  276. {
  277. return $this->execute(function () use ($request, $service) {
  278. $data = $request->validate(['id' => ['required', 'integer', 'exists:agent_flow_commission_profiles,id']]);
  279. $service->delete((int)$data['id'], (int)request()->user->id);
  280. return [];
  281. });
  282. }
  283. public function saveProfitCommissionProfile(Request $request, AgentProfitCommissionProfileService $service)
  284. {
  285. return $this->execute(function () use ($request, $service) {
  286. $id = (int) $request->input('id', 0);
  287. $data = $request->validate([
  288. 'id' => ['nullable', 'integer', 'exists:agent_profit_commission_profiles,id'],
  289. 'name' => ['required', 'string', 'max:128', Rule::unique('agent_profit_commission_profiles', 'name')->whereNull('owner_agent_id')->ignore($id)],
  290. 'live_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  291. 'slot_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  292. 'lottery_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  293. 'sport_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  294. 'esports_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  295. 'fishing_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  296. 'chess_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  297. 'is_default' => ['required', 'boolean'],
  298. ]);
  299. return $service->save($data, (int) request()->user->id);
  300. });
  301. }
  302. public function deleteProfitCommissionProfile(Request $request, AgentProfitCommissionProfileService $service)
  303. {
  304. return $this->execute(function () use ($request, $service) {
  305. $data = $request->validate(['id' => ['required', 'integer', 'exists:agent_profit_commission_profiles,id']]);
  306. $service->delete((int) $data['id'], (int) request()->user->id);
  307. return [];
  308. });
  309. }
  310. private function rules(bool $update = false): array
  311. {
  312. return [
  313. 'username' => $update
  314. ? ['sometimes', 'string', 'min:4', 'max:64', 'alpha_dash', Rule::unique('agents', 'username')->ignore((int) request()->input('id'))]
  315. : ['required', 'string', 'min:4', 'max:64', 'alpha_dash', 'unique:agents,username'],
  316. 'password' => [$update ? 'nullable' : 'required', 'string', 'min:6', 'max:100'],
  317. 'withdraw_password' => ['nullable', 'string', 'min:6', 'max:100'], 'real_name' => ['nullable', 'string', 'max:128'],
  318. 'parent_id' => $update ? ['nullable', 'integer', 'exists:agents,id'] : ['prohibited'],
  319. 'deposit_fee_rate' => ['nullable', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  320. 'withdraw_fee_rate' => ['nullable', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  321. 'flow_commission_rate' => ['prohibited'],
  322. 'profit_commission_rate' => ['prohibited'],
  323. 'flow_commission_profile_id' => $update
  324. ? ['nullable', 'integer', Rule::exists('agent_flow_commission_profiles', 'id')->whereNull('owner_agent_id')]
  325. : ['required', 'integer', Rule::exists('agent_flow_commission_profiles', 'id')->whereNull('owner_agent_id')],
  326. 'profit_commission_profile_id' => ['nullable', 'integer', Rule::exists('agent_profit_commission_profiles', 'id')->whereNull('owner_agent_id')],
  327. 'status' => ['nullable', 'integer', 'in:0,1'],
  328. 'initial_balance' => $update ? ['prohibited'] : ['nullable', 'numeric', 'min:0', new DecimalNumber()],
  329. ];
  330. }
  331. private function paginate($query, array $data, string $orderBy = 'id', bool $filterCreatedAt = true): array
  332. {
  333. $page = max(1, (int) ($data['page'] ?? 1));
  334. $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
  335. if ($filterCreatedAt && !empty($data['start_date'])) $query->where('created_at', '>=', $data['start_date'] . ' 00:00:00');
  336. if ($filterCreatedAt && !empty($data['end_date'])) $query->where('created_at', '<=', $data['end_date'] . ' 23:59:59');
  337. $total = (clone $query)->count();
  338. $list = $query->orderByDesc($orderBy)->orderByDesc('id')->forPage($page, $limit)->get();
  339. return compact('total', 'page', 'limit', 'list');
  340. }
  341. private function formatAgent(AgentModel $agent, AgentDomainService $domains): array
  342. {
  343. $token = $agent->token;
  344. $online = $token
  345. && $token->expires_at->isFuture()
  346. && $token->last_used_at
  347. && $token->last_used_at->gte(now()->subMinutes(config('agent.online_minutes', 5)));
  348. $data = $agent->toArray();
  349. unset($data['token']);
  350. return array_merge($data, $domains->format($agent), [
  351. 'login_status' => $online ? 1 : 0,
  352. 'login_status_text' => $online ? '在线' : '离线',
  353. 'last_active_at' => $token?->last_used_at?->toDateTimeString(),
  354. ]);
  355. }
  356. }