User.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Services\RoomService;
  6. use App\Services\SecretService;
  7. use App\Services\TopUpService;
  8. use Illuminate\Support\Facades\App;
  9. use Illuminate\Support\Facades\Auth;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Validator;
  12. use App\Services\UserService;
  13. use Exception;
  14. use Illuminate\Http\Request;
  15. use Illuminate\Validation\ValidationException;
  16. use App\Services\AddressService;
  17. use Illuminate\Http\JsonResponse;
  18. use App\Models\User as UserModel;
  19. class User extends Controller
  20. {
  21. function banned()
  22. {
  23. try {
  24. $params = request()->validate([
  25. 'member_id' => ['required', 'string', 'min:1'],
  26. 'is_banned' => ['required', 'integer', 'in:0,1'],
  27. ]);
  28. UserModel::where('member_id', $params['member_id'])->update(['is_banned' => $params['is_banned']]);
  29. } catch (ValidationException $e) {
  30. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  31. } catch (Exception $e) {
  32. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  33. }
  34. return $this->success();
  35. }
  36. function setNote()
  37. {
  38. try {
  39. $params = request()->validate([
  40. 'member_id' => ['required', 'string', 'min:1'],
  41. 'admin_note' => ['required', 'string', 'min:1', 'max:120'],
  42. ]);
  43. $user = UserModel::where('member_id', $params['member_id'])->first();
  44. if (!$user) throw new Exception("用户不存在", HttpStatus::CUSTOM_ERROR);
  45. $user->admin_note = $params['admin_note'];
  46. $user->save();
  47. } catch (ValidationException $e) {
  48. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  49. } catch (Exception $e) {
  50. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  51. }
  52. return $this->success();
  53. }
  54. public function index(): JsonResponse
  55. {
  56. try {
  57. $search = request()->validate([
  58. 'page' => ['nullable', 'integer', 'min:1'],
  59. 'limit' => ['nullable', 'integer', 'min:1'],
  60. 'member_id' => ['nullable', 'string', 'min:1'],
  61. 'first_name' => ['nullable', 'string', 'min:1'],
  62. 'username' => ['nullable', 'string', 'min:1'],
  63. 'register_ip' => ['nullable', 'string', 'min:1'],
  64. ]);
  65. $result = UserService::paginate($search);
  66. } catch (ValidationException $e) {
  67. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  68. } catch (Exception $e) {
  69. return $this->error(intval($e->getCode()));
  70. }
  71. return $this->success($result);
  72. }
  73. public function merge(): JsonResponse
  74. {
  75. DB::beginTransaction();
  76. try {
  77. $params = request()->validate([
  78. 'member_id' => ['required', 'string', 'min:1'],
  79. 'secret_key' => ['required', 'string', 'min:1'],
  80. ]);
  81. $res = SecretService::migration($params['member_id'], $params['secret_key']);
  82. if (!$res) {
  83. throw new Exception(lang("迁移失败"), HttpStatus::CUSTOM_ERROR);
  84. }
  85. $oldUser = UserModel::where('secret_key', $params['secret_key'])->first();
  86. $newUser = UserModel::where('member_id', $params['member_id'])->first();
  87. App::setLocale($oldUser->language);
  88. $text = lang('账户转移通知') . ":\n";
  89. $text .= lang('管理员已将您的账户转移至新用户') . "\n\n";
  90. $text .= lang('新用户信息') . "\n";
  91. $text .= lang('用户ID') . ":{$newUser->getMemberId()}\n";
  92. if ($newUser->getUsername()) {
  93. $text .= lang("用户名") . ":@{$newUser->getUsername()}\n";
  94. }
  95. $text .= lang('昵称') . ":{$newUser->getFirstName()}\n";
  96. TopUpService::notifyTransferSuccess($oldUser->getMemberId(), $text);
  97. App::setLocale($newUser->language);
  98. $text = lang("账户转移通知") . ":\n";
  99. $text .= lang("管理员已将指定账户转移至您的账户") . "\n\n";
  100. $text .= lang('原账户信息') . "\n\n";
  101. $text .= lang('用户ID') . ":{$oldUser->getMemberId()}\n";
  102. if ($oldUser->getUsername()) {
  103. $text .= lang('用户名') . ":@{$oldUser->getUsername()}\n";
  104. }
  105. $text .= lang('昵称') . ":{$oldUser->getFirstName()}\n";
  106. TopUpService::notifyTransferSuccess($newUser->getMemberId(), $text);
  107. DB::commit();
  108. } catch (ValidationException $e) {
  109. DB::rollBack();
  110. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  111. } catch (Exception $e) {
  112. DB::rollBack();
  113. if ($e->getCode() == HttpStatus::CUSTOM_ERROR) {
  114. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  115. }
  116. return $this->error(intval($e->getCode()));
  117. }
  118. return $this->success(msg: '已完成迁移');
  119. }
  120. public function address()
  121. {
  122. try {
  123. request()->validate([
  124. 'member_id' => ['required', 'integer', 'min:1'],
  125. ]);
  126. $search = request()->all();
  127. $result = AddressService::findAll($search);
  128. } catch (ValidationException $e) {
  129. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  130. } catch (Exception $e) {
  131. return $this->error(intval($e->getCode()));
  132. }
  133. return $this->success($result);
  134. }
  135. }