Token.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace app\common\library;
  3. use think\helper\Arr;
  4. use think\helper\Str;
  5. use think\facade\Config;
  6. use InvalidArgumentException;
  7. /**
  8. * Token 管理类
  9. */
  10. class Token
  11. {
  12. /**
  13. * Token 实例
  14. * @var array
  15. * @uses Token 数组项
  16. */
  17. public array $instance = [];
  18. /**
  19. * token驱动类句柄
  20. * @var ?object
  21. */
  22. public ?object $handler = null;
  23. /**
  24. * 驱动类命名空间
  25. * @var string
  26. */
  27. protected string $namespace = '\\app\\common\\library\\token\\driver\\';
  28. /**
  29. * 获取驱动句柄
  30. * @param string|null $name
  31. * @return object
  32. */
  33. public function getDriver(string $name = null): object
  34. {
  35. if (!is_null($this->handler)) {
  36. return $this->handler;
  37. }
  38. $name = $name ?: $this->getDefaultDriver();
  39. if (is_null($name)) {
  40. throw new InvalidArgumentException(sprintf(
  41. 'Unable to resolve NULL driver for [%s].',
  42. static::class
  43. ));
  44. }
  45. return $this->createDriver($name);
  46. }
  47. /**
  48. * 创建驱动句柄
  49. * @param string $name
  50. * @return object
  51. */
  52. protected function createDriver(string $name): object
  53. {
  54. $type = $this->resolveType($name);
  55. $method = 'create' . Str::studly($type) . 'Driver';
  56. $params = $this->resolveParams($name);
  57. if (method_exists($this, $method)) {
  58. return $this->$method(...$params);
  59. }
  60. $class = $this->resolveClass($type);
  61. if (isset($this->instance[$type])) {
  62. return $this->instance[$type];
  63. }
  64. return new $class(...$params);
  65. }
  66. /**
  67. * 默认驱动
  68. * @return string
  69. */
  70. protected function getDefaultDriver(): string
  71. {
  72. return $this->getConfig('default');
  73. }
  74. /**
  75. * 获取驱动配置
  76. * @param string|null $name 要获取的配置项,不传递获取完整token配置
  77. * @param null $default
  78. * @return array|string
  79. */
  80. protected function getConfig(string $name = null, $default = null): array|string
  81. {
  82. if (!is_null($name)) {
  83. return Config::get('buildadmin.token.' . $name, $default);
  84. }
  85. return Config::get('buildadmin.token');
  86. }
  87. /**
  88. * 获取驱动配置参数
  89. * @param $name
  90. * @return array
  91. */
  92. protected function resolveParams($name): array
  93. {
  94. $config = $this->getStoreConfig($name);
  95. return [$config];
  96. }
  97. /**
  98. * 获取驱动类
  99. * @param string $type
  100. * @return string
  101. */
  102. protected function resolveClass(string $type): string
  103. {
  104. if ($this->namespace || str_contains($type, '\\')) {
  105. $class = str_contains($type, '\\') ? $type : $this->namespace . Str::studly($type);
  106. if (class_exists($class)) {
  107. return $class;
  108. }
  109. }
  110. throw new InvalidArgumentException("Driver [$type] not supported.");
  111. }
  112. /**
  113. * 获取驱动配置
  114. * @param string $store
  115. * @param string|null $name
  116. * @param null $default
  117. * @return array|string
  118. */
  119. protected function getStoreConfig(string $store, string $name = null, $default = null): array|string
  120. {
  121. if ($config = $this->getConfig("stores.$store")) {
  122. return Arr::get($config, $name, $default);
  123. }
  124. throw new InvalidArgumentException("Store [$store] not found.");
  125. }
  126. /**
  127. * 获取驱动类型
  128. * @param string $name
  129. * @return string
  130. */
  131. protected function resolveType(string $name): string
  132. {
  133. return $this->getStoreConfig($name, 'type', 'Mysql');
  134. }
  135. /**
  136. * 设置token
  137. * @param string $token
  138. * @param string $type
  139. * @param int $user_id
  140. * @param int|null $expire
  141. * @return bool
  142. */
  143. public function set(string $token, string $type, int $user_id, int $expire = null): bool
  144. {
  145. return $this->getDriver()->set($token, $type, $user_id, $expire);
  146. }
  147. /**
  148. * 获取token
  149. * @param string $token
  150. * @param bool $expirationException
  151. * @return array
  152. */
  153. public function get(string $token, bool $expirationException = true): array
  154. {
  155. return $this->getDriver()->get($token, $expirationException);
  156. }
  157. /**
  158. * 检查token
  159. * @param string $token
  160. * @param string $type
  161. * @param int $user_id
  162. * @param bool $expirationException
  163. * @return bool
  164. */
  165. public function check(string $token, string $type, int $user_id, bool $expirationException = true): bool
  166. {
  167. return $this->getDriver()->check($token, $type, $user_id, $expirationException);
  168. }
  169. /**
  170. * 删除token
  171. * @param string $token
  172. * @return bool
  173. */
  174. public function delete(string $token): bool
  175. {
  176. return $this->getDriver()->delete($token);
  177. }
  178. /**
  179. * 清理指定用户token
  180. * @param string $type
  181. * @param int $user_id
  182. * @return bool
  183. */
  184. public function clear(string $type, int $user_id): bool
  185. {
  186. return $this->getDriver()->clear($type, $user_id);
  187. }
  188. }