AuthUser.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Utils\Control;
  4. use Hyperf\Coroutine\Coroutine;
  5. /**
  6. * 用户登录信息验证
  7. */
  8. class AuthUser{
  9. // 用户登录信息
  10. protected array $USER = [];//用户信息
  11. protected static array $instance = [];
  12. public static function getInstance(): AuthUser
  13. {
  14. //协程id
  15. $coroutine_id = Coroutine::id();
  16. if (empty(self::$instance[$coroutine_id])) {
  17. self::$instance[$coroutine_id] = new self();
  18. Coroutine::defer(function () use ($coroutine_id) {
  19. unset(self::$instance[$coroutine_id]);
  20. });
  21. }
  22. return self::$instance[$coroutine_id];
  23. }
  24. /**
  25. * 获取
  26. * @param array $user
  27. * @return void
  28. */
  29. public function set(array $user)
  30. {
  31. $this->USER = $user;
  32. }
  33. /**
  34. * 获取用户登录信息
  35. * @param string $key 用户字段,填写则获取固定字段值,不填写则返回全部
  36. * @return array|mixed|string
  37. */
  38. public function get(string $key = '')
  39. {
  40. if (!empty($key)){
  41. $field = $this->USER[$key] ?? '';
  42. }
  43. return $field ?? $this->USER;
  44. }
  45. /**
  46. * 校验
  47. * @return bool
  48. */
  49. public function check(): bool
  50. {
  51. if (empty($this->USER)) return false;
  52. return true;
  53. }
  54. }