123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- namespace KIF\Core;
- use Cas\Module\Passport;
- use KIF\Data\ResultWrapper;
- use KIF\String\String;
- /**
- *
- * 继承自Controller,封装了微信用户登录状态、用户信息等一些方法
- * @author lishumingoo@gmail.com
- */
- class WXController extends \KIF\Core\Controller {
-
- /**
- *
- * 判断是否登录
- * @return boolean
- */
- public function isLogin() {
- $user = self::getUser();
- if (!$user) {
- return false;
- }
-
- if ($user['loginType'] == Passport::LOGIN_TYPE_NOT_AUTH) {
- return false;
- }
-
- return true;
- }
-
- /**
- *
- * 获取用户id
- * @return boolean | int
- */
- public function getUid() {
- $user = $this->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();
- }
- }
|