get('page', 1)); $pageSize = min((int)($request->get('pageSize', 10)), 100); $result = $this->userService->getUsersPaginated($page, $pageSize); return Response::paginated( $result['users'], $result['total'], $result['page'], $result['pageSize'] ); } #[GetRoute('/{id}')] public function show(int $id): array { $user = $this->userService->getUser($id); if (!$user) { return Response::notFound('User not found'); } return Response::success($user->toArray()); } #[PostRoute('')] public function store(Request $request): array { $data = [ 'username' => $request->post('username'), 'email' => $request->post('email'), 'password' => $request->post('password'), 'status' => (int)($request->post('status', 1)) ]; try { $user = $this->userService->createUser($data); return Response::success($user->toArray(), 'User created successfully'); } catch (\Exception $e) { return Response::error($e->getCode(), $e->getMessage()); } } #[PutRoute('/{id}')] public function update(int $id, Request $request): array { $data = $request->all(); unset($data['id']); // 防止修改ID try { $success = $this->userService->updateUser($id, $data); if (!$success) { return Response::notFound('User not found'); } $user = $this->userService->getUser($id); return Response::success($user->toArray(), 'User updated successfully'); } catch (\Exception $e) { return Response::error($e->getCode(), $e->getMessage()); } } #[DeleteRoute('/{id}')] public function destroy(int $id): array { try { $success = $this->userService->deleteUser($id); if (!$success) { return Response::notFound('User not found'); } return Response::success(null, 'User deleted successfully'); } catch (\Exception $e) { return Response::error($e->getCode(), $e->getMessage()); } } #[GetRoute('/search')] public function search(Request $request): array { $keyword = $request->get('keyword', ''); $page = (int)($request->get('page', 1)); $pageSize = min((int)($request->get('pageSize', 10)), 100); if (empty($keyword)) { return Response::error(400, 'Keyword is required'); } $result = $this->userService->searchUsers($keyword, $page, $pageSize); return Response::paginated( $result['users'], $result['total'], $result['page'], $result['pageSize'] ); } #[GetRoute('/active')] public function active(): array { $users = $this->userService->getActiveUsers(); return Response::success($users); } #[GetRoute('/stats')] public function stats(): array { $stats = [ 'total_users' => $this->userService->getUsersCount(), 'active_users' => $this->userService->getActiveUsersCount(), 'inactive_users' => $this->userService->getUsersCount() - $this->userService->getActiveUsersCount() ]; return Response::success($stats); } #[PutRoute('/{id}/status')] public function toggleStatus(int $id): array { try { $success = $this->userService->toggleUserStatus($id); if (!$success) { return Response::notFound('User not found'); } return Response::success(null, 'User status updated successfully'); } catch (\Exception $e) { return Response::error($e->getCode(), $e->getMessage()); } } #[PutRoute('/{id}/password')] public function changePassword(int $id, Request $request): array { $data = [ 'old_password' => $request->post('old_password'), 'new_password' => $request->post('new_password') ]; $validator = Validator::make($data, [ 'old_password' => 'required', 'new_password' => 'required|min:6' ]); if (!$validator->validate()) { return Response::validationError('Validation failed', $validator->errors()); } try { $success = $this->userService->changePassword($id, $data['old_password'], $data['new_password']); if (!$success) { return Response::notFound('User not found'); } return Response::success(null, 'Password changed successfully'); } catch (\Exception $e) { return Response::error($e->getCode(), $e->getMessage()); } } #[GetRoute('/{id}/exists')] public function exists(int $id): array { $user = $this->userService->getUser($id); return Response::success(['exists' => $user !== null]); } #[PostRoute('/check-email')] public function checkEmail(Request $request): array { $email = $request->post('email'); if (empty($email)) { return Response::error(400, 'Email is required'); } $user = $this->userService->getUserByEmail($email); return Response::success(['exists' => $user !== null]); } #[PostRoute('/check-username')] public function checkUsername(Request $request): array { $username = $request->post('username'); if (empty($username)) { return Response::error(400, 'Username is required'); } $user = $this->userService->getUserByUsername($username); return Response::success(['exists' => $user !== null]); } }