Token.php 4.9 KB

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