TokenSession.php 3.5 KB

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