setError('token不能为空,请重新登录'); return false; } $data = Token::get($token); if (!$data) { $this->setError('token无效,请重新登录'); return false; } $userId = intval($data['user_id']); if ($userId > 0) { $user = \app\model\User::find($userId); if (!$user) { $this->setError('账号不存在,请重新登录'); return false; } //用户状态 1正常 2锁定 if ($user['status'] != 1) { $this->setError('账号被锁定,请联系管理员'.$user); return false; } $this->_user = $user; $this->_isLogin = true; $this->_token = $token; return true; } else { $this->setError('账号不存在,请重新登录'); return false; } } /** * 退出登录 */ public function logout() { if (!$this->_isLogin) { $this->setError('你没有登录'); return false; } //设置登录标识 $this->_isLogin = false; //删除Token Token::delete($this->_token); return true; } /** * 兼容调用user模型的属性 * * @param string $name * @return mixed */ public function __get($name) { return $this->_user ? $this->_user->$name : null; } /** * 获取登录用户信息 */ public function getUserInfo() { $data = $this->_user->toArray(); $allowFields = $this->getAllowFields(); $userInfo = array_intersect_key($data, array_flip($allowFields)); $userInfo = array_merge($userInfo, ['token' => $this->_token]); $order = Order::where('uid',$userInfo['id'])->where('paid',1)->find(); $userInfo = array_merge($userInfo, ['is_new' => empty($userInfo['phone']) && $order]); return $userInfo; } /** * 获取允许输出的字段 * @return array */ public function getAllowFields() { return $this->allowFields; } /** * 获取User模型 * @return User */ public function getUser() { return $this->_user; } /** * 判断是否登录 * @return boolean */ public function isLogin() { if ($this->_isLogin) { return true; } return false; } /** * 获取当前Token * @return string */ public function getToken() { return $this->_token; } }