Auth.class.php 10.9 KB
<?php

/**
 * 用户数据层
 * 
 * @name Service_Auth
 * @copyright yoho.inc
 * @version 0.1
 * @author XMG
 */
class Service_Auth extends Lib_Service
{

    const ROUTER = 'auth.yh_uuc.auth';
    const EXPIRE = 3600;

    /**
     * 定义允许的参数项
     *
     * @see table yh_admin.tbl_aduiting_blog
     * @var array
     */
    private static $_columns_userinfo = array
        (
        'uid' => '0',
        'nick' => '',
        'gender' => 0,
        'age' => '',
        'birthday' => '',
        'job' => '',
        'adress' => '',
        'area' => '',
        'city' => '',
        'province' => '',
        'country' => '',
        'email' => '',
        'wechat' => '',
        'qq' => '',
        'msn' => '',
        'telephone' => '',
        'mobile_phone' => '',
        'real_name' => '',
        'head_pic' => '',
        'income' => '',
        'state' => '',
        'create_time' => '',
        'app_id' => '',
    );
    private static $TABLE_BASE_INFO = 'tbl_basic_info';
    private static $TABLE_AUTH_INFO = 'tbl_auth_info';
    
    private static $_tag_auth = 'tag_yh_uuc_tbl_auth_info_';
    private static $_key_auth = 'key_yh_uuc_tbl_auth_info_';
    
    private static $_tag_password = 'tag_yh_uuc_tbl_password_';
    private static $_key_password = 'key_yh_uuc_tbl_password_';
    
    private static $_tag_userinfo = 'tag_yh_uuc_tbl_userinfo_';
    private static $_key_userinfo = 'key_yh_uuc_tbl_userinfo_';

    private static $_tag_show_relevance = 'tag_yh_uuc_tbl_show_relevance_';
    private static $_key_show_relevance = 'key_yh_uuc_tbl_show_relevance_';
    
    /****清除用户缓存***********/
    
    public function cleanUserCache($uid)
    {
        $tags = array
        (
            self::$_tag_userinfo . $uid
        );
        self::service(self::ROUTER)->delTags($tags);
        self::service(self::ROUTER)->tag(self::$_tag_password)->delKeys(self::$_key_password . $uid);
    }
    
    
    /*     * ***************************** AUTH信息操作----开始 ********************************** */

    /**
     * 根据账号名称获取用户账号信息
     * @param unknown $type
     * @param unknown $auth_id
     */
    public function getAuthByTypeAndAid($type, $auth_id)
    {
        $tag = self::$_tag_auth . strval($type);
        $key = sprintf(self::$_key_auth . '%s_%s', $type, $auth_id);
        $param = array('type' => $type, 'auth_id' => $auth_id);

        return self::service(self::ROUTER)->tag($tag)->key($key)->expire(300)->fetchRow('getAuthByTypeAndAid', $param);
    }

    /**
     * 绑定登录账号
     * @param unknown $uid
     * @param unknown $type
     * @param unknown $auth_id
     * @param unknown $password
     * @param unknown $password_salt
     * @param number $state
     */
    public function setAuth($uid, $type, $auth_id)
    {
        $tag = self::$_tag_auth . strval($type);
        $key = sprintf(self::$_key_auth . '%s_%s', $type, $auth_id);
        $params = array(
            'uid' => $uid,
            'type' => $type,
            'auth_id' => $auth_id,
            'create_time' => time(),
        );
        return self::service(self::ROUTER)->tag($tag)->key($key)->insert('setAuth', $params)->status();
    }
    
    /**
     * 绑定登录账号
     * @param unknown $uid
     * @param unknown $type
     * @param unknown $auth_id
     * @param unknown $password
     * @param unknown $password_salt
     * @param number $state
     */
    public function delAuth($uid, $type, $auth_id)
    {
        $tag = self::$_tag_auth . strval($type);
        $key = sprintf(self::$_key_auth . '%s_%s', $type, $auth_id);
        $params = array(
            'uid' => $uid,
            'type' => $type,
            'auth_id' => $auth_id,
        );
        return self::service(self::ROUTER)->tag($tag)->key($key)->delete('delAuth', $params)->status();
    }

    /**
     * 修改登录账号
     * 
     * @param integer $id  唯一的SSO ID
     * @param integer $type  登录类型
     * @param string $auth_id  原来的登录账号
     * @param string $new_auth_id  新的登录账号
     * @return boolean  (false:失败, true:成功)
     */
    public function updAuth($id, $type, $auth_id, $new_auth_id)
    {
        $result = false;

        if (is_numeric($id) && is_numeric($type) && isset($auth_id) && isset($new_auth_id))
        {
            $tag = self::$_tag_auth . strval($type);
            $key = sprintf(self::$_key_auth . '%s_%s', $type, $auth_id);
            $param = array('auth_id' => $new_auth_id, 'id' => $id,'type'=>$type);
            $result = self::service(self::ROUTER)->tag($tag)->key($key)->update('updAuth', $param)->status();
        }

        return $result;
    }

    /**
     * 获取所有口令列表
     */
    public function getAllAuth($uid)
    {
        return self::service(self::ROUTER)->cache(false)->fetchAssoc('getAllAuth', array('uid' => $uid));
    }

    /*     * ***************************** AUTH信息操作----结束********************************** */



    /*     * ***************************** 基本信息操作----开始 ********************************** */

    /**
     * 创建用户基本信息
     * @param unknown $params
     * @return Ambigous <boolean, number>
     */
    public function setUserinfo($params)
    {
        $params['create_time'] = time();
        $params = array_intersect_key($params, self::$_columns_userinfo);
        if (!$params)
        {
            return false;
        }
        $replaces = array();
        $replaces['fields'] = Util_Utils_SqlString::mergeInsertFields($params);
        $replaces['string'] = Util_Utils_SqlString::mergeInsertString($params);
        return self::service(self::ROUTER)->cache(false)->insert('setUserinfo', $params, $replaces)->lastInsertId();
    }

    /**
     * 更新用户基本信息
     * @param unknown $params
     * @return Ambigous <boolean, number>
     */
    public function updateUserinfo($uid, $params)
    {
        $params = array_intersect_key($params, self::$_columns_userinfo);
        if (!$params)
        {
            return false;
        }
        $params['uid'] = $uid;
        $replaces['upstring'] = Util_Utils_SqlString::mergeSqlUpstring($params);
        return self::service(self::ROUTER)->tag(self::$_tag_userinfo . $uid)->update('updateUserinfo', $params, $replaces)->status();
    }

    /**
     * 获取用户基本信息
     * 
     * @param integer $uid  用户ID
     * @param string $fields  需要查询的字段集合,多个用“,”号隔开
     * @param string $allowFields  允许的字段集合,默认为数据库表中所有的
     */
    public function getUserinfo($uid, $fields = '*', $allowFields = '')
    {
        $result = array();

        // 得到 允许访问的信息列表,将字符串转换为数组格式
        $allowFields = ($allowFields !== '') ? explode(',', $allowFields) : array_keys(self::$_columns_userinfo);
        // 得到 需要访问的信息列表, 将字符串转换为数组格式
        $fields = ($fields !== '' && $fields !== '*') ? explode(',', $fields) : $allowFields;
        // 比较 需要访问的信息 与 允许访问的信息,取交集:得到可以访问的信息
        if ($fields !== $allowFields)
        {
            $fields = array_intersect($fields, $allowFields);
        }
        // 执行数据查询操作
        if ($fields && is_numeric($uid))
        {
            if(!in_array('app_id',$fields))
            {
                $fields[] = 'app_id';
            }
            $columns = implode(',', $fields);
            $tag = self::$_tag_userinfo . $uid;
            $key = self::$_key_userinfo . md5($columns);
            $result = self::service(self::ROUTER)->tag($tag)->key($key)->expire(self::EXPIRE)->fetchRow('getUserinfo', array('uid' => $uid), array('fields' => $columns));
        }

        return $result;
    }

    /**
     * 物理删除用户所有信息 --- 在注册的时候出现失败,回滚会用到 
     * @param unknown $uid
     */
    public function _deleteUserinfo($uid)
    {
        return self::service(self::ROUTER)->tag(self::$_tag_userinfo . $uid)->delete('deleteUser', array('uid' => $uid));
    }

    /*     * ***************************** 基本信息操作----结束 ********************************** */


    /*     * ***************************** 密码信息操作----开始 ********************************** */

    /**
     * 设置/更新密码
     * @param unknown $uid
     * @param unknown $password
     * @param unknown $salt
     */
    public function setPassword($uid, $password, $salt)
    {
        return self::service(self::ROUTER)->tag(self::$_tag_password)->key(self::$_key_password . $uid)->insert('setPassword', array('uid' => $uid, 'password' => $password, 'password_salt' => $salt))->status();
    }

    /**
     * 设置/更新密码
     * @param unknown $uid
     * @param unknown $password
     * @param unknown $salt
     */
    public function getPassword($uid)
    {
        return self::service(self::ROUTER)->tag(self::$_tag_password)->key(self::$_key_password . $uid)->fetchRow('getPassword', array('uid' => $uid));
    }

    /*     * ***************************** 基本信息操作----结束 ********************************** */
    
    /**
     * 关联有货
     * @param unknown $uid
     * @param unknown $sso_uid
     */
    public function relevanceYohobuyAccount( $show_uid, $sso_uid)
    {
        try 
        {
            return self::service(self::ROUTER)->tag(self::$_tag_show_relevance)->key(self::$_key_show_relevance . $show_uid)->insert('relevanceYohobuyAccount', array('show_uid' => $show_uid,'sso_uid' => $sso_uid));
        }catch (Exception $e)
        {
        	return false ;
        }
    }
    
    /**
     * 解除关联有货
     * @param unknown $uid
     * @param unknown $sso_uid
     */
    public function removeYohobuyAccount( $show_uid, $sso_uid)
    {
        return self::service(self::ROUTER)->tag(self::$_tag_show_relevance)->key(self::$_key_show_relevance . $show_uid)->delete('removeYohobuyAccount', array('show_uid' => $show_uid,'sso_uid' => $sso_uid));
    }
    
    /**
     * 获取关联有货
     * @param unknown $uid
     * @param unknown $sso_uid
     */
    public function getYohobuyAccountByShowId( $show_uid)
    {
        return self::service(self::ROUTER)->tag(self::$_tag_show_relevance)->key(self::$_key_show_relevance . $show_uid)->fetchRow('getYohobuyAccountByShowId', array('show_uid' => $show_uid));
    }
    
    /**
     * 根据sso_uid
     * @param unknown $sso_uid array
     * @return Ambigous <multitype:, Util_Dao_Db_PdoQuery>
     */
    public function getShowUidBySsoId($sso_uid)
    {
        return self::service(self::ROUTER)->cache(false)->fetchAssoc('getShowUidBySsoId', array(),array('sso_uid' => implode(',',$sso_uid)));
    }
    
    
    /**
     * 根据新openid获取老ID
     * @param unknown $openid
     */
    public function getFacebookRela($openid)
    {
        return self::service(self::ROUTER)->cache(false)->fetchOne('getFacebookRela', array('new_openid'=>$openid));
    }
}