整合 ucenter 注册自动激活

https://my.oschina.net/banbo/blog/311691

应用整合 UCenter,同步注册到 Discuz 的用户,在 Discuz 登录时得手动激活,用户体验很不好,不过解决方法很简单~
修改应用下的 ./uc_client/model/user.php 文件,大概在 138 行(UCenter 1.6)处的 function add_user 函数里添加代码:

 

function add_user($username, $password, $email, $uid = 0, $questionid = '', $answer = '', $regip = '') {
    $regip = empty($regip) ? $this->base->onlineip : $regip;
    $salt = substr(uniqid(rand()), -6);
    $password = md5(md5($password).$salt);
    $sqladd = $uid ? "uid='".intval($uid)."'," : '';
    $sqladd .= $questionid > 0 ? " secques='".$this->quescrypt($questionid, $answer)."'," : " secques='',";
    $this->db->query("INSERT INTO ".UC_DBTABLEPRE."members SET $sqladd username='$username', password='$password', email='$email', regip='$regip', regdate='".$this->base->time."', salt='$salt'");
    $uid = $this->db->insert_id();
    $this->db->query("INSERT INTO ".UC_DBTABLEPRE."memberfields SET uid='$uid'");
    // BEGIN
    $this->db->query("INSERT INTO ".DISCUZ_DBTABLEPRE."common_member SET uid='$uid', username='$username', password='$password', email='$email', adminid='0', groupid='10', regdate='".$this->base->time."', credits='0', timeoffset='9999'");
    $this->db->query("INSERT INTO ".DISCUZ_DBTABLEPRE."common_member_status SET uid='$uid', regip='$regip', lastip='$regip', lastvisit='".$this->base->time."', lastactivity='".$this->base->time."', lastpost='0', lastsendmail='0'");
    $this->db->query("INSERT INTO ".DISCUZ_DBTABLEPRE."common_member_profile SET uid='$uid'");
    $this->db->query("INSERT INTO ".DISCUZ_DBTABLEPRE."common_member_field_forum SET uid='$uid'");
    $this->db->query("INSERT INTO ".DISCUZ_DBTABLEPRE."common_member_field_home SET uid='$uid'");
    $this->db->query("INSERT INTO ".DISCUZ_DBTABLEPRE."common_member_count SET uid='$uid', extcredits1='0', extcredits2='0', extcredits3='0', extcredits4='0', extcredits5='0', extcredits6='0', extcredits7='0', extcredits8='0'");
    // END
    return $uid;
}

 

注意这些表都是 Discuz 的表,不是 UCenter 的表,不能使用 UC_DBTABLEPRE ! 我自己在 UCenter 配置文件 config.inc.php 中定义了 DISCUZ_DBTABLEPRE:

define('DISCUZ_DBTABLEPRE', '`ultrax`.pre_');
其中ultrax是数据库名称,根据情况修改

 

https://blog.51cto.com/wangyueheng88/1538565

 

多程序整合 UCenter,在新用户首次注册后是没有同步登录到其它程序的,也就是说用户得在其它程序用密码再登录一次,以后才能同步登录,还有就是用户不是在 Discuz 程序注册的话,在 Discuz 登录时得手动激活,这些都导致用户体验很不好,不过解决方法很简单~
修改各个程序目录下的 ./uc_client/model/user.php 文件,大概在 129 行处的 function add_user 函数里添加代码
如 Discuz X 的:

$this->db->query("INSERT INTO `dbname`.pre_common_member SET uid='$uid', username='$username', password='$password', email='$email', adminid='0', groupid='10', regdate='".$this->base->time."', credits='0', timeoffset='9999'");
$this->db->query("INSERT INTO `dbname`.pre_common_member_status SET uid='$uid', regip='$regip', lastip='$regip', lastvisit='".$this->base->time."', lastactivity='".$this->base->time."', lastpost='0', lastsendmail='0'");
$this->db->query("INSERT INTO `dbname`.pre_common_member_profile SET uid='$uid'");
$this->db->query("INSERT INTO `dbname`.pre_common_member_field_forum SET uid='$uid'");
$this->db->query("INSERT INTO `dbname`.pre_common_member_field_home SET uid='$uid'");
$this->db->query("INSERT INTO `dbname`.pre_common_member_count SET uid='$uid', extcredits1='0', extcredits2='0', extcredits3='0', extcredits4='0', extcredits5='0', extcredits6='0', extcredits7='0', extcredits8='0'");

Discuz 的参数比较多,`dbname` 是数据库名,.pre_ 是表前缀,按自己的情况修改,注意需要在 MySQL 设置相应的权限,假设 Discuz X 和 UCenter 是在不同的数据库且不同数据库用户,Discuz X 数据库和用户是 discuz,UCenter 的数据库和用户是 ucenter,那么需要设置 ucenter 拥有数据库 discuz 的 insert 权限。(如果是同一数据库、同一用户则忽略这些步骤)
phpMyAdmin 的操作步骤大概为:权限 -> 编辑权限 -> 按数据库指定权限 -> 选择数据库 -> 勾选 INSERT -> 执行。

这些代码的原理就是,在某应用注册用户时,同时添加其它应用的数据库字段,因为 UCenter 在首次注册时并没有这一步骤所以才没能同步登录与激活。

修改后完整的 function add_user 函数是这样的:

function add_user($username, $password, $email, $uid = 0, $questionid = '', $answer = '', $regip = '') {
    $regip = empty($regip) ? $this->base->onlineip : $regip;
    $salt = substr(uniqid(rand()), -6);
    $password = md5(md5($password).$salt);
    $sqladd = $uid ? "uid='".intval($uid)."'," : '';
    $sqladd .= $questionid > 0 ? " secques='".$this->quescrypt($questionid, $answer)."'," : " secques='',";
    $this->db->query("INSERT INTO ".UC_DBTABLEPRE."members SET $sqladd username='$username', password='$password', email='$email', regip='$regip', regdate='".$this->base->time."', salt='$salt'");
    $uid = $this->db->insert_id();
    $this->db->query("INSERT INTO ".UC_DBTABLEPRE."memberfields SET uid='$uid'");
    // BEGIN
    $this->db->query("INSERT INTO `dbname`.pre_common_member SET uid='$uid', username='$username', password='$password', email='$email', adminid='0', groupid='10', regdate='".$this->base->time."', credits='0', timeoffset='9999'");
    $this->db->query("INSERT INTO `dbname`.pre_common_member_status SET uid='$uid', regip='$regip', lastip='$regip', lastvisit='".$this->base->time."', lastactivity='".$this->base->time."', lastpost='0', lastsendmail='0'");
    $this->db->query("INSERT INTO `dbname`.pre_common_member_profile SET uid='$uid'");
    $this->db->query("INSERT INTO `dbname`.pre_common_member_field_forum SET uid='$uid'");
    $this->db->query("INSERT INTO `dbname`.pre_common_member_field_home SET uid='$uid'");
    $this->db->query("INSERT INTO `dbname`.pre_common_member_count SET uid='$uid', extcredits1='0', extcredits2='0', extcredits3='0', extcredits4='0', extcredits5='0', extcredits6='0', extcredits7='0', extcredits8='0'");
    // END
    return $uid;}

其它的程序就比较简单了,打开程序的数据库的用户表,对比一下就知道了,下面提供几个参考:

// ecmall
$this->db->query("INSERT INTO `ecmall`.ecm_member SET user_id='$uid', user_name='$username', email='$email', password='$password', gender='0', reg_time='".$this->base->time."', last_login='".$this->base->time."', last_ip='$regip', logins='1', ugrade='0', outer_id='0'");
// ecshop
$this->db->query("INSERT INTO `ecshop`.ecs_users SET user_id='$uid', email='$email', user_name='$username', password='$password', reg_time='".$this->base->time."', last_login='".$this->base->time."', last_ip='$regip'");
// thinksns
$this->db->query("INSERT INTO `thinksns`.ts_ucenter_user_link SET uid='$uid', uc_uid='$uid', uc_username='$username'");
$this->db->query("INSERT INTO `thinksns`.ts_user SET uid='$uid', email='$email', password='$password', uname='$username', is_active='1', is_init='1'");
$this->db->query("INSERT INTO `thinksns`.ts_user_online SET uid='$uid', ctime='".$this->base->time."'");

(需要说明的是部分程序的登录机制不同使得此方法的首次注册同步登录无法实现)

 

posted on 2020-03-02 01:55  yipianchuyun  阅读(297)  评论(0编辑  收藏  举报

导航