2023-01-18 17:10:33 +08:00

107 lines
2.4 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace app\common\library\token;
/**
* Token基础类.
*/
abstract class Driver
{
protected $handler = null;
protected $options = [];
/**
* 存储Token.
*
* @param string $token Token
* @param int $user_id 会员ID
* @param int $expire 过期时长,0表示无限,单位秒
*
* @return bool
*/
abstract public function set($token, $user_id, $expire = 0);
/**
* 获取Token内的信息.
*
* @param string $token
*
* @return array
*/
abstract public function get($token);
/**
* 判断Token是否可用.
*
* @param string $token Token
* @param int $user_id 会员ID
*
* @return bool
*/
abstract public function check($token, $user_id);
/**
* 删除Token.
*
* @param string $token
*
* @return bool
*/
abstract public function delete($token);
/**
* 删除指定用户的所有Token.
*
* @param int $user_id
*
* @return bool
*/
abstract public function clear($user_id);
/**
* 返回句柄对象,可执行其它高级方法.
*
* @return object
*/
public function handler()
{
return $this->handler;
}
/**
* 获取加密后的Token.
*
* @param string $token Token标识
*
* @return string
*/
protected function getEncryptedToken($token)
{
$config = \think\facade\Config::get('token');
return hash_hmac($config['hashalgo'], $token, $config['key']);
}
/**
* 获取过期剩余时长
*
* @param $expiretime
*
* @return float|int|mixed
*/
protected function getExpiredIn($expiretime)
{
return $expiretime ? max(0, $expiretime - time()) : 365 * 86400;
}
}