admin管理员组

文章数量:1130349

1.在页面中准备一个跳转qq互联授权登陆页面的a标签或者在控制器中跳转/**

* 跳转登陆页面

*

* @return \think\response\Redirect

*/

public function index()

{

$my_url = 'http://www.kutucn/api';

$SESSION = md5((string)rand());

Session::set('state', $SESSION);

return redirect("https://graph.qq/oauth2.0/authorize?response_type=code&client_id=" . $this->appid . "&redirect_uri=" . urlencode($my_url) . "&state=" . $SESSION);

}

注释:

my-url 为你qq互联上面填写的回调域名

id 跟key为qq互联应用的参数

2.获取token/**

* 获取access_token

*

* @param $code

* @return array|false|string

*/

private function getToken($code)

{

function convertUrlQuery($query)

{

$queryParts = explode('&', $query);

$params = array();

foreach ($queryParts as $param) {

$item = explode('=', $param);

$params[$item[0]] = $item[1];

}

return $params;

}

$client_id = $this->client_id; // APPID

$client_secret = $this->client_secret; // APPSECRET

$url = urlencode('http://www.kutucn/api'); // 授权回调域

$data = file_get_contents("https://graph.qq/oauth2.0/token?grant_type=authorization_code&client_id=$client_id&client_secret=$client_secret&code=$code&redirect_uri=$url");

$data = convertUrlQuery($data);

return $data;

}

3. 用token换取openid 再获取用户info信息 再加上自己网站的登陆逻辑/**

* 获取token 网站登陆逻辑

*

* @param Request $request

* @param $code

* @param $state

* @return \think\response\Json|\think\response\Redirect

* @throws \think\db\exception\DataNotFoundException

* @throws \think\db\exception\DbException

* @throws \think\db\exception\ModelNotFoundException

* @Route("", method="GET")

*/

public function index(Request $request, $code, $state)

{

$client_id = $this->client_id;

$client_secret = $this->client_secret;

// 获取第一步的token

$data = $this->getToken($code);

$access_token = $data['access_token'];

$refresh_token = $data['refresh_token'];

// 获取30天期限的token

$result = file_get_contents("https://graph.qq/oauth2.0/me?access_token=$access_token");

$result = explode('(', $result);

$result = explode(')', $result[1])[0];

$result = json_decode($result, true);

if (key_exists('error', $result)) {

return $this->callJson($result, 2, '登陆失败');

} else {

// 获取用户信息

$openid = $result['openid'];

$info = file_get_contents("https://graph.qq/user/get_user_info?access_token=$access_token&oauth_consumer_key=$client_id&openid=$openid");

$info = json_decode($info, true);

$inset = [

'headerimg' => $info['figureurl_qq'],

'province' => 0,

'city' => 0,

'county' => 0,

'address' => '',

'nickname' => $info['nickname'],

'openid' => $openid,

'sex' => $info['gender'],

'create_time' => time(),

'is_auth' => 1,

'update_time' => time(),

'unionid' => 0,

'token' => $data['access_token'],

'auth' => 0,

'email' => '',

'phone' => 0,

'desc'=> '',

]; // 建立一个存储登陆信息的数组,以便存入用户数据表

}

$url = Cookie::get('url'); // 获取前端页面存储的登陆前页面url

if (empty($url)) {

return redirect($url);// 前端页面 写一个公共的js 用cookie存储当前的页面url,以便返回登陆前的页面

} else {

return redirect($_SERVER["REQUEST_SCHEME"] . '://' . $_SERVER['HTTP_HOST']); // 如果cookie不存在就返回主页

}

}

以上 qq登陆完成,测试可正常使用。

1.在页面中准备一个跳转qq互联授权登陆页面的a标签或者在控制器中跳转/**

* 跳转登陆页面

*

* @return \think\response\Redirect

*/

public function index()

{

$my_url = 'http://www.kutucn/api';

$SESSION = md5((string)rand());

Session::set('state', $SESSION);

return redirect("https://graph.qq/oauth2.0/authorize?response_type=code&client_id=" . $this->appid . "&redirect_uri=" . urlencode($my_url) . "&state=" . $SESSION);

}

注释:

my-url 为你qq互联上面填写的回调域名

id 跟key为qq互联应用的参数

2.获取token/**

* 获取access_token

*

* @param $code

* @return array|false|string

*/

private function getToken($code)

{

function convertUrlQuery($query)

{

$queryParts = explode('&', $query);

$params = array();

foreach ($queryParts as $param) {

$item = explode('=', $param);

$params[$item[0]] = $item[1];

}

return $params;

}

$client_id = $this->client_id; // APPID

$client_secret = $this->client_secret; // APPSECRET

$url = urlencode('http://www.kutucn/api'); // 授权回调域

$data = file_get_contents("https://graph.qq/oauth2.0/token?grant_type=authorization_code&client_id=$client_id&client_secret=$client_secret&code=$code&redirect_uri=$url");

$data = convertUrlQuery($data);

return $data;

}

3. 用token换取openid 再获取用户info信息 再加上自己网站的登陆逻辑/**

* 获取token 网站登陆逻辑

*

* @param Request $request

* @param $code

* @param $state

* @return \think\response\Json|\think\response\Redirect

* @throws \think\db\exception\DataNotFoundException

* @throws \think\db\exception\DbException

* @throws \think\db\exception\ModelNotFoundException

* @Route("", method="GET")

*/

public function index(Request $request, $code, $state)

{

$client_id = $this->client_id;

$client_secret = $this->client_secret;

// 获取第一步的token

$data = $this->getToken($code);

$access_token = $data['access_token'];

$refresh_token = $data['refresh_token'];

// 获取30天期限的token

$result = file_get_contents("https://graph.qq/oauth2.0/me?access_token=$access_token");

$result = explode('(', $result);

$result = explode(')', $result[1])[0];

$result = json_decode($result, true);

if (key_exists('error', $result)) {

return $this->callJson($result, 2, '登陆失败');

} else {

// 获取用户信息

$openid = $result['openid'];

$info = file_get_contents("https://graph.qq/user/get_user_info?access_token=$access_token&oauth_consumer_key=$client_id&openid=$openid");

$info = json_decode($info, true);

$inset = [

'headerimg' => $info['figureurl_qq'],

'province' => 0,

'city' => 0,

'county' => 0,

'address' => '',

'nickname' => $info['nickname'],

'openid' => $openid,

'sex' => $info['gender'],

'create_time' => time(),

'is_auth' => 1,

'update_time' => time(),

'unionid' => 0,

'token' => $data['access_token'],

'auth' => 0,

'email' => '',

'phone' => 0,

'desc'=> '',

]; // 建立一个存储登陆信息的数组,以便存入用户数据表

}

$url = Cookie::get('url'); // 获取前端页面存储的登陆前页面url

if (empty($url)) {

return redirect($url);// 前端页面 写一个公共的js 用cookie存储当前的页面url,以便返回登陆前的页面

} else {

return redirect($_SERVER["REQUEST_SCHEME"] . '://' . $_SERVER['HTTP_HOST']); // 如果cookie不存在就返回主页

}

}

以上 qq登陆完成,测试可正常使用。

本文标签: 互联第三方HTML5QQphpQQ