'user_token', 'expire' => 2592000, 'connection' => [], ]; /** * 构造函数 * @param array $options 参数 * @access public */ public function __construct($options = []) { if (!empty($options)) { $this->options = array_merge($this->options, $options); } if ($this->options['connection']) { $this->handler = \think\Db::connect($this->options['connection'])->name($this->options['table']); } else { $this->handler = \think\Db::name($this->options['table']); } $time = time(); $tokentime = cache('tokentime'); if (!$tokentime || $tokentime < $time - 86400) { cache('tokentime', $time); $this->handler->where('expiretime', '<', $time)->where('expiretime', '>', 0)->delete(); } } /** * 存储Token * @param string $token Token * @param int $user_id 会员ID * @param int $expire 过期时长,0表示无限,单位秒 * @param string|null $platform 平台标识(header-platform),仅审计用 * @return bool */ public function set($token, $user_id, $expire = null, $platform = null) { $expiretime = !is_null($expire) && $expire !== 0 ? time() + $expire : 0; $token = $this->getEncryptedToken($token); $data = [ 'token' => $token, 'user_id' => $user_id, 'createtime' => time(), 'expiretime' => $expiretime, ]; // platform 字段为审计用,避免老数据库未添加字段时报错 if (!is_null($platform) && $platform !== '') { $data['platform'] = (string)$platform; } $this->handler->insert($data); return true; } /** * 获取新的查询构造器(避免与 $this->handler 的链式 where 状态相互污染) * @return \think\db\Query */ protected function getQuery() { if (!empty($this->options['connection'])) { return \think\Db::connect($this->options['connection'])->name($this->options['table']); } return \think\Db::name($this->options['table']); } /** * 统计用户当前有效终端数(未过期的 Token 数量) * @param int $user_id 会员ID * @return int */ public function getUserTerminalCount($user_id) { $time = time(); return (int)$this->getQuery() ->where('user_id', $user_id) ->where(function ($query) use ($time) { $query->where('expiretime', 0) ->whereOr('expiretime', '>', $time); }) ->count(); } /** * 删除该用户最早创建的一条有效 Token(按 createtime 升序) * @param int $user_id 会员ID * @return bool */ public function deleteOldestTerminal($user_id) { $time = time(); $row = $this->getQuery() ->where('user_id', $user_id) ->where(function ($query) use ($time) { $query->where('expiretime', 0) ->whereOr('expiretime', '>', $time); }) ->order('createtime', 'asc') ->find(); if (!$row) { return false; } $this->getQuery()->where('token', $row['token'])->delete(); return true; } /** * 获取Token内的信息 * @param string $token * @return array */ public function get($token) { $data = $this->handler->where('token', $this->getEncryptedToken($token))->find(); if ($data) { if (!$data['expiretime'] || $data['expiretime'] > time()) { //返回未加密的token给客户端使用 $data['token'] = $token; //返回剩余有效时间 $data['expires_in'] = $this->getExpiredIn($data['expiretime']); return $data; } else { self::delete($token); } } return []; } /** * 判断Token是否可用 * @param string $token Token * @param int $user_id 会员ID * @return boolean */ public function check($token, $user_id) { $data = $this->get($token); return $data && $data['user_id'] == $user_id ? true : false; } /** * 删除Token * @param string $token * @return boolean */ public function delete($token) { $this->handler->where('token', $this->getEncryptedToken($token))->delete(); return true; } /** * 删除指定用户的所有Token * @param int $user_id * @return boolean */ public function clear($user_id) { $this->handler->where('user_id', $user_id)->delete(); return true; } }