TokenSession.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace UCore;
  3. use App\Version;
  4. use UCore\Exception\LogicException;
  5. /**
  6. * Token的session,使用cache作为储存介质,有效期10天
  7. *
  8. */
  9. class TokenSession
  10. {
  11. static $ttl = 3600 * 240;
  12. static $session_id = '';
  13. static $prefix = 'token_';
  14. /**
  15. * 获取SessionID
  16. *
  17. * @return string
  18. */
  19. public static function getSessionId(): string
  20. {
  21. if (!self::$session_id) {
  22. self::$session_id = self::genSessionID();
  23. }
  24. if (strlen(self::$session_id) != 32) {
  25. self::$session_id = self::genSessionID();
  26. }
  27. return self::$session_id;
  28. }
  29. /**
  30. * 生成SessionID
  31. *
  32. * @return string
  33. */
  34. public static function genSessionID(): string
  35. {
  36. $id = md5(uniqid());
  37. return $id;
  38. }
  39. /**
  40. * 获取数据
  41. *
  42. * @param $key
  43. * @param $defaultValue
  44. * @return mixed
  45. */
  46. static public function get($key = null, $defaultValue = null)
  47. {
  48. $data = \Illuminate\Support\Facades\Cache::get(static::getKey(), []);
  49. if ($key === null) {
  50. return $data;
  51. }
  52. if (strpos($key, '.')) {
  53. $ks = explode('.', $key);
  54. if (count($ks) == 2) {
  55. $value = $data[$ks[0]][$ks[1]] ?? $defaultValue;
  56. }
  57. if (count($ks) == 3) {
  58. $value = $data[$ks[0]][$ks[1]][$ks[2]] ?? $defaultValue;
  59. }
  60. if (count($ks) == 4) {
  61. $value = $data[$ks[0]][$ks[1]][$ks[2]] [$ks[4]] ?? $defaultValue;
  62. }
  63. if (count($ks) > 4) {
  64. throw new LogicException("不允许的操作");
  65. }
  66. } else {
  67. $value = $data[$key] ?? $defaultValue;
  68. }
  69. return $value;
  70. }
  71. /**
  72. * remove 的别名
  73. *
  74. * @return bool
  75. */
  76. static public function reset()
  77. {
  78. return self::remove();
  79. }
  80. /**
  81. * 删除 ,remove 的别名
  82. *
  83. * @return bool
  84. */
  85. static public function delete()
  86. {
  87. return self::remove();
  88. }
  89. /**
  90. * 去除,删掉整个Session的数据
  91. *
  92. * @return bool
  93. */
  94. static protected function remove()
  95. {
  96. return \Illuminate\Support\Facades\Cache::delete(static::getKey());
  97. }
  98. static protected function set($key, $value = null)
  99. {
  100. self::getSessionId();
  101. $data = \Illuminate\Support\Facades\Cache::get(static::getKey(), []);
  102. if (strpos($key, '.')) {
  103. $ks = explode('.', $key);
  104. if (count($ks) == 2) {
  105. $data[$ks[0]][$ks[1]] = $value;
  106. }
  107. if (count($ks) == 3) {
  108. $data[$ks[0]][$ks[1]][$ks[2]] = $value;
  109. }
  110. if (count($ks) == 4) {
  111. $data[$ks[0]][$ks[1]][$ks[2]] [$ks[4]] = $value;
  112. }
  113. if (count($ks) > 4) {
  114. throw new LogicException("不允许的操作");
  115. }
  116. } else {
  117. $data[$key] = $value;
  118. }
  119. \Illuminate\Support\Facades\Cache::set(static::getKey(), $data, static::$ttl);
  120. return $data;
  121. }
  122. static protected function getKey()
  123. {
  124. return static::$prefix . static::$session_id . '_' . Version::VERSION;
  125. }
  126. static public function checktoken($token)
  127. {
  128. if (strlen($token) != 32) {
  129. return false;
  130. }
  131. return true;
  132. }
  133. }