1.下载官方的API(保留其中的API集成文件)
2.html页面(<a href='<?=Url::toRoute('/user/qqoauth')?>'></a>);
3.控制器:
public function actionQqoauth(){
require_once("frontend/web/qqAPI/qqConnectAPI.php");
$qc = new \QC();
$qc->qq_login();
}
4.回调页处理函数
//qq授权登录
if(yii::$app->request->get('state')!="" || Yii::$app->session['qqlogin']=="qq"){
Yii::$app->session['qqlogin'] = 'qq';
require_once("frontend/web/qqAPI/qqConnectAPI.php");
$qc = new \QC();
if(Yii::$app->session['access']){
$access=Yii::$app->session['access'];
$openid=Yii::$app->session['openid'];
}
else{
$access=$qc->qq_callback();
$openid=$qc->get_openid();
Yii::$app->session['access']=$access;
Yii::$app->session['openid']=$openid;
}
$this->qqlogin($access,$openid);
}
//qq授权登录方法处理
public function qqlogin($access,$openid){
$qc2 = new \QC($access,$openid);
$qquserinfo=$qc2->get_user_info();
$user_model=new AdminMember();
$userinfo=AdminMember::find()->where("openid='$openid'")->one();
if($userinfo){
$userinfo->logdates=time();
$userinfo->logip=CommonFun::getClientIp();
$userinfo->head_img=$qquserinfo['figureurl_qq_2'];
$userinfo->nickname=$qquserinfo['nickname'];
$userinfo->sex = $qquserinfo['gender']=="男"?0:1;
if($userinfo->save()){
Yii::$app->session['islogin'] = 'true';
Yii::$app->session['userid'] = $userinfo->id;
Yii::$app->session['username'] = $userinfo->usersname;
Yii::$app->session['nickname'] = $userinfo->nickname;
}
else{
var_dump($userinfo->getErrors());
exit();
}
}
else{
$user_model->nickname=$qquserinfo['nickname'];
$user_model->sex = $qquserinfo['gender']=="男"?0:1;
$user_model->head_img=$qquserinfo['figureurl_qq_2'];
$code = $user_model->Random_code();
$user_model->vatation_code=$code;
$user_model->dates=time();
$user_model->openid=$openid;
if($user_model->save()){
Yii::$app->session['islogin'] = 'true';
Yii::$app->session['userid'] = $user_model->id;
Yii::$app->session['username'] = $user_model->usersname;
Yii::$app->session['nickname'] = $user_model->nickname;
}
}
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有