Associate.class.php 12 KB
<?php
/**
 * 第三方互联的操作类
 * 
 * @name Facade_Associate
 * @package facade
 * @copyright yoho.inc
 * @version 5.0 (2014-02-10 17:28:08)
 * @author fei.hong <fei.hong@yoho.cn>
 */
class Facade_Associate
{
    private static $_service = null;
    private static $_userinfo = null;
    
    /**
     * 第三方互联服务
     * 
     * 单例模式实例化服务层对象
     *
     * @return object
     */
    private static function service()
    {
        if (null === self::$_service)
        {
            self::$_service = new Service_Associate();
        }
        return self::$_service;
    }
    
    /**
     * 用户信息服务
     * 
     * 单例模式实例化服务层对象
     * 
     * @return object
     */
    private static function userinfo()
    {
        if (null === self::$_userinfo)
        {
            self::$_userinfo = new Service_Auth();
        }
        return self::$_userinfo;
    }
    
    /**
     * 设置第三方的关联信息
     *
     * @param string $auth  唯一标识(第三方用户)
     * @param integer type  第三方类型:3:weibo,4:qq,5:支付宝,6:人人,7:豆瓣,8:facebook,9:instagram
     * @param integer $uid  用户的ID
     * @param string $token  访问令牌
     * @param string $refreshToken  刷新令牌
     * @param string $expiresIn  使用周期(访问令牌)
     * @return boolean (false:失败, true:成功)
     * 
     * @assert (null, 1, 0, '', '', '0') == false
     * @assert ("xxxxx", 1, 3932011, "abcdef123456", "", "123456") == true
     */
    public static function set($auth, $type, $uid, $token, $refreshToken, $expiresIn)
    {
        $result = false;
    
        if ($auth && $type && $uid && $token && $expiresIn)
        {
            $result = self::service()->set($auth, $type, $uid, $token, $refreshToken, $expiresIn);
        }
    
        return $result;
    }
    
    /**
     * 获取第三方的关联信息
     *
     * @param string $auth  唯一标识(第三方用户)
     * @param integer type  第三方类型:3:weibo,4:qq,5:支付宝,6:人人,7:豆瓣,8:facebook,9:instagram
     * @return array
     * 
     * @assert (0, "1") == array()
     */
    public static function get($auth, $type)
    {
        $result = array();
    
        if ($auth && is_numeric($type))
        {
            $result = self::service()->get($auth, $type);
        }
    
        return $result;
    }
    
    /**
     * 更新第三方的关联信息
     *
     * @param string $auth  唯一标识(第三方用户)
     * @param integer type  第三方类型:3:weibo,4:qq,5:支付宝,6:人人,7:豆瓣,8:facebook,9:instagram
     * @param array $param  更新的参数项
     * @return boolean (false:失败, true:成功)
     */
    public static function upd($auth, $type, $param)
    {
        $result = array();
    
        if ($auth && $type && $param)
        {
            $result = self::service()->upd($auth, $type, $param);
        }
    
        return $result;
    }
    
    /**
     * 删除第三方的关联信息
     *
     * @param string $auth  唯一标识(第三方用户)
     * @param integer type  第三方类型:3:weibo,4:qq,5:支付宝,6:人人,7:豆瓣,8:facebook,9:instagram
     * @return boolean (false:失败, true:成功)
     */
    public static function del($auth, $type)
    {
        $result = array();
    
        if ($auth && $type)
        {
            $result = self::service()->del($auth, $type);
        }
    
        return $result;
    }
    
    /**
     * 获取用户的第三方关联信息
     *
     * @param integer $uid  站内用户ID
     * @return array
     */
    public static function getByUid($uid)
    {
        $result = array();
    
        if ($uid)
        {
            $result = self::service()->getByUid($uid);
        }
    
        return $result;
    }
    
    /**
     * 设置第三方信息,并生成唯一的UID
     * 
     * @param array $param  参数项
     * @return integer
     */
    public static function setBasicInfo($param)
    {
        $result = 0;
        
        if ($param)
        {
            $result = self::service()->setBasicInfo($param);
        }
        
        return $result;
    }

    /**
     * 第三方账号登录
     * 
     * @param integer $type  第三方类型:3:weibo,4:qq,5:支付宝,6:人人,7:豆瓣,8:facebook,9:instagram
     * @param string $auth  唯一标识(第三方用户)
     * @param string $nick  用户的昵称
     * @param string $headpic  用户的头像
     * @param string $alipay_email  支付宝用户的邮箱或手机号,用于合并有货的老数据,必须的参数
     * @param type $fields  需要返回的信息
     * @param type $scope  允许返回的信息集合
     * @return array
     */
    public static function login($type, $auth, $nick, $headpic, $alipayEmail, $fields, $scope, $appId)
    {
        $result = array();
        
        do 
        {
            /* 校验传递的参数是否有效 */
            if (!is_numeric($type) || $type > 11 || !$auth || !is_string($fields))
            {
                $result = Config_Code::$error['missing_parameter'];
                break;
            }

            $authInfo = array();
            
            $isOld = false;
            $isAlipay = false;
            
            // 获取有货支付宝老账号信息
            if (!empty($alipayEmail) && $type == Facade_Auth::AUTH_TYPE_ALIPAY)
            {
                $isAlipay = true;
                // 检查支付宝类型
                $authInfo = self::userinfo()->getAuthByTypeAndAid($type, $alipayEmail);
                // 检查是邮箱或手机号类型
                if (empty($authInfo))
                {
                    $isOld = true;
                    $genType = is_numeric($alipayEmail) ? Facade_Auth::AUTH_TYPE_MOBILE : Facade_Auth::AUTH_TYPE_EMAIL;
                    $authInfo = self::userinfo()->getAuthByTypeAndAid($genType, $alipayEmail);
                }
            }
            // 获取其它第三方的老账号信息
            elseif (!empty($alipayEmail))
            {
                $isAlipay = true;
                $isOld = true;
                
                $genType = Facade_Auth::AUTH_TYPE_EMAIL;
                $authInfo = self::userinfo()->getAuthByTypeAndAid($genType, $alipayEmail);
            }
            // 当不是有货时则需要先检查邮箱账号,新浪微博账号除外,因SHOW的新浪微博账号已有用户发表的数据
            elseif ($appId != '1392109259' && $type != Facade_Auth::AUTH_TYPE_WEIBO)
            {
                $format = '';
                switch ($type)
                {
                    case Facade_Auth::AUTH_TYPE_QQ:
                        $format = 'qq__%s@%s';
                        break;
                    case Facade_Auth::AUTH_TYPE_RENREN:
                        $format = 'renren__%s@%s';
                        break;
                    case Facade_Auth::AUTH_TYPE_DOUBAN:
                        $format = 'douban__%s@%s';
                        break;
                }
                
                if ($format !== '')
                {
                    $isAlipay = true;
                    $isOld = true;
                
                    $genType = Facade_Auth::AUTH_TYPE_EMAIL;
                    $alipayEmail = sprintf($format, $auth, 'yohoinc.com');
                    $authInfo = self::userinfo()->getAuthByTypeAndAid($genType, $alipayEmail);
                    if (empty($authInfo))
                    {
                        $alipayEmail = sprintf($format, $auth, 'yohoe.cn');
                        $authInfo = self::userinfo()->getAuthByTypeAndAid($genType, $alipayEmail);
                    }
                }
            }
            
            if (empty($authInfo))
            {
                /*检查老的facebook账号是否存在新的facebook的openid*/
                if($type==Facade_Auth::AUTH_TYPE_FACEBOOK)
                {
                    $oldOpenid = self::userinfo()->getFacebookRela($auth);
                    if($oldOpenid)
                    {
                        $auth = $oldOpenid;
                    }
                }
                $authInfo = self::userinfo()->getAuthByTypeAndAid($type, $auth);
            }
            elseif ($isAlipay)
            {
                // 邮箱登录或手机登录方式
                if ($isOld)
                {
                    self::userinfo()->setAuth($authInfo['uid'], $type, $auth);
                }
                // 第三方登录方式
                else
                {
                    self::userinfo()->updAuth($authInfo['uid'], $type, $authInfo['auth_id'], $auth);
                }
            }
            
            /* 检验并获取认证信息 */
            if ($authInfo && isset($authInfo['uid'])) 
            {
                $userinfo = self::userinfo()->getUserinfo($authInfo['uid'], $fields, $scope);
                $result = empty($userinfo) ? array('uid' => $authInfo['uid'], 'app_id' => $appId) : ((isset($userinfo['state']) && !$userinfo['state']) ? Config_Code::$error['user_disabled'] : $userinfo);
                break;
            }

            /* 生成新用户并保存登录验证信息 */
            $uid = self::userinfo()->setUserinfo(array('nick' => $nick, 'gender' => 3, 'headpic' => $headpic, 'app_id' => $appId));
            if (!$uid || !self::userinfo()->setAuth($uid, $type, $auth) )
            {
                $result = Config_Code::$error['account_set'];
                break;
            }
            
            /* 尝试获取订阅的用户信息 */
            $result = self::userinfo()->getUserinfo($uid, $fields, $scope);
            if (!$result)
            {
                $result = array('uid' => $authInfo['uid'], 'app_id' => $appId);
                break;
            }

        } while (false);
        
        return $result;
    }
    
    /**
     * 绑定手机号或邮箱
     * 
     * @param integer $uid  用户的ID
     * @param string $account  账号
     * @param string $password  密码
     * @return array
     */
    public static function bind($uid, $account, $password)
    {
        $result = array();
        
        do
        {
            /* 检查是否存在登录用户 */
            if (!$uid)
            {
                $result = Config_Code::$error['user_no_login'];
                break;
            }
            
            /* 验证账号是否为空 */
            if (empty($account))
            {
                $result = Config_Code::$error['account_format'];
                break;
            }
            
            /* 验证账号的类型 */
            $authType = null;
            if (filter_var($account, FILTER_VALIDATE_EMAIL))
            {
                $authType = Facade_Auth::AUTH_TYPE_EMAIL;
            }
            elseif (preg_match('/^1[3|4|5|8][0-9]\d{8}$/', $account))
            {
                $authType = Facade_Auth::AUTH_TYPE_MOBILE;
            }
            else
            {
                $result = Config_Code::$error['account_format'];
                break;
            }
            
            /* 验证密码 */
            if (empty($password) || strlen($password) < 6)
            {
                $result = Config_Code::$error['pwd_format'];
                break;
            }
            
            /* 获取用户的所有口令 */
            $userInfo = self::userinfo()->getAllAuth($uid);
            if (empty($userInfo))
            {
                $result = Config_Code::$error['user_not_exist'];
                break;
            }
            
            /* 生成并保存登录密码 */
            $pwdInfo = Facade_Auth::buildPassword($password);
            $status = self::userinfo()->setPassword($uid, $pwdInfo['password']);
            if (!$status)
            {
                $result = Config_Code::$error['system'];
                break;
            }
            
            $result = Config_Code::$success['user_register'];
        }
        while (false);
        
        return $result;
    }
    
}