Autosign.php 11.7 KB
<?php

use Action\WebAction;
use LibModels\Web\Passport\LoginData;
use WebPlugin\Helpers;
use Passport\PassportModel as PassportModel;
use WebPlugin\Partner\Factory;

class AutosignController extends WebAction
{

    /**
     * 支付宝登录:授权页面
     */
    public function alipayAction()
    {
        $this->setSession('_TOKEN', '');

        Factory::create('alipay')->getAuthorizeUrl();

        exit();
    }

    /**
     * QQ账号登录:授权页面
     */
    public function qqAction()
    {
        $this->setSession('_TOKEN', '');

        Factory::create('qqconnect')->getAuthorizeUrl();

        exit();
    }

    /**
     * 新浪微博账号登录:授权页面
     */
    public function sinaAction()
    {
        $this->setSession('_TOKEN', '');

        $this->go(Factory::create('sinaweibo')->getAuthorizeUrl());
    }

    /**
     * 新浪微博账号登录:授权页面
     */
    public function renrenAction()
    {
        $this->setSession('_TOKEN', '');

        $this->go(Factory::create('renren')->getAuthorizeUrl());
    }

    /**
     * 豆瓣账号登录:授权页面
     */
    public function doubanAction()
    {
        $this->setSession('_TOKEN', '');

        $this->go(Factory::create('douban')->getAuthorizeUrl());
    }

    /**
     * 微信网站授权入口
     */
    public function wechatAction()
    {
        $this->setSession('_TOKEN', '');
        $this->go(Factory::create('wechat')->getAuthorizeUrl());
    }

    /**
     * 支付宝账号登录:回调方法
     */
    public function alipaycallbackAction()
    {
        $realName = $this->_request->get('real_name');
        $email = $this->_request->get('email');
        $userId = $this->_request->get('user_id');

        $result = array();
        if (isset($realName, $email, $userId)) {
            $shoppingKey = Helpers::getShoppingKeyByCookie();
            $result = LoginData::signinByOpenID($realName, $userId, 'alipay', $shoppingKey);
        }

        //判定是否需要绑定手机号
        if (isset($result['data']['is_bind']) && $result['data']['is_bind'] == 'N') {
            $fillHerf = Helpers::url('/passport/thirdlogin/index', array('openId' => $userId, 'sourceType' => 'alipay', 'nickname' => $realName));
            $this->go($fillHerf);
        }

        $refer = rawurldecode($this->getCookie('refer'));
        $isRedirectHome = PassportModel::redirectHome($refer);
        if (empty($refer) || $isRedirectHome) {
            $refer = SITE_MAIN;
        }

        if (isset($result['code']) && $result['code'] == 200 && !empty($result['data']['uid'])) {
            $token = Helpers::makeToken($result['data']['uid']);
            $this->setSession('_TOKEN', $token);
            $this->setSession('_LOGIN_UID', $result['data']['uid']);
            $this->setCookie('_TOKEN', $token);
            $this->go(Helpers::syncUserSession($result['data']['uid'], $refer));
        }
        else {
            $this->go($refer);
        }
    }

    /**
     * QQ账号登录:回调方法
     */
    public function qqcallbackAction()
    {
        $qqconnect = Factory::create('qqconnect');
        $access = $qqconnect->getAccessToken();
        /* 获取QQ腾讯用户的详细信息 */
        $partnerInfo = $qqconnect->getUserInfo($access);
        $result = array();
        if (!empty($partnerInfo) && isset($partnerInfo['nickname'])) {
            $shoppingKey = Helpers::getShoppingKeyByCookie();
            $result = LoginData::signinByOpenID($partnerInfo['nickname'], $access['openid'], 'qq', $shoppingKey);
        }

        $refer = rawurldecode($this->getCookie('refer'));
        $isRedirectHome = PassportModel::redirectHome($refer);
        if (empty($refer) || $isRedirectHome) {
            $refer = SITE_MAIN;
        }

        //判定是否需要绑定手机号
        if (isset($result['data']['is_bind']) && $result['data']['is_bind'] == 'N') {
            $fillHerf = Helpers::url('/passport/thirdlogin/index', array('openId' => $access['openid'], 'sourceType' => 'qq', 'nickname' => $partnerInfo['nickname']));
            $this->go($fillHerf);
        }

        if (isset($result['code']) && $result['code'] == 200 && !empty($result['data']['uid'])) {
            $token = Helpers::makeToken($result['data']['uid']);
            $this->setSession('_TOKEN', $token);
            $this->setSession('_LOGIN_UID', $result['data']['uid']);
            $this->setCookie('_TOKEN', $token);
            $this->go(Helpers::syncUserSession($result['data']['uid'], $refer));
        }
        else {
            $this->go($refer);
        }
    }

    /**
     * 新浪微博账号登录:回调方法
     */
    public function sinacallbackAction()
    {
        $sina = Factory::create('sinaweibo');
        $access = $sina->getAccessToken();
        /* 获取用户的详细信息 */
        $partnerInfo = $sina->getUserInfo($access);

        $result = array();
        if ($partnerInfo && is_array($partnerInfo)) {
            $shoppingKey = Helpers::getShoppingKeyByCookie();
            $result = LoginData::signinByOpenID($partnerInfo['screen_name'], $access['uid'], 'sina', $shoppingKey);
        }

        //判定是否需要绑定手机号
        if (isset($result['data']['is_bind']) && $result['data']['is_bind'] == 'N') {
            $fillHerf = Helpers::url('/passport/thirdlogin/index', array('openId' => $access['uid'], 'sourceType' => 'sina', 'nickname' => $partnerInfo['screen_name']));
            $this->go($fillHerf);
        }

        $refer = rawurldecode($this->getCookie('refer'));
        $isRedirectHome = PassportModel::redirectHome($refer);
        if (empty($refer) || $isRedirectHome) {
            $refer = SITE_MAIN;
        }

        if (isset($result['code']) && $result['code'] == 200 && !empty($result['data']['uid'])) {
            $token = Helpers::makeToken($result['data']['uid']);
            $this->setSession('_TOKEN', $token);
            $this->setSession('_LOGIN_UID', $result['data']['uid']);
            $this->setCookie('_TOKEN', $token);
            $this->go(Helpers::syncUserSession($result['data']['uid'], $refer));
        }
        else {
            $this->go($refer);
        }
    }

    /**
     * renren账号登录:回调方法
     */
    public function renrenbackAction()
    {
        $renren = Factory::create('renren');
        $access = $renren->getAccessToken();

        $result = array();
        if ($access && is_array($access)) {
            $shoppingKey = Helpers::getShoppingKeyByCookie();
            $result = LoginData::signinByOpenID($access['user']['name'], $access['user']['id'], 'renren', $shoppingKey);
        }

        //判定是否需要绑定手机号
        if (isset($result['data']['is_bind']) && $result['data']['is_bind'] == 'N') {
            $fillHerf = Helpers::url('/passport/thirdlogin/index', array('openId' => $access['user']['id'], 'sourceType' => 'renren', 'nickname' => $access['user']['name']));
            $this->go($fillHerf);
        }
        if (isset($result['data']['mobile']) && $result['data']['mobile'] == '') {
            $token = Helpers::makeToken($result['data']['uid']);
            $this->setSession('_TOKEN', $token);
            $this->setSession('_LOGIN_UID', $result['data']['uid']);
            $this->setCookie('_TOKEN', $token);
            $fillHerf = rawurlencode(Helpers::url('/passport/autouserinfo/userinfo', array('openId' => $access['user']['id'], 'sourceType' => 'renren', 'nickname' => $access['user']['name'])));
            $this->go(Helpers::syncUserSession($result['data']['uid'], $fillHerf));
        }

        $refer = rawurldecode($this->getCookie('refer'));
        $isRedirectHome = PassportModel::redirectHome($refer);
        if (empty($refer) || $isRedirectHome) {
            $refer = SITE_MAIN;
        }

        if (isset($result['code']) && $result['code'] == 200 && !empty($result['data']['uid'])) {
            $token = Helpers::makeToken($result['data']['uid']);
            $this->setSession('_TOKEN', $token);
            $this->setSession('_LOGIN_UID', $result['data']['uid']);
            $this->setCookie('_TOKEN', $token);
            $this->go(Helpers::syncUserSession($result['data']['uid'], $refer));
        }
        else {
            $this->go($refer);
        }
    }

    /**
     * douban账号登录:回调方法
     */
    public function doubanbackAction()
    {
        $douban = Factory::create('douban');
        $access = $douban->getAccessToken();
        /* 获取用户的详细信息 */
        $partnerInfo = $douban->getUserInfo($access);

        $result = array();
        if ($partnerInfo && is_array($partnerInfo)) {
            $shoppingKey = Helpers::getShoppingKeyByCookie();
            $result = LoginData::signinByOpenID($partnerInfo['name'], $partnerInfo['uid'], 'douban', $shoppingKey);
        }

        //判定是否需要绑定手机号
        if (isset($result['data']['is_bind']) && $result['data']['is_bind'] == 'N') {
            $fillHerf = Helpers::url('/passport/thirdlogin/index', array('openId' => $access['douban_user_id'], 'sourceType' => 'douban', 'nickname' => $partnerInfo['name']));
            $this->go($fillHerf);
        }

        $refer = rawurldecode($this->getCookie('refer'));
        $isRedirectHome = PassportModel::redirectHome($refer);
        if (empty($refer) || $isRedirectHome) {
            $refer = SITE_MAIN;
        }

        if (isset($result['code']) && $result['code'] == 200 && !empty($result['data']['uid'])) {
            $token = Helpers::makeToken($result['data']['uid']);
            $this->setSession('_TOKEN', $token);
            $this->setSession('_LOGIN_UID', $result['data']['uid']);
            $this->setCookie('_TOKEN', $token);
            $this->go(Helpers::syncUserSession($result['data']['uid'], $refer));
        }
        else {
            $this->go($refer);
        }
    }

    /**
     * 微信网站授权获取用户信息并登录
     * @param string code
     */
    public function wechatbackAction()
    {
        $wechat = Factory::create('wechat');
        $access = $wechat->getAccessToken();
        /* 获取用户的详细信息 */
        $partnerInfo = $wechat->getUserInfo($access);

        $result = array();
        if ($partnerInfo && is_array($partnerInfo)) {
            $shoppingKey = Helpers::getShoppingKeyByCookie();
            $result = LoginData::signinByOpenID($partnerInfo['nickname'], $partnerInfo['openid'], 'wechat', $shoppingKey, $partnerInfo['openid']);
        }

        //判定是否需要绑定手机号
        if (isset($result['data']['is_bind']) && $result['data']['is_bind'] == 'N') {
            $fillHerf = Helpers::url('/passport/thirdlogin/index', array('openId' => $partnerInfo['openid'], 'sourceType' => 'wechat', 'nickname' => $partnerInfo['nickname']));
            $this->go($fillHerf);
        }

        $refer = rawurldecode($this->getCookie('refer'));
        $isRedirectHome = PassportModel::redirectHome($refer);
        if (empty($refer) || $isRedirectHome) {
            $refer = SITE_MAIN;
        }

        if (isset($result['code']) && $result['code'] == 200 && !empty($result['data']['uid'])) {
            $token = Helpers::makeToken($result['data']['uid']);
            $this->setSession('_TOKEN', $token);
            $this->setSession('_LOGIN_UID', $result['data']['uid']);
            $this->setCookie('_TOKEN', $token);
            $this->go(Helpers::syncUserSession($result['data']['uid'], $refer));
        }
        else {
            $this->go($refer);
        }
    }

}