lxy254069025 2012-11-29 14:05:55 5275次浏览 2条回复 2 0 0
<?php
/**
 *
 * UCenter封装类
 *
 * 封装UCenter的各接口,不使用UCenter的原生方法访问,以免代码格式及结构发生混乱
 *
 */
class UCenter
{
    public $cookies;

    public function __construct ()
    {
        //
        // 加载UCenter配置文件
        //
        include_once Yii::getPathOfAlias('application.config') . DIRECTORY_SEPARATOR . 'ucenter_config.php';

        //
        // 加载UCenter 客户端
        //
        include_once Yii::getPathOfAlias('application.components.uc_client') . DIRECTORY_SEPARATOR . 'client.php';
    }

    /**
	 *
	 * 注册接口
	 *
	 * @params username 用户名
	 * @params password
	 * @params email
	 * @params questionid
	 * @params answer
	 * @params regip
	 *
	 * @return integer 大于 0:返回用户 ID,表示用户注册成功
     *                      -1:用户名不合法
     *                      -2:包含不允许注册的词语
     *                      -3:用户名已经存在
     *                      -4:Email 格式有误
     *                      -5:Email 不允许注册
     *                      -6:该 Email 已经被注册
     *
	 */
    public function register ($username, $password, $email, $questionid = 0, $answer = '', $regip = '')
    {
        return uc_user_register ($username, $password, $email, $questionid, $answer, $regip);
    }

    /**
     *
     * 登陆接口
     *
     * @params username
     * @params password
     * @params isuid
     * @params checkques
     * @params questionid
     * @params answer
     *
     * @return array
     *    0 => integer 大于 0:返回用户 ID,表示用户登录成功
     *                      -1:用户不存在,或者被删除
     *                      -2:密码错
     *                      -3:安全提问错
     *    1 => string 用户名
     *    2 => string 密码
     *    3 => string Email
     *    4 => bool 用户名是否重名
     *
     */
    public function login ($username, $password, $isuid = 0, $checkques = 0, $questionid = 0, $answer = '')
    {
        return uc_user_login ($username, $password, $isuid, $checkques, $questionid, $answer);
    }

    /**
     *
     * 同步登陆接口
     *
     * @params uid 用户 ID
     *
     * @return array 同步登录的地址
     *
     */
    public function synLogin ($uid)
    {
        $content = uc_user_synlogin ($uid);
        preg_match_all('/src\s*=\s*"(.+?)"/', $content, $matches);
        $this->sendNotice ($matches[1]);
        return;
    }

    /**
     *
     * 同步退出接口
     *
     * @return array 同步退出的地址
     *
     */
    public function synLogout ()
    {
        $content = uc_user_synlogout();
        preg_match_all('/src\s*=\s*"(.+?)"/', $content, $matches);
        $this->sendNotice ($matches[1]);
        return;
    }

    /**
     *
     * 获取用户数据
     *
     */
    public function getUser ($username, $isuid = 0)
    {
        return uc_get_user ($username, $isuid);
    }

    /**
     *
     * 检查 Email 地址接口
     *
     * @params email Email
     *
     * @return integer 1:成功
     *                 -4:Email 格式有误
     *                 -5:Email 不允许注册
     *                 -6:该 Email 已经被注册
     *
     */
    public function checkEmail ($email)
    {
        return uc_user_checkemail($email);
    }

    /**
     *
     * 检查用户名接口
     *
     * @params username 用户名
     *
     * @return integer 1:成功
     *                 -4:用户名不合法
     *                 -5:包含要允许注册的词语
     *                 -6:用户名已经存在
     *
     */
    public function checkUserName ($userName)
    {
        return uc_user_checkname($userName);
    }

    /**
	 *
	 * 更新用户资料
	 *
	 * @params username 用户名
	 * @params oldpw 旧密码
	 * @params newpw 新密码,如不修改为空
	 * @params email Email,如不修改为空
	 * @params ignoreoldpw 是否忽略旧密码
     *                     1:忽略,更改资料不需要验证密码
     *                     0:(默认值) 不忽略,更改资料需要验证密码
	 * @params questionid 安全提问索引
	 * @params answer 安全提问答案
	 *
	 * @return integer 1:更新成功
     *                 0:没有做任何修改
     *                 -1:旧密码不正确
     *                 -4:Email 格式有误
     *                 -5:Email 不允许注册
     *                 -6:该 Email 已经被注册
     *                 -7:没有做任何修改
     *                 -8:该用户受保护无权限更改
     *
	 */
    public function updateUser ($username, $oldpw, $newpw, $email = '', $ignoreoldpw = 0, $questionid = '', $answer = '')
    {
        return uc_user_edit ($username, $oldpw, $newpw, $email, $ignoreoldpw, $questionid, $answer);
    }

    /**
     *
     * 删除用户
     *
     * @params integer/array uid 用户名
     *
     * @return integer 1:成功
     *                 0:失败
     *
     */
    public function deleteUser ($uid)
    {
        return uc_user_delete ($uid);
    }

    /**
     *
     * 将同步消息的JS转发到redirect的地址
     *
     * @params urls 数组或者字符串,指定API地址
     *
     */
    public function sendNotice ($urls)
    {
        $user = Yii::app()->user;
        if (!is_array($urls) && is_string($urls))
        {
            $urls = array($urls);
        }
	    if ($user->hasFlash ('ucNoticeUrls'))
	    {
	        $tmp = $user->getFlash ('ucNoticeUrls');
	        if (!is_array($tmp) && is_string($tmp))
	        {
	            $tmp = array($tmp);
	        }
	        $urls = array_merge ($urls, $tmp);
	    }
	    $user->setFlash ('ucNoticeUrls', $urls);
    }
}
  • 回复于 2012-11-29 14:13 举报
    $_identity=new UserIdentity(trim($_POST['reg']['mobile']),UserEncryption::encryption(trim($_POST['reg']['password'])));
    $_identity->authenticate();
    Yii::app()->user->login($_identity, 0);
    

    这是写在api中的。
    然后在你的api中这么使用就同步登录了。。。

  • 回复于 2012-11-29 14:15 举报

    说明:在你的useridentity中的登录是先从uc中去查,查到了会返回id给你,再把你id查本站的user表,然后再设置getId;因为UC和你本站的加密不一样。。

您需要登录后才可以回复。登录 | 立即注册