你现在的位置:首页 > PHP网站建设知识库 > phpcms > 正文

phpcms v9整合腾讯开放平台QQ登陆功能实现

phpcms v9整合腾讯开放平台QQ登陆功能实现(必须修改v9_member里面的connectid为varchar(32).QQ返回的openid是32位的)

具体实现代码:
一、打开caches/configs/system.php加入qq_appid,qq_appkey,qq_callback这三个参数是QQ登陆的参数,必须到腾讯网站上申请。
QQ上面的回调地址设置为 网站域名即可
地址:connect#opensns#qq#com
二、在member/index.php里面加入

public function public_qq_login(){
$appid = pc_base::load_config('system', 'qq_appid');
$appkey = pc_base::load_config('system', 'qq_appkey');
$callback = pc_base::load_config('system', 'qq_callback');
pc_base::load_app_class('qqapi','',0);
$info = new qqapi($appid,$appkey,$callback);
$this->_session_start();
 
if(!isset($_GET['oauth_token'])){
$info->redirect_to_login();
}else{
$info->get_openid();
if(!empty($_SESSION['openid'])){
$r = $this->db->get_one(array('connectid'=>$_SESSION['openid'],'from'=>'qq'));
if(!empty($r)){
//登陆
$password = $r['password'];
$this->_init_phpsso();
$synloginstr = $this->client->ps_member_synlogin($r['phpssouid']);
$userid = $r['userid'];
$groupid = $r['groupid'];
$username = $r['username'];
$nickname = empty($r['nickname']) ? $username : $r['nickname'];
$this->db->update(array('lastip'=>ip(), 'lastdate'=>SYS_TIME, 'nickname'=>$me['name']), array('userid'=>$userid));
 
if(!$cookietime) $get_cookietime = param::get_cookie('cookietime');
$_cookietime = $cookietime ? intval($cookietime) : ($get_cookietime ? $get_cookietime : 0);
$cookietime = $_cookietime ? TIME + $_cookietime : 0;
 
$phpcms_auth_key = md5(pc_base::load_config('system', 'auth_key').$this->http_user_agent);
$phpcms_auth = sys_auth($userid."\t".$password, 'ENCODE', $phpcms_auth_key);
 
param::set_cookie('auth', $phpcms_auth, $cookietime);
param::set_cookie('_userid', $userid, $cookietime);
param::set_cookie('_username', $username, $cookietime);
param::set_cookie('_groupid', $groupid, $cookietime);
param::set_cookie('cookietime', $_cookietime, $cookietime);
param::set_cookie('_nickname', $nickname, $cookietime);
$forward = isset($_GET['forward']) && !empty($_GET['forward']) ? $_GET['forward'] : 'index.php?m=member&c=index';
showmessage(L('login_success').$synloginstr, $forward);
 
}else{
$user = $info->get_user_info();
$_SESSION['connectid'] = $_SESSION['openid'];
$_SESSION['from'] = 'qq';
$connect_username = $user['nickname'];
include template('member', 'connect');
}
}
}
}