config = array_merge($this->config, $config); } $this->options = array_merge($this->config, $options); } /** * * @param array $options 参数 * @return Auth */ public static function instance($options = []) { if (is_null(self::$instance)) { self::$instance = new static($options); } return self::$instance; } /** * 获取User模型 * @return UserModel */ public function getUser() { return $this->_user; } /** * 兼容调用user模型的属性 * * @param string $name * @return mixed */ public function __get($name) { return $this->_user ? $this->_user->$name : null; } /** * 兼容调用user模型的属性 */ public function __isset($name) { return isset($this->_user) ? isset($this->_user->$name) : false; } /** * 根据Token初始化 * * @param string $token 请求头里的加密过的token * @return boolean */ public function init(string $token) { if ($this->_logined) { return true; } if ($this->_error) { return false; } // 解析Token $tokenInfo = Token::unmarshal($token); if (count($tokenInfo) == 0) { return false; } // 校验Token有效性 $check = Token::check($tokenInfo["user_id"], $tokenInfo["token"]); if (!$check) { return false; } $user_id = intval($tokenInfo["user_id"]); if ($user_id > 0) { $user = (new UserModel())->get($user_id); if (!$user) { $this->setError('Account not exist'); return false; } $this->_user = $user; $this->_logined = true; $this->_tokenUserID = $tokenInfo["user_id"]; $this->_tokenString = $tokenInfo["token"]; //初始化成功的事件 Hook::listen("user_init_successed", $this->_user); return true; } else { $this->setError('You are not logged in'); return false; } } /** * 判断是否登录 * @return boolean */ public function isLogin() { if ($this->_logined) { return true; } return false; } /** * 获取当前Token的用户ID * @return int */ public function getTokenUserID(): int { return $this->_tokenUserID; } /** * 获取会员基本信息 */ public function getUserinfo() { $data = $this->_user->toArray(); $allowFields = $this->getAllowFields(); return array_intersect_key($data, array_flip($allowFields)); } /** * 设置当前请求的URI * @param string $uri */ public function setRequestUri($uri) { $this->requestUri = $uri; } /** * 获取允许输出的字段 * @return array */ public function getAllowFields() { return $this->allowFields; } /** * 设置允许输出的字段 * @param array $fields */ public function setAllowFields($fields) { $this->allowFields = $fields; } /** * 删除一个指定会员 * @param int $user_id 会员ID * @return boolean */ public function delete($user_id) { $user = (new UserModel())->get($user_id); if (!$user) { return false; } Db::startTrans(); try { // 删除会员 (new UserModel())->destroy($user_id); // 删除会员Token Token::delete($user_id); Hook::listen("user_delete_successed", $user); Db::commit(); } catch (Exception $e) { Db::rollback(); $this->setError($e->getMessage()); return false; } return true; } /** * 获取密码加密后的字符串 * @param string $password 密码 * @param string $salt 密码盐 * @return string */ public function getEncryptPassword($password, $salt = '') { return md5(md5($password) . $salt); } /** * 检测当前控制器和方法是否匹配传递的数组 * * @param array $arr 需要验证权限的数组 * @return boolean */ public function match($arr = []) { $request = Request::instance(); $arr = is_array($arr) ? $arr : explode(',', $arr); if (!$arr) { return false; } $arr = array_map('strtolower', $arr); // 是否存在 if (in_array(strtolower($request->action()), $arr) || in_array('*', $arr)) { return true; } // 没找到匹配 return false; } /** * 设置错误信息 * * @param string $error 错误信息 * @return Auth */ public function setError($error) { $this->_error = $error; return $this; } /** * 获取错误信息 * @return string */ public function getError() { return $this->_error ? __($this->_error) : ''; } }