User.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Services\SecretService;
  6. use App\Services\TopUpService;
  7. use Illuminate\Support\Facades\App;
  8. use Illuminate\Support\Facades\DB;
  9. use App\Services\UserService;
  10. use App\Services\RegisterStatService;
  11. use Exception;
  12. use Illuminate\Validation\ValidationException;
  13. use App\Services\AddressService;
  14. use Illuminate\Http\JsonResponse;
  15. use App\Models\User as UserModel;
  16. use App\Models\UserSession;
  17. use App\Models\UserLogin;
  18. use App\Models\ThirdGameRecycle;
  19. use App\Models\Wallet;
  20. use App\Services\BalanceLogService;
  21. use App\Services\ThirdGameBalanceService;
  22. use Illuminate\Support\Facades\Cache;
  23. use Illuminate\Support\Str;
  24. class User extends Controller
  25. {
  26. //修改用户密码/资金密码
  27. function setPassword()
  28. {
  29. try {
  30. $params = request()->validate([
  31. 'member_id' => ['required', 'string', 'min:1'],
  32. 'password' => ['nullable', 'string', 'min:6', 'max:16'],
  33. 'payment_password' => ['nullable', 'string', 'min:6', 'max:16'],
  34. ]);
  35. $user = UserModel::where('member_id', $params['member_id'])->first();
  36. if (!$user) throw new Exception("用户不存在", HttpStatus::CUSTOM_ERROR);
  37. if (!empty($params['password'])) {
  38. $user->password = create_password($params['password']);
  39. $user->save();
  40. //删除缓存
  41. $token = UserSession::where('user_id', $params['member_id'])->orderByDesc('expire_time')->value('token');
  42. Cache::delete('token_user_' . $token);
  43. UserSession::where('user_id', $params['member_id'])->delete();
  44. }
  45. if (!empty($params['payment_password'])) {
  46. $user->payment_password = password_hash($params['payment_password'], PASSWORD_DEFAULT);
  47. $user->save();
  48. }
  49. } catch (ValidationException $e) {
  50. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  51. } catch (Exception $e) {
  52. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  53. }
  54. return $this->success();
  55. }
  56. function banned()
  57. {
  58. try {
  59. $params = request()->validate([
  60. 'member_id' => ['required', 'string', 'min:1'],
  61. 'is_banned' => ['required', 'integer', 'in:0,1'],
  62. ]);
  63. UserModel::where('member_id', $params['member_id'])->update(['is_banned' => $params['is_banned']]);
  64. if ($params['is_banned'] == 1) {
  65. //如果用户被禁用,删除所有会话
  66. UserSession::where('user_id', $params['member_id'])->delete();
  67. return $this->success();
  68. }
  69. } catch (ValidationException $e) {
  70. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  71. } catch (Exception $e) {
  72. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  73. }
  74. return $this->success();
  75. }
  76. function setNote()
  77. {
  78. try {
  79. $params = request()->validate([
  80. 'member_id' => ['required', 'string', 'min:1'],
  81. 'admin_note' => ['required', 'string', 'min:1', 'max:120'],
  82. ]);
  83. $user = UserModel::where('member_id', $params['member_id'])->first();
  84. if (!$user) throw new Exception("用户不存在", HttpStatus::CUSTOM_ERROR);
  85. $user->admin_note = $params['admin_note'];
  86. $user->save();
  87. } catch (ValidationException $e) {
  88. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  89. } catch (Exception $e) {
  90. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  91. }
  92. return $this->success();
  93. }
  94. public function index(): JsonResponse
  95. {
  96. try {
  97. $search = request()->validate([
  98. 'page' => ['nullable', 'integer', 'min:1'],
  99. 'limit' => ['nullable', 'integer', 'min:1'],
  100. 'member_id' => ['nullable', 'string', 'min:1'],
  101. 'account' => ['nullable', 'string', 'min:1'],
  102. 'like_first_name' => ['nullable', 'string', 'min:1'],
  103. 'username' => ['nullable', 'string', 'min:1'],
  104. 'register_ip' => ['nullable', 'string', 'min:1'],
  105. 'order' => ['nullable', 'string', 'in:asc,desc'],
  106. 'by' => ['nullable', 'string', 'in:available_balance,created_at,last_active_time,frozen_balance,yuebao,total_consume,wager_required,wager_completed,wager_remaining,third_game_total_balance'],
  107. 'sort_field' => ['nullable', 'string', 'in:available_balance,created_at,last_active_time,frozen_balance,yuebao,total_consume,wager_required,wager_completed,wager_remaining,third_game_total_balance'],
  108. 'sort_order' => ['nullable', 'string', 'in:asc,desc'],
  109. 'user_code' => ['nullable'],
  110. 'agent_user_code' => ['nullable'],
  111. 'level' => ['nullable'],
  112. 'from' => ['nullable'],
  113. 'start_time' => ['nullable', 'date', 'date_format:Y-m-d', 'required_with:end_time'],
  114. 'end_time' => ['nullable', 'date', 'date_format:Y-m-d', 'required_with:start_time'],
  115. 'recharge_channel_group_id' => ['nullable'],
  116. ]);
  117. $sortField = $search['sort_field'] ?? $search['by'] ?? 'created_at';
  118. $sortOrder = $search['sort_order'] ?? $search['order'] ?? 'desc';
  119. $result = UserService::paginate($search, $sortOrder, $sortField);
  120. } catch (ValidationException $e) {
  121. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first().'ssss');
  122. } catch (Exception $e) {
  123. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  124. }
  125. return $this->success($result);
  126. }
  127. /**
  128. * 查询指定用户在 ag、pg 等三方游戏平台的余额明细。
  129. */
  130. public function thirdGameDetail(ThirdGameBalanceService $service): JsonResponse
  131. {
  132. try {
  133. $params = request()->validate([
  134. 'member_id' => ['required', 'string', 'min:1'],
  135. ]);
  136. if (!UserModel::where('member_id', $params['member_id'])->exists()) {
  137. throw new Exception('用户不存在', HttpStatus::CUSTOM_ERROR);
  138. }
  139. $result = $service->detail((string) $params['member_id']);
  140. if (!$result['ok']) {
  141. throw new Exception($result['msg'], HttpStatus::CUSTOM_ERROR);
  142. }
  143. unset($result['ok']);
  144. return $this->success($result);
  145. } catch (ValidationException $e) {
  146. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  147. } catch (Exception $e) {
  148. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  149. }
  150. }
  151. /**
  152. * 一键回收指定用户的全部三方游戏余额并入账主钱包。
  153. */
  154. public function recycleThirdGameBalance(ThirdGameBalanceService $service): JsonResponse
  155. {
  156. $lock = null;
  157. $lockAcquired = false;
  158. try {
  159. $params = request()->validate([
  160. 'member_id' => ['required', 'string', 'min:1'],
  161. ]);
  162. $memberId = (string) $params['member_id'];
  163. if (!UserModel::where('member_id', $memberId)->exists()) {
  164. throw new Exception('用户不存在', HttpStatus::CUSTOM_ERROR);
  165. }
  166. if (!Wallet::where('member_id', $memberId)->exists()) {
  167. throw new Exception('用户钱包不存在', HttpStatus::CUSTOM_ERROR);
  168. }
  169. $lock = Cache::lock('admin_third_game_recycle:' . $memberId, 120);
  170. $lockAcquired = $lock->get();
  171. if (!$lockAcquired) {
  172. throw new Exception('该用户正在回收三方余额,请勿重复操作', HttpStatus::CUSTOM_ERROR);
  173. }
  174. $pendingCredit = ThirdGameRecycle::where('member_id', $memberId)
  175. ->where('status', ThirdGameRecycle::STATUS_PROVIDER_SUCCEEDED)
  176. ->orderBy('id')
  177. ->first();
  178. if ($pendingCredit) {
  179. try {
  180. $credited = $this->creditThirdGameRecycle($pendingCredit->id);
  181. } catch (Exception $e) {
  182. throw new Exception(
  183. '三方余额已回收但钱包补入账失败,流水号:' . $pendingCredit->operation_id,
  184. HttpStatus::CUSTOM_ERROR
  185. );
  186. }
  187. $credited['recovered_pending_operation'] = true;
  188. return $this->success($credited);
  189. }
  190. $unresolved = ThirdGameRecycle::where('member_id', $memberId)
  191. ->whereIn('status', [
  192. ThirdGameRecycle::STATUS_PENDING,
  193. ThirdGameRecycle::STATUS_UNCERTAIN,
  194. ])
  195. ->orderByDesc('id')
  196. ->first();
  197. if ($unresolved) {
  198. throw new Exception(
  199. '存在待核对的三方回收流水:' . $unresolved->operation_id . ',请先人工核对三方账单',
  200. HttpStatus::CUSTOM_ERROR
  201. );
  202. }
  203. $operation = ThirdGameRecycle::create([
  204. 'operation_id' => (string) Str::uuid(),
  205. 'member_id' => $memberId,
  206. 'player_id' => $service->playerId($memberId),
  207. 'status' => ThirdGameRecycle::STATUS_PENDING,
  208. ]);
  209. $recycled = $service->recycle($memberId);
  210. if (!$recycled['ok']) {
  211. $operation->status = !empty($recycled['uncertain'])
  212. ? ThirdGameRecycle::STATUS_UNCERTAIN
  213. : ThirdGameRecycle::STATUS_FAILED;
  214. $operation->error = $recycled['msg'];
  215. $operation->save();
  216. throw new Exception(
  217. $recycled['msg'] . ',流水号:' . $operation->operation_id,
  218. HttpStatus::CUSTOM_ERROR
  219. );
  220. }
  221. $operation->status = ThirdGameRecycle::STATUS_PROVIDER_SUCCEEDED;
  222. $operation->game_balance = $recycled['game_balance'];
  223. $operation->wallet_balance = $recycled['wallet_balance'];
  224. $operation->save();
  225. try {
  226. $credited = $this->creditThirdGameRecycle($operation->id);
  227. } catch (Exception $e) {
  228. throw new Exception(
  229. '三方余额已回收但钱包入账失败,请重试回收以补入账,流水号:' . $operation->operation_id,
  230. HttpStatus::CUSTOM_ERROR
  231. );
  232. }
  233. return $this->success($credited);
  234. } catch (ValidationException $e) {
  235. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  236. } catch (Exception $e) {
  237. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  238. } finally {
  239. if ($lockAcquired && $lock) {
  240. $lock->release();
  241. }
  242. }
  243. }
  244. /**
  245. * 将三方已回收的流水幂等入账;事务失败时保留 provider_succeeded 供下次补偿。
  246. */
  247. private function creditThirdGameRecycle(int $operationId): array
  248. {
  249. return DB::transaction(function () use ($operationId) {
  250. $operation = ThirdGameRecycle::where('id', $operationId)->lockForUpdate()->firstOrFail();
  251. if ($operation->status === ThirdGameRecycle::STATUS_COMPLETED) {
  252. $walletBalance = (float) Wallet::where('member_id', $operation->member_id)
  253. ->value('available_balance');
  254. return $this->recycleResponse($operation, $walletBalance);
  255. }
  256. if ($operation->status !== ThirdGameRecycle::STATUS_PROVIDER_SUCCEEDED) {
  257. throw new Exception('三方回收流水状态不可入账', HttpStatus::CUSTOM_ERROR);
  258. }
  259. $wallet = Wallet::where('member_id', $operation->member_id)->lockForUpdate()->firstOrFail();
  260. $before = (string) $wallet->available_balance;
  261. $walletGain = (string) $operation->wallet_balance;
  262. $after = bcadd($before, $walletGain, 10);
  263. if (bccomp($walletGain, '0', 10) > 0) {
  264. BalanceLogService::addLog(
  265. $operation->member_id,
  266. $walletGain,
  267. $before,
  268. $after,
  269. '三方游戏转出',
  270. $operation->id,
  271. '后台一键回收游戏余额'
  272. );
  273. $wallet->available_balance = $after;
  274. $wallet->save();
  275. }
  276. $operation->status = ThirdGameRecycle::STATUS_COMPLETED;
  277. $operation->credited_at = now();
  278. $operation->save();
  279. return $this->recycleResponse($operation, (float) $after);
  280. });
  281. }
  282. private function recycleResponse(ThirdGameRecycle $operation, float $walletBalance): array
  283. {
  284. return [
  285. 'operation_id' => $operation->operation_id,
  286. 'member_id' => $operation->member_id,
  287. 'recovered_game_balance' => (float) $operation->game_balance,
  288. 'recovered_balance' => (float) $operation->wallet_balance,
  289. 'wallet_balance' => $walletBalance,
  290. ];
  291. }
  292. /**
  293. * 查询三方游戏回收流水,用于核对 pending/uncertain 状态。
  294. */
  295. public function thirdGameRecycleRecords(): JsonResponse
  296. {
  297. try {
  298. $params = request()->validate([
  299. 'page' => ['nullable', 'integer', 'min:1'],
  300. 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  301. 'member_id' => ['nullable', 'string', 'min:1'],
  302. 'status' => ['nullable', 'string', 'in:pending,provider_succeeded,completed,failed,uncertain'],
  303. ]);
  304. $page = (int) ($params['page'] ?? 1);
  305. $limit = (int) ($params['limit'] ?? 15);
  306. $query = ThirdGameRecycle::query()
  307. ->when(!empty($params['member_id']), function ($query) use ($params) {
  308. $query->where('member_id', $params['member_id']);
  309. })
  310. ->when(!empty($params['status']), function ($query) use ($params) {
  311. $query->where('status', $params['status']);
  312. });
  313. $total = (clone $query)->count();
  314. $list = $query->orderByDesc('id')->forPage($page, $limit)->get();
  315. return $this->success(['total' => $total, 'data' => $list]);
  316. } catch (ValidationException $e) {
  317. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  318. } catch (Exception $e) {
  319. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  320. }
  321. }
  322. /**
  323. * 人工核对待确认流水:确认未回收,或按三方实际金额补入账。
  324. */
  325. public function resolveThirdGameRecycle(ThirdGameBalanceService $service): JsonResponse
  326. {
  327. $lock = null;
  328. $lockAcquired = false;
  329. try {
  330. $params = request()->validate([
  331. 'operation_id' => ['required', 'uuid'],
  332. 'action' => ['required', 'string', 'in:failed,credit'],
  333. 'game_balance' => ['nullable', 'required_if:action,credit', 'numeric', 'min:0'],
  334. 'remark' => ['nullable', 'string', 'max:500'],
  335. ]);
  336. $operation = ThirdGameRecycle::where('operation_id', $params['operation_id'])->first();
  337. if (!$operation) {
  338. throw new Exception('三方回收流水不存在', HttpStatus::CUSTOM_ERROR);
  339. }
  340. $lock = Cache::lock('admin_third_game_recycle:' . $operation->member_id, 120);
  341. $lockAcquired = $lock->get();
  342. if (!$lockAcquired) {
  343. throw new Exception('该用户正在处理三方回收,请稍后再试', HttpStatus::CUSTOM_ERROR);
  344. }
  345. $operation = DB::transaction(function () use ($operation, $params, $service) {
  346. $operation = ThirdGameRecycle::where('id', $operation->id)->lockForUpdate()->firstOrFail();
  347. if (!in_array($operation->status, [
  348. ThirdGameRecycle::STATUS_PENDING,
  349. ThirdGameRecycle::STATUS_UNCERTAIN,
  350. ], true)) {
  351. throw new Exception('当前流水状态无需人工处理', HttpStatus::CUSTOM_ERROR);
  352. }
  353. $remark = trim((string) ($params['remark'] ?? ''));
  354. if ($params['action'] === 'failed') {
  355. $operation->status = ThirdGameRecycle::STATUS_FAILED;
  356. $operation->resolution_remark = '后台人工核对:确认三方未回收'
  357. . ($remark !== '' ? ';' . $remark : '');
  358. } else {
  359. $gameBalance = (float) $params['game_balance'];
  360. $operation->status = ThirdGameRecycle::STATUS_PROVIDER_SUCCEEDED;
  361. $operation->game_balance = $gameBalance;
  362. $operation->wallet_balance = $service->toWalletBalance($gameBalance);
  363. $operation->resolution_remark = '后台人工核对:确认三方已回收'
  364. . ($remark !== '' ? ';' . $remark : '');
  365. }
  366. $operation->save();
  367. return $operation;
  368. });
  369. if ($params['action'] === 'failed') {
  370. return $this->success([
  371. 'operation_id' => $operation->operation_id,
  372. 'member_id' => $operation->member_id,
  373. 'status' => $operation->status,
  374. ]);
  375. }
  376. try {
  377. $credited = $this->creditThirdGameRecycle($operation->id);
  378. } catch (Exception $e) {
  379. throw new Exception(
  380. '核对金额已保存但钱包入账失败,请再次提交回收操作补入账,流水号:' . $operation->operation_id,
  381. HttpStatus::CUSTOM_ERROR
  382. );
  383. }
  384. $credited['resolved_manually'] = true;
  385. return $this->success($credited);
  386. } catch (ValidationException $e) {
  387. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  388. } catch (Exception $e) {
  389. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  390. } finally {
  391. if ($lockAcquired && $lock) {
  392. $lock->release();
  393. }
  394. }
  395. }
  396. public function merge(): JsonResponse
  397. {
  398. DB::beginTransaction();
  399. try {
  400. $params = request()->validate([
  401. 'member_id' => ['required', 'string', 'min:1'],
  402. 'secret_key' => ['required', 'string', 'min:1'],
  403. ]);
  404. $res = SecretService::migration($params['member_id'], $params['secret_key']);
  405. if (!$res) {
  406. throw new Exception(lang("迁移失败"), HttpStatus::CUSTOM_ERROR);
  407. }
  408. $oldUser = UserModel::where('secret_key', $params['secret_key'])->first();
  409. $newUser = UserModel::where('member_id', $params['member_id'])->first();
  410. App::setLocale($oldUser->language);
  411. $text = lang('账户转移通知') . ":\n";
  412. $text .= lang('管理员已将您的账户转移至新用户') . "\n\n";
  413. $text .= lang('新用户信息') . "\n";
  414. $text .= lang('用户ID') . ":{$newUser->getMemberId()}\n";
  415. if ($newUser->getUsername()) {
  416. $text .= lang("用户名") . ":@{$newUser->getUsername()}\n";
  417. }
  418. $text .= lang('昵称') . ":{$newUser->getFirstName()}\n";
  419. TopUpService::notifyTransferSuccess($oldUser->getMemberId(), $text);
  420. App::setLocale($newUser->language);
  421. $text = lang("账户转移通知") . ":\n";
  422. $text .= lang("管理员已将指定账户转移至您的账户") . "\n\n";
  423. $text .= lang('原账户信息') . "\n\n";
  424. $text .= lang('用户ID') . ":{$oldUser->getMemberId()}\n";
  425. if ($oldUser->getUsername()) {
  426. $text .= lang('用户名') . ":@{$oldUser->getUsername()}\n";
  427. }
  428. $text .= lang('昵称') . ":{$oldUser->getFirstName()}\n";
  429. TopUpService::notifyTransferSuccess($newUser->getMemberId(), $text);
  430. DB::commit();
  431. } catch (ValidationException $e) {
  432. DB::rollBack();
  433. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  434. } catch (Exception $e) {
  435. DB::rollBack();
  436. if ($e->getCode() == HttpStatus::CUSTOM_ERROR) {
  437. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  438. }
  439. return $this->error(intval($e->getCode()));
  440. }
  441. return $this->success(msg: '已完成迁移');
  442. }
  443. public function address()
  444. {
  445. try {
  446. request()->validate([
  447. 'member_id' => ['required', 'integer', 'min:1'],
  448. ]);
  449. $search = request()->all();
  450. $result = AddressService::findAll($search);
  451. } catch (ValidationException $e) {
  452. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  453. } catch (Exception $e) {
  454. return $this->error(intval($e->getCode()));
  455. }
  456. return $this->success($result);
  457. }
  458. /**
  459. * 用户登录日志(只读,不写库、不打外网)
  460. */
  461. public function loginLog()
  462. {
  463. try {
  464. $params = request()->validate([
  465. 'page' => ['nullable', 'integer', 'min:1'],
  466. 'limit' => ['nullable', 'integer', 'min:1'],
  467. 'user_id' => ['nullable'],
  468. 'member_id' => ['nullable'],
  469. 'account' => ['nullable', 'string', 'max:64'],
  470. 'first_name' => ['nullable'],
  471. 'status' => ['nullable', 'integer', 'in:0,1'],
  472. 'start_time' => ['nullable', 'date', 'date_format:Y-m-d', 'required_with:end_time'],
  473. 'end_time' => ['nullable', 'date', 'date_format:Y-m-d', 'required_with:start_time'],
  474. ]);
  475. $page = (int)request()->input('page', 1);
  476. $limit = (int)request()->input('limit', 15);
  477. $query = UserLogin::query()
  478. ->leftJoin('users', 'user_login.user_id', '=', 'users.user_id')
  479. ->select([
  480. 'user_login.id',
  481. 'user_login.user_id',
  482. 'user_login.login_account',
  483. 'user_login.login_ip',
  484. 'user_login.login_domain',
  485. 'user_login.country',
  486. 'user_login.browser',
  487. 'user_login.os',
  488. 'user_login.platform',
  489. 'user_login.status',
  490. 'user_login.logout_time',
  491. 'user_login.online_duration',
  492. 'user_login.user_agent',
  493. 'user_login.created_at',
  494. 'user_login.updated_at',
  495. 'users.member_id',
  496. 'users.first_name',
  497. 'users.account',
  498. 'users.username',
  499. ]);
  500. if (!empty($params['user_id'])) {
  501. $query->where('user_login.user_id', $params['user_id']);
  502. }
  503. if (!empty($params['member_id'])) {
  504. $query->where('user_login.user_id', $params['member_id']);
  505. }
  506. if (!empty($params['account'])) {
  507. // 登录失败时可能匹配不到会员,同时按登录时输入的账号筛选
  508. $account = '%' . trim((string)$params['account']) . '%';
  509. $query->where(function ($q) use ($account) {
  510. $q->where('users.account', 'like', $account)
  511. ->orWhere('user_login.login_account', 'like', $account);
  512. });
  513. }
  514. if (!empty($params['first_name'])) {
  515. $query->where('users.first_name', 'like', "%{$params['first_name']}%");
  516. }
  517. if (array_key_exists('status', $params) && $params['status'] !== null) {
  518. $query->where('user_login.status', (int)$params['status']);
  519. }
  520. if (!empty($params['start_time'])) {
  521. $query->where('user_login.created_at', '>=', $params['start_time'] . ' 00:00:00');
  522. }
  523. if (!empty($params['end_time'])) {
  524. $query->where('user_login.created_at', '<=', $params['end_time'] . ' 23:59:59');
  525. }
  526. $count = (clone $query)->count();
  527. $list = $query
  528. ->forPage($page, $limit)
  529. ->orderByDesc('user_login.id')
  530. ->get();
  531. } catch (Exception $e) {
  532. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  533. }
  534. return $this->success(['total' => $count, 'data' => $list]);
  535. }
  536. /**
  537. * 会员注册走势图
  538. */
  539. public function registerTrend()
  540. {
  541. try {
  542. $params = request()->validate([
  543. 'type' => ['nullable', 'string', 'in:today,all'],
  544. 'start_time' => ['nullable', 'date', 'date_format:Y-m-d'],
  545. 'end_time' => ['nullable', 'date', 'date_format:Y-m-d', 'after_or_equal:start_time'],
  546. ]);
  547. $data = RegisterStatService::trend(
  548. $params['type'] ?? 'today',
  549. $params['start_time'] ?? null,
  550. $params['end_time'] ?? null
  551. );
  552. } catch (Exception $e) {
  553. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  554. }
  555. return $this->success($data);
  556. }
  557. /**
  558. * 域名登录人数统计
  559. */
  560. public function registerDomain()
  561. {
  562. try {
  563. $params = request()->validate([
  564. 'start_time' => ['nullable', 'date', 'date_format:Y-m-d'],
  565. 'end_time' => ['nullable', 'date', 'date_format:Y-m-d', 'after_or_equal:start_time'],
  566. 'page' => ['nullable', 'integer', 'min:1'],
  567. 'limit' => ['nullable', 'integer', 'min:1', 'max:200'],
  568. ]);
  569. $page = (int)request()->input('page', 1);
  570. $limit = (int)request()->input('limit', 50);
  571. $data = RegisterStatService::domain(
  572. $params['start_time'] ?? null,
  573. $params['end_time'] ?? null,
  574. $page,
  575. $limit
  576. );
  577. } catch (Exception $e) {
  578. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  579. }
  580. return $this->success($data);
  581. }
  582. function setRechargeChannelGroup()
  583. {
  584. try {
  585. $params = request()->validate([
  586. 'member_id' => ['required', 'array'],
  587. 'recharge_channel_group_id' => ['required', 'integer', 'min:1'],
  588. ]);
  589. DB::transaction(function () use ($params) {
  590. $group = \App\Models\RechargeChannelGroup::query()->lockForUpdate()->find($params['recharge_channel_group_id']);
  591. if (!$group) throw new Exception('层级不存在或已删除');
  592. UserModel::whereIn('member_id', $params['member_id'])->update(['recharge_channel_group_id' => $group->id]);
  593. }, 3);
  594. } catch (ValidationException $e) {
  595. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  596. } catch (Exception $e) {
  597. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  598. }
  599. return $this->success();
  600. }
  601. }