User.php 27 KB

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