User.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. use App\Models\UserSession;
  20. class User extends Controller
  21. {
  22. function banned()
  23. {
  24. try {
  25. $params = request()->validate([
  26. 'member_id' => ['required', 'string', 'min:1'],
  27. 'is_banned' => ['required', 'integer', 'in:0,1'],
  28. ]);
  29. UserModel::where('member_id', $params['member_id'])->update(['is_banned' => $params['is_banned']]);
  30. if ($params['is_banned'] == 1) {
  31. //如果用户被禁用,删除所有会话
  32. UserSession::where('user_id', $params['member_id'])->delete();
  33. return $this->success();
  34. }
  35. } catch (ValidationException $e) {
  36. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  37. } catch (Exception $e) {
  38. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  39. }
  40. return $this->success();
  41. }
  42. function setNote()
  43. {
  44. try {
  45. $params = request()->validate([
  46. 'member_id' => ['required', 'string', 'min:1'],
  47. 'admin_note' => ['required', 'string', 'min:1', 'max:120'],
  48. ]);
  49. $user = UserModel::where('member_id', $params['member_id'])->first();
  50. if (!$user) throw new Exception("用户不存在", HttpStatus::CUSTOM_ERROR);
  51. $user->admin_note = $params['admin_note'];
  52. $user->save();
  53. } catch (ValidationException $e) {
  54. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  55. } catch (Exception $e) {
  56. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  57. }
  58. return $this->success();
  59. }
  60. public function index(): JsonResponse
  61. {
  62. try {
  63. $search = request()->validate([
  64. 'page' => ['nullable', 'integer', 'min:1'],
  65. 'limit' => ['nullable', 'integer', 'min:1'],
  66. 'member_id' => ['nullable', 'string', 'min:1'],
  67. 'like_first_name' => ['nullable', 'string', 'min:1'],
  68. 'username' => ['nullable', 'string', 'min:1'],
  69. 'register_ip' => ['nullable', 'string', 'min:1'],
  70. 'order' => ["nullable", 'string', "in:asc,desc"],
  71. 'by' => ['nullable', 'string', "in:available_balance,created_at,last_active_time"],
  72. 'user_code' => ['nullable'],
  73. 'agent_member_id' => ['nullable'],
  74. 'level' => ['nullable'],
  75. ]);
  76. $order = request()->input('order', 'desc');
  77. $by = request()->input('by', 'available_balance');
  78. $result = UserService::paginate($search,$order,$by);
  79. } catch (ValidationException $e) {
  80. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first().'ssss');
  81. } catch (Exception $e) {
  82. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  83. }
  84. return $this->success($result);
  85. }
  86. public function merge(): JsonResponse
  87. {
  88. DB::beginTransaction();
  89. try {
  90. $params = request()->validate([
  91. 'member_id' => ['required', 'string', 'min:1'],
  92. 'secret_key' => ['required', 'string', 'min:1'],
  93. ]);
  94. $res = SecretService::migration($params['member_id'], $params['secret_key']);
  95. if (!$res) {
  96. throw new Exception(lang("迁移失败"), HttpStatus::CUSTOM_ERROR);
  97. }
  98. $oldUser = UserModel::where('secret_key', $params['secret_key'])->first();
  99. $newUser = UserModel::where('member_id', $params['member_id'])->first();
  100. App::setLocale($oldUser->language);
  101. $text = lang('账户转移通知') . ":\n";
  102. $text .= lang('管理员已将您的账户转移至新用户') . "\n\n";
  103. $text .= lang('新用户信息') . "\n";
  104. $text .= lang('用户ID') . ":{$newUser->getMemberId()}\n";
  105. if ($newUser->getUsername()) {
  106. $text .= lang("用户名") . ":@{$newUser->getUsername()}\n";
  107. }
  108. $text .= lang('昵称') . ":{$newUser->getFirstName()}\n";
  109. TopUpService::notifyTransferSuccess($oldUser->getMemberId(), $text);
  110. App::setLocale($newUser->language);
  111. $text = lang("账户转移通知") . ":\n";
  112. $text .= lang("管理员已将指定账户转移至您的账户") . "\n\n";
  113. $text .= lang('原账户信息') . "\n\n";
  114. $text .= lang('用户ID') . ":{$oldUser->getMemberId()}\n";
  115. if ($oldUser->getUsername()) {
  116. $text .= lang('用户名') . ":@{$oldUser->getUsername()}\n";
  117. }
  118. $text .= lang('昵称') . ":{$oldUser->getFirstName()}\n";
  119. TopUpService::notifyTransferSuccess($newUser->getMemberId(), $text);
  120. DB::commit();
  121. } catch (ValidationException $e) {
  122. DB::rollBack();
  123. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  124. } catch (Exception $e) {
  125. DB::rollBack();
  126. if ($e->getCode() == HttpStatus::CUSTOM_ERROR) {
  127. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  128. }
  129. return $this->error(intval($e->getCode()));
  130. }
  131. return $this->success(msg: '已完成迁移');
  132. }
  133. public function address()
  134. {
  135. try {
  136. request()->validate([
  137. 'member_id' => ['required', 'integer', 'min:1'],
  138. ]);
  139. $search = request()->all();
  140. $result = AddressService::findAll($search);
  141. } catch (ValidationException $e) {
  142. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  143. } catch (Exception $e) {
  144. return $this->error(intval($e->getCode()));
  145. }
  146. return $this->success($result);
  147. }
  148. }