| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- <?php
- namespace app\common\library;
- use app\common\model\UserModel;
- use think\Config;
- use think\Db;
- use think\Exception;
- use think\Hook;
- use think\Request;
- class Auth
- {
- protected static $instance = null;
- protected $_error = '';
- protected $_logined = false;
- protected $_user = null;
- //Token信息中的用户ID
- protected $_tokenUserID = 0;
- //Token信息中的Token
- protected $_tokenString = '';
- protected $requestUri = '';
- //默认配置
- protected $config = [];
- protected $options = [];
- protected $allowFields = ['id', 'address', 'nickname', 'avatar', 'createtime'];
- public function __construct($options = [])
- {
- if ($config = Config::get('user')) {
- $this->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) : '';
- }
- }
|