phpcms V9实现qq登陆oauth2.0的方法

phpcms教程 强烈建议 2024-03-02 10:45 1820 0

  //QQ登陆oAuth2.0

  ublic function public_qq_login2(){

  $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('qqOAuth2','',0);

  $info=newqqOAuth2($appid,$appkey,$callback);

  $this->_session_start();

  if(!isset($_GET['code'])){

  $info->redirect_to_login();

  }else{

  $code=$_GET['code'];

  $_SESSION['openid']=$info->get_openid($code);//调取QQopenid值

  if(!emptyempty($_SESSION['openid'])){

  $r=$this->db->get_one(array('connectid'=>$_SESSION['openid'],'from'=>'qq'));

  if(!emptyempty($r)){

  //QQ已存在于数据库,则直接转向登陆操作

  $password=$r['password'];

  $this->_init_phpsso();

  $synloginstr=$this->client->ps_member_synlogin($r['phpssouid']);

  $userid=$r['userid'];

  $groupid=$r['groupid'];

  $username=$r['username'];

  $nickname=emptyempty($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." ".$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'])&&!emptyempty($_GET['forward'])?$_GET['forward']:'index.php?m=member&c=index';

  showmessage(L('login_success').$synloginstr,$forward);

  }else{

  //未存在于数据库中,跳去完善资料页面。页面预置用户名(QQ返回是UTF8编码,如有需要进行转码)

  $user=$info->get_user_info();

  $_SESSION['connectid']=$_SESSION['openid'];

  $_SESSION['from']='qq';

  if(CHARSET!='utf-8'){//转编码

  

$connect_username=iconv('utf-8',CHARSET,$user['nickname']);

  }

  includetemplate('member','connect');

  }}}

  }

本站资源均来自互联网或会员发布,如果不小心侵犯了您的权益请与我们联系。我们将立即删除!谢谢!