discuz自带ucenter,主要就是用于和其他程序的通信.我们可以下载discuz的ucenter开发手册进行magento和discuz的集成.里面有一些ucenter的一些接口函数和参数说明,没有这个文档,最模板就无法进行下面:
1、把api和uc_client目录放在应用程序的跟目录.
2、修改自己的配置文件(application/config/config.ini),在最后加上下面的代码:代码如下
[ucenter]
uc.UC_ON = 0
uc.UC_CONNECT = mysql
uc.UC_DBHOST = localhost
uc.UC_DBUSER = root
uc.UC_DBPW = vertrigo
uc.UC_DBNAME = ucenter
uc.UC_DBCHARSET = utf8
uc.UC_DBTABLEPRE = ucenter.uc_
uc.UC_KEY = 123456789
uc.UC_API = http://localhost/comsenz/ucenter15
uc.UC_CHARSET = utf-8
uc.UC_IP =
uc.UC_APPID = 1
uc.UC_ON = 0
uc.UC_CONNECT = mysql
uc.UC_DBHOST = localhost
uc.UC_DBUSER = root
uc.UC_DBPW = vertrigo
uc.UC_DBNAME = ucenter
uc.UC_DBCHARSET = utf8
uc.UC_DBTABLEPRE = ucenter.uc_
uc.UC_KEY = 123456789
uc.UC_API = http://localhost/comsenz/ucenter15
uc.UC_CHARSET = utf-8
uc.UC_IP =
uc.UC_APPID = 1
复制代码
上面的各项一看就知道啥意思了,uc.UC_ON是系统中是否开启ucenter同步的开关,1:打开,0:关闭。注意uc.UC_KEY必须和 ucenter中设置
应用的key一致不然会显示通信失败。
3、修改index.php入口文件,加入配置解析,代码如下:
代码如下
//ucenter配置
Zend_Registry::set('UC_ON',$config->ucenter->uc->UC_ON);
if (Zend_Registry::get('UC_ON')) {
define('UC_CONNECT', $config->ucenter->uc->UC_CONNECT); // mysql 是直接连接的数据库, 为了效率, 建议采用 mysql
define('UC_DBHOST', $config->ucenter->uc->UC_DBHOST); // UCenter 数据库主机
define('UC_DBUSER', $config->ucenter->uc->UC_DBUSER); // UCenter 数据库用户名
define('UC_DBPW', $config->ucenter->uc->UC_DBPW); // UCenter 数据库密码
define('UC_DBNAME', $config->ucenter->uc->UC_DBNAME); // UCenter 数据库名称
define('UC_DBCHARSET', $config->ucenter->uc->UC_DBCHARSET); // UCenter 数据库字符集
define('UC_DBTABLEPRE', $config->ucenter->uc->UC_DBTABLEPRE); // UCenter 数据库表前缀
define('UC_KEY', $config->ucenter->uc->UC_KEY); // 与 UCenter 的通信密钥, 要与 UCenter 保持一致
define('UC_API', $config->ucenter->uc->UC_API); // UCenter 的 URL 地址, 在调用头像时依赖此常量
define('UC_CHARSET', $config->ucenter->uc->UC_CHARSET); // UCenter 的字符集
define('UC_IP', $config->ucenter->uc->UC_IP); // UCenter 的 IP
define('UC_APPID', $config->ucenter->uc->UC_APPID); // 当前应用的 ID
include './uc_client/client.php';
}
Zend_Registry::set('UC_ON',$config->ucenter->uc->UC_ON);
if (Zend_Registry::get('UC_ON')) {
define('UC_CONNECT', $config->ucenter->uc->UC_CONNECT); // mysql 是直接连接的数据库, 为了效率, 建议采用 mysql
define('UC_DBHOST', $config->ucenter->uc->UC_DBHOST); // UCenter 数据库主机
define('UC_DBUSER', $config->ucenter->uc->UC_DBUSER); // UCenter 数据库用户名
define('UC_DBPW', $config->ucenter->uc->UC_DBPW); // UCenter 数据库密码
define('UC_DBNAME', $config->ucenter->uc->UC_DBNAME); // UCenter 数据库名称
define('UC_DBCHARSET', $config->ucenter->uc->UC_DBCHARSET); // UCenter 数据库字符集
define('UC_DBTABLEPRE', $config->ucenter->uc->UC_DBTABLEPRE); // UCenter 数据库表前缀
define('UC_KEY', $config->ucenter->uc->UC_KEY); // 与 UCenter 的通信密钥, 要与 UCenter 保持一致
define('UC_API', $config->ucenter->uc->UC_API); // UCenter 的 URL 地址, 在调用头像时依赖此常量
define('UC_CHARSET', $config->ucenter->uc->UC_CHARSET); // UCenter 的字符集
define('UC_IP', $config->ucenter->uc->UC_IP); // UCenter 的 IP
define('UC_APPID', $config->ucenter->uc->UC_APPID); // 当前应用的 ID
include './uc_client/client.php';
}
复制代码
好了,准备工作完成了,下面要进行程序开发了。
4、登陆同步的实现,下面的代码有点长,因为我是开始没有集成ucenter,后来新增的ucenter,所以做了兼容性,兼顾了没有开启ucenter 的
登录。
代码如下
//如果开启了ucenter
if ($this->is_uc_on) {
//通过接口判断登录帐号的正确性,返回值为数组
list($uid, $username, $password, $email) = uc_user_login($username, $password);
if($uid > 0) {
//判断用户是否存在于用户表,不存在则跳转到激活页面
if($this->user->checkUserValid($username)) {
//将这些用户信息写入cookie,用于激活页面
@setcookie('zf_auth', uc_authcode($uid."t".$username."t".$password."t".$email, 'ENCODE'),time()+3600,'/');
$this->view->securl = $this->view->domain.'/user/activation';
$this->view->msg = '您需要需要激活该帐号,才能进入本应用程序!';
echo $this->view->render('success.php');
exit;
}
//用户登陆成功,设置 Cookie
$udata = array('id'=>$uid,'username'=>$username,'role'=>'member');
$expire = 86400;
$this->setCookie($udata,$expire);
//生成同步登录的代码
$ucsynlogin = uc_user_synlogin($uid);
$this->view->securl = $this->view->domain;
$this->view->msg = '登录成功,正在跳转回首页!';
echo $this->view->render('success.php');
} elseif($uid == -1) {
$this->view->error = "用户不存在,或者被删除!";
echo $this->view->render('error.php');exit;
} elseif($uid == -2) {
$this->view->error = "密码错误!";
echo $this->view->render('error.php');exit;
} else {
$this->view->error = "系统错误,请稍候再试!";
echo $this->view->render('error.php');exit;
}
}
else //没有采用uc的登录方式
{
$dbAdapter = Zend_Registry::get('dbAdapter');
$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
//$authAdapter->setTableName('bg_user');
$authAdapter->setTableName(Zend_Registry::get('dbprefix').'user');
$authAdapter->setIdentityColumn('username');
$authAdapter->setCredentialColumn('password');
$authAdapter->setIdentity($username);
$authAdapter->setCredential($password);
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($authAdapter);
if($result->isValid()){
$data = $authAdapter->getResultRowObject(null,'password');
//$auth->getStorage()->write($data);
//写入cookie
$udata = array('id'=>$data->id,'username'=>$data->username,'role'=>$data->role);
$expire = 86400;
$this->setCookie($udata,$expire);
$this->view->securl = $this->view->domain;
$this->view->msg = '登录成功,正在跳转回首页!';
echo $this->view->render('success.php');
}else{
//用户名密码
$this->view->error = "用户名密码错误!";
echo $this->view->render('error.php');
exit();
}
}
if ($this->is_uc_on) {
//通过接口判断登录帐号的正确性,返回值为数组
list($uid, $username, $password, $email) = uc_user_login($username, $password);
if($uid > 0) {
//判断用户是否存在于用户表,不存在则跳转到激活页面
if($this->user->checkUserValid($username)) {
//将这些用户信息写入cookie,用于激活页面
@setcookie('zf_auth', uc_authcode($uid."t".$username."t".$password."t".$email, 'ENCODE'),time()+3600,'/');
$this->view->securl = $this->view->domain.'/user/activation';
$this->view->msg = '您需要需要激活该帐号,才能进入本应用程序!';
echo $this->view->render('success.php');
exit;
}
//用户登陆成功,设置 Cookie
$udata = array('id'=>$uid,'username'=>$username,'role'=>'member');
$expire = 86400;
$this->setCookie($udata,$expire);
//生成同步登录的代码
$ucsynlogin = uc_user_synlogin($uid);
$this->view->securl = $this->view->domain;
$this->view->msg = '登录成功,正在跳转回首页!';
echo $this->view->render('success.php');
} elseif($uid == -1) {
$this->view->error = "用户不存在,或者被删除!";
echo $this->view->render('error.php');exit;
} elseif($uid == -2) {
$this->view->error = "密码错误!";
echo $this->view->render('error.php');exit;
} else {
$this->view->error = "系统错误,请稍候再试!";
echo $this->view->render('error.php');exit;
}
}
else //没有采用uc的登录方式
{
$dbAdapter = Zend_Registry::get('dbAdapter');
$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
//$authAdapter->setTableName('bg_user');
$authAdapter->setTableName(Zend_Registry::get('dbprefix').'user');
$authAdapter->setIdentityColumn('username');
$authAdapter->setCredentialColumn('password');
$authAdapter->setIdentity($username);
$authAdapter->setCredential($password);
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($authAdapter);
if($result->isValid()){
$data = $authAdapter->getResultRowObject(null,'password');
//$auth->getStorage()->write($data);
//写入cookie
$udata = array('id'=>$data->id,'username'=>$data->username,'role'=>$data->role);
$expire = 86400;
$this->setCookie($udata,$expire);
$this->view->securl = $this->view->domain;
$this->view->msg = '登录成功,正在跳转回首页!';
echo $this->view->render('success.php');
}else{
//用户名密码
$this->view->error = "用户名密码错误!";
echo $this->view->render('error.php');
exit();
}
}
好了,同步登录完成,接着就仿照上面的注销,修改密码,修改用户资料等都可以实现了。