Associate.class.php 6.07 KB
<?php
/**
 * 第三方互联的数据存储类
 * 
 * @name Service_Associate
 * @package service
 * @copyright yoho.inc
 * @version 5.0 (2014-02-10 17:29:59)
 * @author fei.hong <fei.hong@yoho.cn>
 */
class Service_Associate extends Lib_Service
{
    const ROUTER = 'auth.yh_uuc.associate';
    
    const EXPIRE = 3600; // 缓存一小时
    
    private static $_key = 'key_yh_uuc_tbl_partner_info_';
    
    /**
     * 设置第三方的关联信息
     * 
     * @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:成功)
     */
    public function set($auth, $type, $uid, $token, $refreshToken, $expiresIn)
    {
        $result = false;
        
        if (is_string($auth) && is_numeric($type) && is_numeric($uid) 
            && is_string($token) && is_string($refreshToken) && is_numeric($expiresIn))
        {
            $key = array();
            $key[] = sprintf(self::$_key . '%s_%s', $auth, $type);
            $key[] = sprintf(self::$_key . 'byuid_%s', $uid);
            $param = array(
                'auth' => $auth, 'type' => $type, 'uid' => $uid, 'token' => $token, 
                'refresh_token' => $refreshToken, 'expires_in' => $expiresIn, 'create_time' => time(),
            );
            $result = self::service(self::ROUTER)->key($key)->insert('set', $param)->status();
        }
        
        return $result;
    }
    
    /**
     * 获取第三方的关联信息
     * 
     * @param string $auth  唯一标识(第三方用户)
     * @param @param integer type  第三方类型:3:weibo,4:qq,5:支付宝,6:人人,7:豆瓣,8:facebook,9:instagram
     * @return array
     */
    public function get($auth, $type)
    {
        $result = array();
        
        if (is_string($auth) && is_numeric($type))
        {
            $key = sprintf(self::$_key . '%s_%s', $auth, $type);
            $param = array('auth' => $auth, 'type' => $type,);
            $result = self::service(self::ROUTER)->key($key)->expire(self::EXPIRE)->fetchRow('get', $param);
        }
        
        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 function upd($auth, $type, $param)
    {
        $result = array();
        
        if (is_string($auth) && is_numeric($type) && is_array($param))
        {
            $key = sprintf(self::$_key . '%s_%s', $auth, $type);
            $replace = array('STRING' => Util_Utils_SqlString::mergeSqlUpstring($param));
            $param['auth'] = $auth;
            $param['type'] = $type;
            $result = self::service(self::ROUTER)->key($key)->update('upd', $param, $replace)->status();
        }
        
        return $result;
    }
    
    /**
     * 删除第三方的关联信息
     *
     * @param string $auth  唯一标识(第三方用户)
     * @param integer type  第三方类型:3:weibo,4:qq,5:支付宝,6:人人,7:豆瓣,8:facebook,9:instagram
     * @return boolean (false:失败, true:成功)
     */
    public function del($auth, $type)
    {
        $result = array();
        
        if (is_string($auth) && is_numeric($type))
        {
            $key = sprintf(self::$_key . '%s_%s', $auth, $type);
            $param = array('auth' => $auth, 'type' => $type,);
            $result = self::service(self::ROUTER)->key($key)->delete('del', $param);
        }
        
        return $result;
    }
    
    /**
     * 获取用户的第三方关联信息
     * 
     * @param integer $uid  站内用户ID
     * @return array
     */
    public function getByUid($uid)
    {
        $result = array();
        
        if (is_numeric($uid))
        {
            $key = sprintf(self::$_key . 'byuid_%s', $uid);
            $param = array('uid' => $uid);
            $result = self::service(self::ROUTER)->key($key)->expire(self::EXPIRE)->fetchAssoc('getByUid', $param);
        }
        
        return $result;
    }
    
    /**
     * 设置第三方信息,并生成唯一的UID
     *
     * @param array $param  参数项
     * @return integer
     */
    public function setBasicInfo($param)
    {
        $result = 0;
    
        if (is_array($param))
        {
            $replace = array();
            $replace['FIELDS'] = Util_Utils_SqlString::mergeInsertFields($param);
            $replace['STRING'] = Util_Utils_SqlString::mergeInsertString($param);
            
            $result = self::service(self::ROUTER)->cache(false)->insert('setBasicInfo', $param, $replace)->lastInsertId();
        }
    
        return $result;
    }
    
    /**
     * 设置第三方用户绑定的登录账号信息
     *
     * @param integer $uid  用户的ID
     * @param integer $type  第三方类型: 3:weibo,4:qq,5:支付宝,6:人人,7:豆瓣,8:facebook,9:instagram
     * @param string $account  账号
     * @param string $password  密码(加盐)
     * @param string $passwordSalt  盐
     * @return boolean (false:失败, true:成功)
     */
    public function setAuthInfo($uid, $type, $account, $password, $passwordSalt)
    {
        $result = false;
    
        if (is_numeric($uid) && is_numeric($type) && is_string($account) && is_string($password) && is_string($passwordSalt))
        {
            $param = array(
                'uid' => $uid, 'type' => $type, 'auth_id' => $account, 
                'password' => $password, 'password_salt' => $passwordSalt, 'create_time' => time()
            );
            $result = self::service(self::ROUTER)->cache(false)->insert('setAuthInfo', $param)->status();
        }
    
        return $result;
    }
    
}