1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-07-13 03:49:11 +08:00
koogua dd99631f6e !33 开放登录阶段性合并
* Merge remote-tracking branch 'gitee/xiaochong0302/I280IZ' into xiaocho…
* 初步完成开放登录,待线上测试7
* Merge branch 'demo' of gitee.com:koogua/course-tencent-cloud into xiao…
* 初步完成开放登录,待线上测试6
* !30 开放登录线上测试5
* !29 开放登录线上测试5
* 初步完成开放登录,待线上测试5
* !28 开放登录线上测试4
* 初步完成开放登录,待线上测试4
* !27 开放登录线上测试3
* 初步完成开放登录,待线上测试3
* !26 开放登录线上测试2
* 初步完成开放登录,待线上测试2
* !25 开放登录线上测试
* 初步完成开放登录,待线上测试
* !22 验证更新h5支付
* Merge remote-tracking branch 'remotes/gitee/develop' into demo
* !20 验证更新h5支付
* Merge branch 'develop' of https://gitee.com/koogua/course-tencent-clou…
* !16 v1.2.0阶段性合并
* 删除调试断点代码
* 删除重复的signature方法
* Merge branch 'develop' of https://gitee.com/koogua/course-tencent-clou…
* demo后台增加统计
* !5 更新版本号1.1.0
* !4 v1.1.0版本develop->demo
* Merge branch 'develop' into demo
* 1.增加changelog.md
* Merge branch 'develop' into demo
* Merge branch 'develop' into demo
* Merge branch 'develop' into demo
* !1 精简优化代码
* Merge branch 'develop' into demo
* 合并修改
2020-12-07 11:02:13 +08:00

89 lines
2.3 KiB
PHP

<?php
namespace App\Services\OAuth;
use App\Services\OAuth;
class WeiBo extends OAuth
{
const AUTHORIZE_URL = 'https://api.weibo.com/oauth2/authorize';
const ACCESS_TOKEN_URL = 'https://api.weibo.com/oauth2/access_token';
const USER_INFO_URL = 'https://api.weibo.com/2/users/show.json';
public function getAuthorizeUrl()
{
$params = [
'client_id' => $this->clientId,
'redirect_uri' => $this->redirectUri,
'state' => $this->getState(),
'response_type' => 'code',
];
return self::AUTHORIZE_URL . '?' . http_build_query($params);
}
public function getAccessToken($code)
{
$params = [
'code' => $code,
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'redirect_uri' => $this->redirectUri,
'grant_type' => 'authorization_code',
];
$response = $this->httpPost(self::ACCESS_TOKEN_URL, $params);
$this->accessToken = $this->parseAccessToken($response);
return $this->accessToken;
}
public function getOpenId($accessToken = null)
{
return $this->openId;
}
public function getUserInfo($accessToken, $openId)
{
$params = [
'access_token' => $accessToken,
'uid' => $openId,
];
$response = $this->httpGet(self::USER_INFO_URL, $params);
return $this->parseUserInfo($response);
}
private function parseAccessToken($response)
{
$data = json_decode($response, true);
if (!isset($data['access_token']) || !isset($data['uid'])) {
throw new \Exception("Fetch Access Token Failed:{$response}");
}
$this->openId = $data['uid'];
return $data['access_token'];
}
private function parseUserInfo($response)
{
$data = json_decode($response, true);
if (isset($data['error_code']) && $data['error_code'] != 0) {
throw new \Exception("Fetch User Info Failed:{$response}");
}
$userInfo['id'] = $data['id'];
$userInfo['name'] = $data['name'];
$userInfo['avatar'] = $data['profile_image_url'];
return $userInfo;
}
}