1.到QQ互联上申请APP ID、APP KEY。地址:http://connect.qq.com/ 详细注方法这里不再赘述。
需要注意可以写多个这里的回调地址需要和你配置文件中的一致:http://www.***.com/Login/qq_login_cb;http://www.***.com/Index/qq_bc
3.QQHelper.class.php这个文件请放到ThinkPhp核心报中 路径为:ThinkphpExtendLibraryORGNetQQHelper.class.php,此文件为QQ登录的核心类。
4.配置一下配置文件如下:
//网站url路径
define ( 'WEBSITE_URL', 'http://www.***.com' );
//应用ID 这个是在 http://connect.qq.com/ 申请
define ( 'APP_ID', ********* );
//应用KEY 这个是在 http://connect.qq.com/ 申请
define ( 'APP_KEY', '****9676d9bec3bf0f0d53ff4ff6****' );
//QQ登录回调地址
define ( 'APP_LOGIN_CPATH', WEBSITE_URL . '/Login/qq_login_cb' );//这里的路径要和QQ互联上的回调接口一致
//绑定QQ回调地址
define ( 'APP_BIND_CPATH', WEBSITE_URL . '/Index/qq_bc' );//这里的路径要和QQ互联上的回调接口一致
//请求用户授权时向用户显示的可进行授权的列表
define ( 'APP_SCOPE', 'get_user_info,add_share,list_album,add_album,upload_pic,add_topic,add_one_blog,add_weibo' );
//图片是否本地化
define ( 'AVATAR_LOCAL', false );
//Session设置
define ( 'SES_TOKEN_NAME', 'qq_token' ); //token名称
define ( 'SES_OPENID_NAME', 'qq_openid' ); //QQ用户的openid名称
define ( 'SES_STATE_NAME', 'qq_state' );// 状态名称
5.php代码
1.commonAction.class.php代码
class CommonAction extends Action
{
public function _initialize() {
header("Content-Type:text/html; charset=utf-8");
header('Content-Type:application/json; charset=utf-8');
$systemConfig = include WEB_ROOT . 'Common/systemConfig.php';
$this->assign("site", $systemConfig);
if(empty($_SESSION['info']['username']))
{
if(empty($_SESSION['qq_openid']))
{
$L="<li><a href=".$systemConfig['WEB_ROOT']."Register/index>注册</a></li>";
$L.="<li><a href='javascript:void(0);' id='login'>登录</a></li>";
$L.="<li><a href='".$systemConfig['WEB_ROOT']."Login/qq_login'><img src='".$systemConfig['WEB_ROOT']."/Public/home/images/qq_login.png'/></a></li>";
}else{
if($_SESSION['info']['bind']=="N")
{
$L="<li><a href='".$systemConfig['WEB_ROOT']."Login/loginout'>退出</a></li>";
$L.="<li><a href='".$systemConfig['WEB_ROOT']."Center/index'>会员中心</a></li>";
$L.="<li><a href='".$systemConfig['WEB_ROOT']."Index/readtemplate?source=bind_uname'>请绑定用户名</a></li>";
$L.="<li>".$_SESSION['info']['nickname']."</li>";
$L.="<li><img src='".$_SESSION['info']['avatar']."' style='width:30px;'/></li>";
}else{
$L="<li><a href='".$systemConfig['WEB_ROOT']."Login/loginout'>退出</a></li>";
$L.="<li><a href='".$systemConfig['WEB_ROOT']."Center/index'>会员中心</a></li>";
//$L.="<li><a href='/Index/readtemplate?source=bind_uname'>请绑定用户名</a></li>";
$L.="<li>".$_SESSION['info']['nickname']."</li>";
$L.="<li><img src='".$_SESSION['info']['avatar']."' style='width:30px;'/></li>";
}
}
}else{
$L="<li><a href='".$systemConfig['WEB_ROOT']."Login/loginout'>退出</a></li>";
if(empty($_SESSION['info']['open_id']))
{
$L.="<li><a href='".$systemConfig['WEB_ROOT']."Index/qq_bind'>绑定QQ号</a></li>";
}else{
$L.="<li><a href=".$systemConfig['WEB_ROOT']."Index/qq_unbind'>解绑QQ号</a></li>";
}
$L.="<li><a href='".$systemConfig['WEB_ROOT']."Center/index'>会员中心</a></li>";
$L.="<li>您好:".$_SESSION['info']['username']."</li>";
}
$this->assign("L", $L);
}
}
2.实现QQ登录代码 LoginAction 都需继承 CommonAction
class LoginAction extends CommonAction {
private $user;
private $qqHelper;
function _initialize() {
$this->user = M('Member');
import ('ORG.Net.QQHelper');
$this->qqHelper = new QQHelper ();
}
//QQ登录
public function qq_login()
{
$this->qqHelper->login ( APP_ID, APP_SCOPE, APP_LOGIN_CPATH );
}
//QQ登录的回调
function qq_login_cb() {
$this->qqHelper->callback ( APP_LOGIN_CPATH );
$this->qqHelper->get_openid ();
//先去数据库中查找openid存在不
$temp_data = $this->user->where(array('open_id' => $_SESSION [SES_OPENID_NAME]))->find();
if (empty($temp_data['open_id'])) {
//证明该QQ还没有在本站登录过
$userInfo = json_decode ( $this->qqHelper->get_user_info (), true );
$data ['open_id'] = $_SESSION [SES_OPENID_NAME];
$data ['nickname'] = $userInfo ['nickname'];
$data ['avatar'] = $userInfo ['figureurl_2'];
$data ['login_ip'] = get_client_ip();
$data ['login_time'] = time();
$data ['reg_date'] = time();
$this->user->add ( $data );
//将信息写入session
$temp_data = $this->user->where(array('uid' => $this->user->getLastInsID()))->find();
$_SESSION['info']=$temp_data;
} else {
$_SESSION['info']=$temp_data;
}
$this->redirect ('Index/index');
}
//退出
public function loginout()
{
unset($_SESSION['info']);
session_destroy();
$this->redirect('Index/index');
}
}
3.QQ号与本地用户绑定 IndexAction 都需继承 CommonAction
class IndexAction extends CommonAction {
private $user;
private $qqHelper;
function _initialize() {
parent::_initialize();
$this->user = M ( 'Member' );
import ('ORG.Net.QQHelper');
$this->qqHelper = new QQHelper ();
}
/**QQ登录**/
//绑定用户名 指采用QQ登录的用户,还没有本站帐号和密码
function bind_uname() {
$data ['username'] = $_POST ['uname'];
$data ['pwd'] = MD5(MD5($_POST ['pwd']));
$data ['bind'] = 'Y';
$map ['open_id'] = $_SESSION [SES_OPENID_NAME]; //QQ的openid
//查找该openid是否已经绑定过用户名没有
$stat = $this->user->where ( $map )->find ();
if ($stat ['username'] != '' || $stat ['username'] != null) {
$this->error ( '你已经绑定过用户名,请不要再绑定啦' );
}
$res = $this->user->where ( $map )->save ( $data );
//更新session
$_SESSION['info']['bind']='Y';
if ($res) {
$this->success ( '绑定用户成功');
} else {
$this->error ( '绑定失败' );
}
}
//绑定QQ号 主要是通过本站注册的用户
function qq_bind() {
//先去判断该帐号是否绑定过QQ号
$map ['open_id'] = $_SESSION [SES_OPENID_NAME];
$res = $this->user->where ( $map )->find ();
if ($res != null) {
$this->error ( '您已经绑定了QQ帐号了,如果要更改请先解除绑定!' );
}
$this->qqHelper->login ( APP_ID, APP_SCOPE, APP_BIND_CPATH ); //注意这里的回调地址和登录的回调地址不同
}
//绑定QQ号 的回调地址
function qq_bc() {
$tmp_ses = $_SESSION [SES_OPENID_NAME]; //保存之前的Sesion 避免Session被覆盖
$this->qqHelper->callback ( APP_BIND_CPATH ); //注意这里的回调地址和登录的回调地址不同
$this->qqHelper->get_openid ();
$userInfo = json_decode ( $this->qqHelper->get_user_info (), true );
//先要去判断这个QQ号是否已经绑定过帐号没有
$res = $this->user->where ( array ('open_id' => $_SESSION [SES_OPENID_NAME] ) )->find ();
if ($res != null) {
$_SESSION [SES_OPENID_NAME] = $tmp_ses;
$this->error ( '该QQ号已经绑定过帐号了,请勿再次绑定', /esoxue.php );
}
$map['uid'] = $_SESSION['info']['uid'];
$data ['open_id'] = $_SESSION [SES_OPENID_NAME];
$data ['avatar'] = $userInfo ['figureurl_2'];
$data ['bind'] = 'Y';
$stat = $this->user->where ( $map )->save ( $data );
if ($stat > 0) {
$this->success ( '绑定QQ号成功!', /esoxue.php );
} else {
$this->error ( '绑定QQ失败', /esoxue.php );
}
}
//解绑QQ号
function qq_unbind() {
$map ['uid'] = $_SESSION['info']['uid'];
$data ['open_id'] = '';
$res = $this->user->where ( $map )->save ( $data );
if ($res > 0) {
$this->success ( '解绑成功!', /esoxue.php );
} else {
$this->error ( '解绑失败', /esoxue.php );
}
}
//读模板
function readtemplate() {
$dotpl = $_GET ['source'];
if ($dotpl == 'bind_uname') {
$this->display ( 'bind_uname' );
}
}
/**QQ登录**/
}
在header中输出{$L} header 是包含文件
6.数据库在您的会员中添加如下字段:
nickname、avatar、open_id、bind
CREATE TABLE IF NOT EXISTS `lw_member` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`nickname` varchar(20) DEFAULT NULL COMMENT '用户昵称',
`pwd` char(32) DEFAULT NULL COMMENT '密码',
`avatar` varchar(100) DEFAULT NULL COMMENT '用户头像',
`login_ip` varchar(15) DEFAULT NULL COMMENT '登录ip',
`login_time` int(17) DEFAULT NULL COMMENT '登录时间',
`username` varchar(50) NOT NULL COMMENT '用户名',
`open_id` varchar(32) NOT NULL,
`bind` enum('N','Y') NOT NULL DEFAULT 'N',
PRIMARY KEY (`uid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='网站前台会员表' AUTO_INCREMENT=6 ;
绑定本地用户模板代码:
<form action="{:U('Index/bind_uname')}" method="POST">
<p style="margin:0;text-align:right;margin-right:10px;cursor: pointer;" ></p>
<h1>绑定用户</h1>
<p> </p>
<p><input type="text" name="uname" class="input" placeholder="请输入用户名"/></p>
<p> </p>
<p><input type="password" name="pwd" class="input" placeholder="请输入密码"/></p>
<p> </p>
<p><input type="submit" name="submit" value="绑 定" class="submit"/></p>
</form>
临时演示地址:http://sheng.weilingdao.com
以上所有代码可根据实际情况更改。需要注意:QQ登录需要CURL的支持