第三方從UC帳號免激活異步登陸DX 2.5 方法

最近恰好須要一個結合UC並使用了DX2.5的方案,發現DX2.5官方只提供容許登陸自動激活的功能( 這選項在DX管理中心->站長 -> UCENTER設置),等於經過UC註冊的帳號,仍必須在DX2.5登陸一次,才能自動激活帳號,這樣其它基於UC的其它應用,才能實現異步登陸。

如下方法解決第三方應用登陸,同時異步登陸DX2.5,,已在DX2.5+UC1.6+開發的第三方應用中測試經過。 php

api/uc.php
function synlogin($get, $post) {
                global $_G;

                if(!API_SYNLOGIN) {
                        return API_RETURN_FORBIDDEN;
                }

                header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');

                $cookietime = 31536000;
                $uid = intval($get['uid']);
                if(($member = getuserbyuid($uid, 1))) {
                        dsetcookie('auth', authcode("$member[password]\t$member[uid]", 'ENCODE'), $cookietime);
                }
}
改爲:
function synlogin($get, $post) {
        global $_G;

        if(!API_SYNLOGIN) {
                return API_RETURN_FORBIDDEN;
        }

        header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');

        $cookietime = 31536000;
        $uid = intval($get['uid']);
        if(($member = getuserbyuid($uid, 1))) {
                dsetcookie('auth', authcode("$member[password]\t$member[uid]", 'ENCODE'), $cookietime);
        }
/**如下代碼是新添加**/
        else{
                $init_arr = explode(',',  $_G['setting']['initcredits']);
                $groupid =  $_G['setting']['regverify'] ? 8 :  $_G['setting']['newusergroupid'];

                C::t('common_member')->insert($uid, $get['username'], md5(random(10)), $get['email'], $_G['clientip'], $groupid, $init_arr);
                C::t('common_member_status')->update($uid, array('lastip' => $_G['clientip'], 'lastvisit' =>TIMESTAMP, 'lastactivity' => TIMESTAMP));
                if(($member = getuserbyuid($uid, 1))) {
                        dsetcookie('auth', authcode("$member[password]\t$member[uid]", 'ENCODE'), $cookietime);
                }
        }
/**到這裏新增代碼結束**/
}
若是要符合DX2.5後臺的自動激活選項,要改爲這樣:
function synlogin($get, $post) {
                global $_G;

                if(!API_SYNLOGIN) {
                        return API_RETURN_FORBIDDEN;
                }

                header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');

                $cookietime = 31536000;
                $uid = intval($get['uid']);
                if(($member = getuserbyuid($uid, 1))) {
                        dsetcookie('auth', authcode("$member[password]\t$member[uid]", 'ENCODE'), $cookietime);
                }
                else{
                        if($_G['setting']['fastactivation']){
                                $init_arr = explode(',',  $_G['setting']['initcredits']);
                                $groupid =  $_G['setting']['regverify'] ? 8 :  $_G['setting']['newusergroupid'];

                                C::t('common_member')->insert($uid, $get['username'], md5(random(10)), $get['email'], $_G['clientip'], $groupid, $init_arr);
                                C::t('common_member_status')->update($uid, array('lastip' => $_G['clientip'], 'lastvisit' =>TIMESTAMP, 'lastactivity' => TIMESTAMP));
                                if(($member = getuserbyuid($uid, 1))) {
                                        dsetcookie('auth', authcode("$member[password]\t$member[uid]", 'ENCODE'), $cookietime);
                                }
                        }
                }
        }

只是簡單實現登陸 api

通過DX 2.5 測試上面之方法,發現用戶的郵箱只能寫入Ucenter並無同步到Discuz中,然今日找到解決之方法,共享給兄弟們 cookie

function synlogin($get, $post) {
	global $_G;
	if(!API_SYNLOGIN) {
		return API_RETURN_FORBIDDEN;
	}
	header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
	$cookietime = 31536000;
	$uid = intval($get['uid']);
	$query = DB::query("SELECT uid, username, password FROM ".DB::table('common_member')." WHERE uid='$uid'");
	if($member = DB::fetch($query)) {
		dsetcookie('auth', authcode("$member[password]\t$member[uid]", 'ENCODE'), $cookietime);
	}else{
		//用戶不存在  從UC裏取用戶
		$username = $get['username'];
		$query = DB::query("SELECT uid,username,password,email FROM ".DB::table('ucenter_members')." WHERE username='$username'");
		$member = DB::fetch($query);
		$password = $member['password'];
		$email = $member['email'];
		$ip = $_SERVER['REMOTE_ADDR'];
		$time = time();
		$userdata = array(
			'uid' => $uid,
			'username' => $username,
			'password' => $password,
			'email' => $email,
			'adminid' => 0,
			'groupid' => 10,
			'regdate' => $time,
			'credits' => 0,
			'timeoffset' => 9999
		);
		DB::insert('common_member', $userdata);
		
		$status_data = array(
			'uid' => $uid,
			'regip' => $ip,
			'lastip' => $ip,
			'lastvisit' => $time,
			'lastactivity' => $time,
			'lastpost' => 0,
			'lastsendmail' => 0,
		);
		DB::insert('common_member_status', $status_data);
		DB::insert('common_member_profile', array('uid' => $uid));
		DB::insert('common_member_field_forum', array('uid' => $uid));
		DB::insert('common_member_field_home', array('uid' => $uid));
		DB::insert('common_member_count', array('uid' => $uid));
		DB::query("UPDATE ".DB::table('common_setting')." SET svalue='$username' WHERE skey='lastmember'");
	
		$query = DB::query("SELECT uid, username, password FROM ".DB::table('common_member')." WHERE uid='$uid'");
		if($member = DB::fetch($query)) {
			dsetcookie('auth', authcode("$member[password]\t$member[uid]", 'ENCODE'), $cookietime);
		}
	}
}
相關文章
相關標籤/搜索