AuthUser.php 1.3 KB

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