1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- namespace App\Utils\Encrypt\Token;
- use Hyperf\Config\Annotation\Value;
- /**
- * Token基础类
- */
- abstract class Driver
- {
- protected $handler = null;
- protected array $options = [];
- #[Value("token.default")]
- protected array $config = [];
- /**
- * 存储Token
- * @param string $token Token
- * @param int $user_id 会员ID
- * @param int $expire 过期时长,0表示无限,单位秒
- * @return bool
- */
- abstract function set(string $token, int $user_id, int $expire = 0);
- /**
- * 获取Token内的信息
- * @param string $token
- * @return array
- */
- abstract function get(string $token);
- /**
- * 判断Token是否可用
- * @param string $token Token
- * @param int $user_id 会员ID
- * @return boolean
- */
- abstract function check(string $token, int $user_id): bool;
- /**
- * 删除Token
- * @param string $token
- * @return boolean
- */
- abstract function delete(string $token): bool;
- /**
- * 删除指定用户的所有Token
- * @param int $user_id
- * @return boolean
- */
- abstract function clear(int $user_id): bool;
- /**
- * 返回句柄对象,可执行其它高级方法
- *
- * @access public
- * @return object
- */
- public function handler()
- {
- return $this->handler;
- }
- /**
- * 获取加密后的Token
- * @param string $token Token标识
- * @return string
- */
- protected function getEncryptedToken(string $token): string
- {
- return hash_hmac($this->config['hash_algo'], $token, $this->config['key']);
- }
- /**
- * 获取过期剩余时长
- * @param $expiretime
- * @return float|int|mixed
- */
- protected function getExpiredIn($expire_time)
- {
- return $expire_time ? max(0, $expire_time - time()) : 365 * 86400;
- }
- }
|