UserInfoHandler.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace App\Module\OpenAPI\Handlers\User;
  3. use App\Module\OpenAPI\Handlers\BaseHandler;
  4. use App\Module\OpenAPI\Services\ScopeService;
  5. use App\Module\OpenAPI\Enums\SCOPE_TYPE;
  6. use Illuminate\Http\JsonResponse;
  7. /**
  8. * 用户信息Handler
  9. *
  10. * 处理获取用户信息的业务逻辑
  11. */
  12. class UserInfoHandler extends BaseHandler
  13. {
  14. public function __construct(ScopeService $scopeService)
  15. {
  16. parent::__construct($scopeService);
  17. }
  18. /**
  19. * 获取所需的权限范围
  20. *
  21. * @return SCOPE_TYPE[]
  22. */
  23. public function getRequiredScopes(): array
  24. {
  25. return [SCOPE_TYPE::USER_READ];
  26. }
  27. /**
  28. * 处理获取用户信息请求
  29. *
  30. * @param array $data 请求数据
  31. * @param array $context 上下文信息
  32. * @return JsonResponse
  33. */
  34. protected function process(array $data, array $context = []): JsonResponse
  35. {
  36. // 获取用户ID
  37. $userId = $data['user_id'] ?? $this->getUserId($context);
  38. if (!$userId) {
  39. return $this->errorResponse('用户ID不能为空', null, 400);
  40. }
  41. // 调用User模块服务获取用户信息
  42. $userInfo = $this->getUserInfo($userId);
  43. if (!$userInfo) {
  44. return $this->errorResponse('用户不存在', null, 404);
  45. }
  46. // 记录操作日志
  47. $this->logAction('user.info.get', ['user_id' => $userId], $context);
  48. return $this->successResponse('获取用户信息成功', $userInfo);
  49. }
  50. /**
  51. * 获取用户信息
  52. *
  53. * 这里应该调用User模块的实际服务
  54. *
  55. * @param int $userId
  56. * @return array|null
  57. */
  58. protected function getUserInfo(int $userId): ?array
  59. {
  60. // TODO: 这里应该调用User模块的服务
  61. // 例如: return app(UserService::class)->getUserById($userId);
  62. // 暂时返回示例数据,等待与User模块集成
  63. if ($userId > 0) {
  64. return [
  65. 'id' => $userId,
  66. 'username' => "user{$userId}",
  67. 'nickname' => "用户{$userId}",
  68. 'email' => "user{$userId}@example.com",
  69. 'avatar' => "https://example.com/avatar/{$userId}.jpg",
  70. 'level' => rand(1, 100),
  71. 'exp' => rand(0, 10000),
  72. 'gold' => rand(1000, 50000),
  73. 'diamond' => rand(10, 1000),
  74. 'status' => 'active',
  75. 'created_at' => now()->subDays(rand(1, 365))->toISOString(),
  76. 'last_login_at' => now()->subHours(rand(1, 24))->toISOString(),
  77. ];
  78. }
  79. return null;
  80. }
  81. }