getUser(); if (!$user) { return false; } return $user['uid']; } /** * * 获取用户登陆类型 * 授权、未授权登陆 * @return boolean | int Passport::LOGIN_TYPE_NOT_AUTH Passport::LOGIN_TYPE_HAS_AUTH */ public function getLoginType() { $user = $this->getUser(); if (!$user) { return false; } return $user['loginType']; } /** * * 获取登陆用户openid * @return boolean | string */ public function getOpenid() { $user = $this->getUser(); if (!$user) { return false; } return $user['openid']; } /** * * 获取用户微信昵称 * @return boolean | string */ public function getNickname() { $user = $this->getUser(); if (!$user) { return false; } return $user['nickname']; } /** * * 获取用户微信头像 * @return boolean | string */ public function getHeadimgurl() { $user = $this->getUser(); if (!$user) { return false; } return $user['headimgurl']; } /** * * 从Cookie中获取用户的登陆信息 * @return boolean | array */ public function getUser() { static $user = null; if ($user) { return $user; } $objPassport = new Passport(); $tmpResult = $objPassport->getLoginInfo(); if ($tmpResult->isSuccess()) { $user = $tmpResult->getData(); } return $user; } /** * 判断登陆状态是否满足活动要求,满足时返回true,不满足返回授权地址 * @param Boolean $isAuth 是否需要授权。true时需要loginType = Passport::LOGIN_TYPE_HAS_AUTH; false时loginType至少属于Passport::LOGIN_TYPE_NOT_AUTH; * @return ResultWrapper success 满足条件; fail 不满足条件,需要通过返回的授权url进行授权 */ public function checkLoginStatus($isAuth = false, $from) { $authUrl = Request::schemeDomain() . '/connect/'; if (!$isAuth) { if (!self::getUser()) { $state = 'base'; } } else { if (!self::isLogin()) { $state = 'userinfo'; } } if ($state) { return ResultWrapper::fail(String::jointUrl($authUrl, array('from' => $from, 'state' => $state))); } return ResultWrapper::success(); } public function __construct() { if (self::isLogin()) { define('IS_LOGIN', 1); define('UID', self::getUid()); define('NICKNAME', self::getNickname()); define('AVATAR', self::getHeadimgurl()); } else { // $tmpResult = self::checkLoginStatus(false, Request::url()); // if (!$tmpResult->isSuccess()) { // self::redirect($tmpResult->getData()); // } define('IS_LOGIN', 0); } } public function run() { $action = $this->action; $this->$action(); } }