| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- namespace App\Module\OpenAPI\Handlers\User;
- use App\Module\OpenAPI\Handlers\BaseHandler;
- use App\Module\OpenAPI\Services\ScopeService;
- use App\Module\OpenAPI\Enums\SCOPE_TYPE;
- use Illuminate\Http\JsonResponse;
- /**
- * 用户列表Handler
- *
- * 处理获取用户列表的业务逻辑
- */
- class UserListHandler extends BaseHandler
- {
- public function __construct(ScopeService $scopeService)
- {
- parent::__construct($scopeService);
- }
- /**
- * 获取所需的权限范围
- *
- * @return SCOPE_TYPE[]
- */
- public function getRequiredScopes(): array
- {
- return [SCOPE_TYPE::USER_READ];
- }
- /**
- * 处理获取用户列表请求
- *
- * @param array $data 请求数据
- * @param array $context 上下文信息
- * @return JsonResponse
- */
- protected function process(array $data, array $context = []): JsonResponse
- {
- // 验证请求参数
- $validationErrors = $this->validateData($data, [
- 'page' => 'integer|min:1',
- 'per_page' => 'integer|min:1|max:100',
- 'search' => 'string|max:100',
- 'status' => 'string|in:active,inactive,banned',
- ]);
- if ($validationErrors) {
- return $this->errorResponse('参数验证失败', $validationErrors, 422);
- }
- // 获取分页参数
- $page = $data['page'] ?? 1;
- $perPage = $data['per_page'] ?? 20;
- $search = $data['search'] ?? '';
- $status = $data['status'] ?? '';
- // 调用User模块服务获取用户列表
- $userList = $this->getUserList($page, $perPage, $search, $status);
- // 记录操作日志
- $this->logAction('user.list.get', [
- 'page' => $page,
- 'per_page' => $perPage,
- 'search' => $search,
- 'status' => $status,
- ], $context);
- return $this->successResponse('获取用户列表成功', $userList);
- }
- /**
- * 获取用户列表
- *
- * 这里应该调用User模块的实际服务
- *
- * @param int $page
- * @param int $perPage
- * @param string $search
- * @param string $status
- * @return array
- */
- protected function getUserList(int $page, int $perPage, string $search, string $status): array
- {
- // TODO: 这里应该调用User模块的服务
- // 例如: return app(UserService::class)->getUserList($page, $perPage, $search, $status);
-
- // 暂时返回示例数据,等待与User模块集成
- $users = [];
- $total = 1000; // 假设总用户数
-
- // 生成示例用户数据
- for ($i = 1; $i <= $perPage; $i++) {
- $userId = ($page - 1) * $perPage + $i;
- if ($userId > $total) break;
-
- $users[] = [
- 'id' => $userId,
- 'username' => "user{$userId}",
- 'nickname' => "用户{$userId}",
- 'email' => "user{$userId}@example.com",
- 'avatar' => "https://example.com/avatar/{$userId}.jpg",
- 'level' => rand(1, 100),
- 'status' => $status ?: ['active', 'inactive'][rand(0, 1)],
- 'created_at' => now()->subDays(rand(1, 365))->toISOString(),
- 'last_login_at' => now()->subHours(rand(1, 24))->toISOString(),
- ];
- }
- return [
- 'users' => $users,
- 'pagination' => [
- 'current_page' => $page,
- 'per_page' => $perPage,
- 'total' => $total,
- 'last_page' => ceil($total / $perPage),
- 'from' => ($page - 1) * $perPage + 1,
- 'to' => min($page * $perPage, $total),
- ],
- 'filters' => [
- 'search' => $search,
- 'status' => $status,
- ],
- ];
- }
- }
|