mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-06-22 03:32:47 +08:00
* 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 * 合并修改
108 lines
2.2 KiB
PHP
108 lines
2.2 KiB
PHP
<?php
|
||
|
||
namespace App\Services;
|
||
|
||
use GuzzleHttp\Client as HttpClient;
|
||
use Phalcon\Crypt;
|
||
use Phalcon\Di;
|
||
|
||
abstract class OAuth extends Service
|
||
{
|
||
|
||
/**
|
||
* @var string
|
||
*/
|
||
protected $clientId;
|
||
|
||
/**
|
||
* @var string
|
||
*/
|
||
protected $clientSecret;
|
||
|
||
/**
|
||
* @var string
|
||
*/
|
||
protected $redirectUri;
|
||
|
||
/**
|
||
* @var string
|
||
*/
|
||
protected $accessToken;
|
||
|
||
/**
|
||
* @var string
|
||
*/
|
||
protected $openId;
|
||
|
||
public function __construct($clientId, $clientSecret, $redirectUri)
|
||
{
|
||
$this->clientId = $clientId;
|
||
$this->clientSecret = $clientSecret;
|
||
$this->redirectUri = $redirectUri;
|
||
}
|
||
|
||
public function httpGet($uri, $params = [], $headers = [])
|
||
{
|
||
$client = new HttpClient();
|
||
|
||
$options = ['query' => $params, 'headers' => $headers];
|
||
|
||
$response = $client->get($uri, $options);
|
||
|
||
return $response->getBody();
|
||
}
|
||
|
||
public function httpPost($uri, $params = [], $headers = [])
|
||
{
|
||
$client = new HttpClient();
|
||
|
||
$options = ['query' => $params, 'headers' => $headers];
|
||
|
||
$response = $client->post($uri, $options);
|
||
|
||
return $response->getBody();
|
||
}
|
||
|
||
public function getState()
|
||
{
|
||
/**
|
||
* @var $crypt Crypt
|
||
*/
|
||
$crypt = Di::getDefault()->get('crypt');
|
||
|
||
return $crypt->encryptBase64(rand(1000, 9999));
|
||
}
|
||
|
||
public function checkState($state)
|
||
{
|
||
/**
|
||
* 注意事项:
|
||
* callback中的state参数并未做encode处理,参数中含有"+"
|
||
* 获取参数的时候却自动做了decode处理,"+"变成了空格
|
||
*/
|
||
$state = str_replace(' ', '+', $state);
|
||
|
||
/**
|
||
* @var $crypt Crypt
|
||
*/
|
||
$crypt = Di::getDefault()->get('crypt');
|
||
|
||
$value = $crypt->decryptBase64($state);
|
||
|
||
if ($value < 1000 || $value > 9999) {
|
||
throw new \Exception('Invalid OAuth State Value');
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
abstract public function getAuthorizeUrl();
|
||
|
||
abstract public function getAccessToken($code);
|
||
|
||
abstract public function getOpenId($accessToken);
|
||
|
||
abstract public function getUserInfo($accessToken, $openId);
|
||
|
||
}
|