Oauth.class.php 5.2 KB
<?php
/**
 * 使用第三方合作账号互联
 * 
 * @name Controller_Oauth
 * @package controller
 * @copyright yoho.inc
 * @version 5.0 (2014-2-12 13:42:44)
 * @author fei.hong <fei.hong@yoho.cn>
 */
class Controller_Oauth extends Controller_Abstract
{
    const API_NAME_QQ = 'qqconnect';
    const API_NAME_SINA = 'sinaweibo';
    const API_NAME_RENREN = 'renren';
    const API_NAME_DOUBAN = 'douban';
    const API_NAME_ALIPAY = 'alipay';
    const API_NAME_FACEBOOK = 'facebook';
    const API_NAME_INSTAGRAM = 'instagram';
    
    /**
     * 初始化
     */
    protected function init() {}
    
    /**
     * 统一入口
     */
    public function indexAction()
    {
    	$t = strtolower(trim($this->_request->t)) ;
    	if($t == '')
    	{
    		return 'Missing Parameter';
    	}
    	$fun = $t.'Action';
    	if (method_exists($this, $fun)) 
    	{
    		return $this->$fun();
    	}
    	return 'System Error';
    }
    
    /**
     * QQ互联: 授权
     */
    public function qqAction()
    {
        $apiObj = Lib_Partner_Factory::create(self::API_NAME_QQ);
        
        $apiObj->getAuthorizeUrl();
        
        exit;
    }
    
    /**
     * QQ互联: 回调
     * 
     * @todo test interface
     */
    public function qqcallbackAction()
    {
        $apiObj = Lib_Partner_Factory::create(self::API_NAME_QQ);
        
        $token = $apiObj->getAccessToken();
        
        var_dump($token); exit;
    }
    
    /**
     * 新浪微博: 授权
     */
    public function sinaAction()
    {
        $apiObj = Lib_Partner_Factory::create(self::API_NAME_SINA);
        
        $authorizeUrl = $apiObj->getAuthorizeUrl();
        
        header('Location:' . $authorizeUrl); exit;
    }
    
    /**
     * 新浪微博:回调
     * 
     * @todo test interface
     */
    public function sinacallbackAction()
    {
        $apiObj = Lib_Partner_Factory::create(self::API_NAME_SINA);
        
        $token = $apiObj->getAccessToken();
        
        var_dump($token); exit;
    }
    
    /**
     * 人人网: 授权
     */
    public function renrenAction()
    {
        $apiObj = Lib_Partner_Factory::create(self::API_NAME_RENREN);
        
        $authorizeUrl = $apiObj->getAuthorizeUrl();
        
        header('Location:' . $authorizeUrl); exit;
    }
    
    /**
     * 人人网: 回调
     * 
     * @todo test interface
     */
    public function renrencallbackAction()
    {
        $apiObj = Lib_Partner_Factory::create(self::API_NAME_RENREN);
        
        // 返回object
        $token = $apiObj->getAccessToken();
        
        // 返回array, 所括user信息
        $tokens = $apiObj->getTokens();
        
        var_dump($token, $tokens); exit;
    }
    
    /**
     * 豆瓣网: 授权
     */
    public function doubanAction()
    {
        $apiObj = Lib_Partner_Factory::create(self::API_NAME_DOUBAN);
        
        $authorizeUrl = $apiObj->getAuthorizeUrl();
        
        header('Location:' . $authorizeUrl); exit;
    }
    
    /**
     * 豆瓣网: 回调
     * 
     * @todo test interface
     */
    public function doubancallbackAction()
    {
        $apiObj = Lib_Partner_Factory::create(self::API_NAME_DOUBAN);
        
        $token = $apiObj->getAccessToken();
        
        var_dump($token); exit;
    }
    
    /**
     * Facebook: 授权
     */
    public function facebookAction()
    {
        $apiObj = Lib_Partner_Factory::create(self::API_NAME_FACEBOOK);
        
        $authorizeUrl = $apiObj->getAuthorizeUrl();
        
        header('Location:' . $authorizeUrl); exit;
    }
    
    /**
     * Facebook: 回调
     * 
     * @todo test interface
     */
    public function facebookcallbackAction()
    {
        $apiObj = Lib_Partner_Factory::create(self::API_NAME_FACEBOOK);
        
        $token = $apiObj->getAccessToken();
        
        var_dump($token); exit;
    }
    
    /**
     * Instagram: 授权
     */
    public function instagramAction()
    {
        $apiObj = Lib_Partner_Factory::create(self::API_NAME_INSTAGRAM);
        
        $authorizeUrl = $apiObj->getAuthorizeUrl();
        
        header('Location:' . $authorizeUrl); exit;
    }
    
    /**
     * Instagram: 回调
     * 
     * @todo test interface
     */
    public function instagramcallbackAction()
    {
        $apiObj = Lib_Partner_Factory::create(self::API_NAME_INSTAGRAM);
        
        $token = $apiObj->getAccessToken();
        
        $userInfo = $apiObj->getUserInfo($token);
        
        var_dump($token, $userInfo); exit;
    }
    
    /**
     * 支付宝快捷登录
     */
    public function alipayAction()
    {
        Lib_Partner_Factory::create(self::API_NAME_ALIPAY)->getAuthorizeUrl();
    }
    
    /**
     * 支付宝快捷登录:回调
     * 
     * @todo test interface
     */
    public function alipaycallbackAction()
    {
        var_dump($this->_request->pathinfo());
        
        if (isset($_GET['oauth/alipaycallback']))
        {
            unset($_GET['oauth/alipaycallback']);
        }
        unset($_GET['module'], $_GET['namespace'], $_GET['controller'], $_GET['action']);
        
        $token = Lib_Partner_Factory::create(self::API_NAME_ALIPAY)->getAccessToken();
        
        var_dump($token); exit;
    }
    
}