Autosign.php 10.9 KB
<?php

use Action\AbstractAction;
use LibModels\Web\Passport\LoginData;
use Passport\PassportModel as PassportModel;
use Plugin\Helpers;
use Plugin\Partner\Factory;

class AutosignController extends AbstractAction
{

    /**
     * 支付宝登录:授权页面
     */
    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(){
    	$url = QINWechat_Sdk_Open::getCode('http://www.yohobuy.com/passport/autosign/wechatback');
    	$this->helpGo($url);
    }

    /**
     * 支付宝账号登录:回调方法
     */
    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']['mobile']) && $result['data']['mobile'] == '') {
            $this->go(Helpers::url('/passport/bind/index', array('openId' => $userId, 'sourceType' => 'alipay', 'nickname' => $realName)));
        }

        $refer = $this->getCookie('refer');
        if (empty($refer)) {
            $refer = SITE_MAIN . '/?go=1';
        }
        else {
            $refer = rawurldecode($refer);
        }

        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);
        }

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

        $refer = $this->getCookie('refer');
        if (empty($refer)) {
            $refer = SITE_MAIN . '/?go=1';
        }
        else {
            $refer = rawurldecode($refer);
        }

        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']['mobile']) && $result['data']['mobile'] == '') {
//            $this->go(Helpers::url('/passport/bind/index', array('openId' => $access['uid'], 'sourceType' => 'sina', 'nickname' => $partnerInfo['screen_name'])));
        }

        $refer = $this->getCookie('refer');
        if (empty($refer)) {
            $refer = SITE_MAIN . '/?go=1';
        }
        else {
            $refer = rawurldecode($refer);
        }

        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();
        /* 获取用户的详细信息 */
        $partnerInfo = $renren->getUserInfo($access);

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

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

        $refer = $this->getCookie('refer');
        if (empty($refer)) {
            $refer = SITE_MAIN . '/?go=1';
        }
        else {
            $refer = rawurldecode($refer);
        }

        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()
    {
        $sina = Factory::create('douban');
        $access = $sina->getAccessToken();
        /* 获取用户的详细信息 */
        $partnerInfo = $sina->getUserInfo($access);

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

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

        $refer = $this->getCookie('refer');
        if (empty($refer)) {
            $refer = SITE_MAIN . '/?go=1';
        }
        else {
            $refer = rawurldecode($refer);
        }

        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(){
    	$code = $this->helpGquery('code');
    	$accessToken = QINWechat_Sdk_Open::getAccessToken($code);
    	$userInfo = array();
    	if(empty($accessToken)){
    		$this->helpGo('/signin.html');
    	}
    	if(!isset($accessToken['access_token']) || !isset($accessToken['unionid']) || empty($accessToken['access_token']) || empty($accessToken['unionid'])){
    		$this->helpGo('/signin.html');
    	}
    	$userInfo = QINWechat_Sdk_Open::getUserInfo($accessToken['access_token'], $accessToken['unionid']);
    	if(empty($userInfo)){
    		$this->helpGo('/signin.html');
    	}
    	$this->wechatlogin($userInfo);
    }
    
    /**
     * 微信登录
     * @param array $userInfo
     */
    public function wechatlogin($userInfo){
   		if(empty($userInfo)){
   			$this->helpGo('/signin.html');
   		}
    	try {
    		$oauthUserInfo = QINAuth_User_Utils::check('wechat', $userInfo['unionid'], $userInfo['nickname'], null, $userInfo);
    		$auth = QINAuth_Factory::profile('wechat');
    		$res=$auth->associate($oauthUserInfo['email'], array(
    				'password' => QINAuth_User_Utils::$defaultPassword,
    				'open_id' => $userInfo['unionid'],
    				'nick_name' => $userInfo['nickname']
    		));
    	} catch (Exception $e) {
    		$this->helpSession('passport_space')->__set('error_message', 'wechat' . $e->getMessage());
    		$this->helpGo('/signin.html');
    	}
    	//跳转到
    	$url = $this->authInfo($res, 'wechat' , $oauthUserInfo['email']);
    	$this->helpGo($url);
    }

}