first commit

This commit is contained in:
Mr.Qin
2022-08-19 19:48:37 +08:00
commit afdd648b65
3275 changed files with 631084 additions and 0 deletions

View File

@@ -0,0 +1,146 @@
<?php
declare (strict_types = 1);
namespace system\third;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
/**
* Gitee登录类
*/
class gitee
{
const GET_AUTH_CODE_URL = "https://gitee.com/oauth/authorize";
const GET_ACCESS_TOKEN_URL = "https://gitee.com/oauth/token";
const GET_USERINFO_URL = "https://gitee.com/api/v5/user";
/**
* 配置信息
* @var array
*/
private $config = [];
/**
* Http实例
* @var Object
*/
protected $http = null;
public function __construct($options = [])
{
if ($config = saenv('gitee')) {
$this->config = array_merge($this->config, $config);
}
$this->config = array_merge($this->config, is_array($options) ? $options : []);
$this->http = new Client();
}
/**
* 用户登录
*/
public function login() {
return redirect($this->getAuthorizeUrl());
}
/**
* 获取登录地址
*/
public function getAuthorizeUrl()
{
$state = hash('sha256',uniqid((string)mt_rand()));
session('state', $state);
$queryarr = array(
"response_type" => "code",
"client_id" => $this->config['app_id'],
"redirect_uri" => $this->config['callback'],
// "scope" => 'user_info',
"state" => $state,
);
request()->isMobile() && $queryarr['display'] = 'mobile';
$url = self::GET_AUTH_CODE_URL . '?' . http_build_query($queryarr);
return $url;
}
/**
* 获取用户信息
* @param array $params
* @return array
* @throws GuzzleException
*/
public function getUserInfo(array $params = []): array
{
$params = $params ? $params : input();
if ((isset($params['state']) && $params['state'] == session('state') && isset($params['code']))) {
// 获取access_token
$data = isset($params['code']) ? $this->getAccessToken($params['code']) : $params;
$access_token = isset($data['access_token']) ? $data['access_token'] : '';
$refresh_token = isset($data['refresh_token']) ? $data['refresh_token'] : '';
$expires_in = isset($data['expires_in']) ? $data['expires_in'] : 0;
if ($access_token) {
// 获取用户信息
$queryarr = [
"access_token" => $access_token,
];
$ret = $this->http->get(self::GET_USERINFO_URL.'?'.http_build_query($queryarr))->getBody()->getContents();
$userinfo = json_decode($ret, true);
if (!$userinfo || !is_array($userinfo)) {
return [];
}
$userinfo['avatar'] = isset($userinfo['avatar_url']) ? $userinfo['avatar_url'] : '';
$userinfo['avatar'] = str_replace('http://','https://',$userinfo['avatar']);
$data = [
'access_token' => $access_token,
'refresh_token' => $refresh_token,
'expires_in' => $expires_in,
'userinfo' => $userinfo,
'id' => $userinfo['id'],
];
return $data;
}
}
return [];
}
/**
* 获取access_token
* @param string $code
* @return array
*/
public function getAccessToken($code = '')
{
if (!$code) {
return [];
}
$queryarr = array(
"grant_type" => "authorization_code",
"client_id" => $this->config['app_id'],
"client_secret" => $this->config['app_key'],
"redirect_uri" => $this->config['callback'],
"code" => $code,
);
try {
$params = $this->http->post(self::GET_ACCESS_TOKEN_URL,['query'=>$queryarr])->getBody()->getContents();
} catch (\Throwable $th) {
if (strstr($th->getMessage(),'error_description')) {
throw new \Exception('登录已过期,请重新登录');
}
}
return $params ? json_decode($params,true): [];
}
}

154
extend/system/third/qq.php Normal file
View File

@@ -0,0 +1,154 @@
<?php
declare (strict_types = 1);
namespace system\third;
use GuzzleHttp\Client;
/**
* QQ登录类
*/
class qq
{
const GET_AUTH_CODE_URL = "https://graph.qq.com/oauth2.0/authorize";
const GET_ACCESS_TOKEN_URL = "https://graph.qq.com/oauth2.0/token";
const GET_USERINFO_URL = "https://graph.qq.com/user/get_user_info";
const GET_OPENID_URL = "https://graph.qq.com/oauth2.0/me";
/**
* 配置信息
* @var array
*/
private $config = [];
/**
* Http实例
* @var Object
*/
protected $http = null;
public function __construct($options = [])
{
if ($config = saenv('qq')) {
$this->config = array_merge($this->config, $config);
}
$this->config = array_merge($this->config, is_array($options) ? $options : []);
$this->http = new Client();
}
/**
* 用户登录
*/
public function login() {
return redirect($this->getAuthorizeUrl());
}
/**
* 获取登录地址
*/
public function getAuthorizeUrl()
{
$state = hash('sha256',uniqid((string)mt_rand()));
session('state', $state);
$queryarr = array(
"response_type" => "code",
"client_id" => $this->config['app_id'],
"redirect_uri" => $this->config['callback'],
"scope" => 'get_user_info',
"state" => $state,
);
request()->isMobile() && $queryarr['display'] = 'mobile';
$url = self::GET_AUTH_CODE_URL . '?' . http_build_query($queryarr);
return $url;
}
/**
* 获取用户信息
* @param array $params
* @return array
*/
public function getUserInfo($params = [])
{
$params = $params ? $params : input();
if (isset($params['access_token']) || (isset($params['state']) && $params['state'] == session('state') && isset($params['code']))) {
//获取access_token
$data = isset($params['code']) ? $this->getAccessToken($params['code']) : $params;
$access_token = isset($data['access_token']) ? $data['access_token'] : '';
$refresh_token = isset($data['refresh_token']) ? $data['refresh_token'] : '';
$expires_in = isset($data['expires_in']) ? $data['expires_in'] : 0;
if ($access_token) {
$openid = $this->getOpenId($access_token);
//获取用户信息
$queryarr = [
"access_token" => $access_token,
"oauth_consumer_key" => $this->config['app_id'],
"openid" => $openid,
];
$ret = $this->http->get(self::GET_USERINFO_URL.'?'.http_build_query($queryarr))->getBody()->getContents();
$userinfo = (array)json_decode($ret, true);
if (!$userinfo || !isset($userinfo['ret']) || $userinfo['ret'] !== 0) {
return [];
}
$userinfo = $userinfo ? $userinfo : [];
$userinfo['avatar'] = isset($userinfo['figureurl_qq_2']) ? $userinfo['figureurl_qq_2'] : '';
$userinfo['avatar'] = str_replace('http://','https://',$userinfo['avatar']);
$data = [
'access_token' => $access_token,
'refresh_token' => $refresh_token,
'expires_in' => $expires_in,
'openid' => $openid,
'userinfo' => $userinfo
];
return $data;
}
}
return [];
}
/**
* 获取access_token
* @param string $code
* @return array
*/
public function getAccessToken($code = '')
{
if (!$code) {
return [];
}
$queryarr = array(
"grant_type" => "authorization_code",
"client_id" => $this->config['app_id'],
"client_secret" => $this->config['app_key'],
"redirect_uri" => $this->config['callback'],
"code" => $code,
);
$ret = $this->http->get(self::GET_ACCESS_TOKEN_URL.'?'.http_build_query($queryarr))->getBody()->getContents();
$params = [];
parse_str($ret, $params);
return $params ? $params : [];
}
/**
* 获取open_id
* @param string $access_token
* @return string
*/
private function getOpenId($access_token = '')
{
$response = $this->http->get(self::GET_OPENID_URL.'?access_token='.$access_token)->getBody()->getContents();
if (strpos($response, "callback") !== false) {
$lpos = strpos($response, "(");
$rpos = strrpos($response, ")");
$response = substr($response, $lpos + 1, $rpos - $lpos - 1);
}
$user = (array)json_decode($response, true);
return isset($user['openid']) ? $user['openid'] : '';
}
}

View File

@@ -0,0 +1,133 @@
<?php
declare (strict_types = 1);
namespace system\third;
use GuzzleHttp\Client;
/**
* 微博登录类
*/
class weibo
{
const GET_AUTH_CODE_URL = "https://api.weibo.com/oauth2/authorize";
const GET_ACCESS_TOKEN_URL = "https://api.weibo.com/oauth2/access_token";
const GET_USERINFO_URL = "https://api.weibo.com/2/users/show.json";
/**
* 配置信息
* @var array
*/
private $config = [];
/**
* Http实例
* @var Object
*/
protected $http = null;
public function __construct($options = [])
{
if ($config = saenv('weibo')) {
$this->config = array_merge($this->config, $config);
}
$this->config = array_merge($this->config, is_array($options) ? $options : []);
$this->http = new Client();
}
/**
* 用户登录
*/
public function login() {
return redirect($this->getAuthorizeUrl());
}
/**
* 获取登录地址
*/
public function getAuthorizeUrl()
{
$state = hash('sha256',uniqid((string)mt_rand()));
session('state', $state);
$queryarr = array(
"response_type" => "code",
"client_id" => $this->config['app_id'],
"redirect_uri" => $this->config['callback'],
"state" => $state,
);
request()->isMobile() && $queryarr['display'] = 'mobile';
$url = self::GET_AUTH_CODE_URL . '?' . http_build_query($queryarr);
return $url;
}
/**
* 获取用户信息
* @param array $params
* @return array
*/
public function getUserInfo($params = [])
{
$params = $params ? $params : input();
if (isset($params['access_token']) || (isset($params['state']) && $params['state'] == session('state') && isset($params['code']))) {
//获取access_token
$data = isset($params['code']) ? $this->getAccessToken($params['code']) : $params;
$access_token = isset($data['access_token']) ? $data['access_token'] : '';
$refresh_token = isset($data['refresh_token']) ? $data['refresh_token'] : '';
$expires_in = isset($data['expires_in']) ? $data['expires_in'] : 0;
if ($access_token) {
$uid = isset($data['uid']) ? $data['uid'] : '';
//获取用户信息
$queryarr = [
"access_token" => $access_token,
"uid" => $uid,
];
$ret = $this->http->get(self::GET_USERINFO_URL.'?'.http_build_query($queryarr))->getBody()->getContents();
$userinfo = (array)json_decode($ret, true);
if (!$userinfo || isset($userinfo['error_code'])) {
return [];
}
$userinfo = $userinfo ? $userinfo : [];
$userinfo['nickname'] = isset($userinfo['screen_name']) ? $userinfo['screen_name'] : '';
$userinfo['avatar'] = isset($userinfo['profile_image_url']) ? $userinfo['profile_image_url'] : '';
$userinfo['avatar'] = str_replace('http://','https://',$userinfo['avatar']);
$data = [
'access_token' => $access_token,
'refresh_token' => $refresh_token,
'expires_in' => $expires_in,
'openid' => $uid,
'userinfo' => $userinfo
];
return $data;
}
}
return [];
}
/**
* 获取access_token
* @param string code
* @return array
*/
public function getAccessToken($code = '')
{
if (!$code) {
return [];
}
$queryarr = array(
"grant_type" => "authorization_code",
"client_id" => $this->config['app_id'],
"client_secret" => $this->config['app_key'],
"redirect_uri" => $this->config['callback'],
"code" => $code,
);
$response = $this->http->post(self::GET_ACCESS_TOKEN_URL,['query'=>$queryarr])->getBody()->getContents();
$ret = (array)json_decode($response, true);
return $ret ? $ret : [];
}
}

View File

@@ -0,0 +1,143 @@
<?php
declare (strict_types = 1);
namespace system\third;
use GuzzleHttp\Client;
/**
* 微信登录类
*/
class weixin
{
const GET_AUTH_CODE_URL = "https://open.weixin.qq.com/connect/qrconnect";
const GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token";
const GET_USERINFO_URL = "https://api.weixin.qq.com/sns/userinfo";
/**
* 配置信息
* @var array
*/
private $config = [];
/**
* Http实例
* @var Object
*/
protected $http = null;
public function __construct($options = [])
{
if ($config = saenv('weixin')) {
$this->config = array_merge($this->config, $config);
}
$this->config = array_merge($this->config, is_array($options) ? $options : []);
$this->http = new Client();
}
/**
* 用户登录
*/
public function login() {
return redirect($this->getAuthorizeUrl());
}
/**
* 获取登录地址
*/
public function getAuthorizeUrl()
{
$state = hash('sha256',uniqid((string)mt_rand()));
session('state', $state);
$queryarr = array(
"response_type" => "code",
"appid" => $this->config['app_id'],
"redirect_uri" => $this->config['callback'],
"scope" => 'snsapi_login,',
"state" => $state,
);
request()->isMobile() && $queryarr['display'] = 'mobile';
$url = self::GET_AUTH_CODE_URL . '?' . http_build_query($queryarr);
return $url;
}
/**
* 获取用户信息
* @param array $params
* @return array
*/
public function getUserInfo($params = [])
{
$params = $params ? $params : input();
if (isset($params['access_token']) || (isset($params['state']) && $params['state'] == session('state') && isset($params['code']))) {
//获取access_token
$data = isset($params['code']) ? $this->getAccessToken($params['code']) : $params;
$access_token = isset($data['access_token']) ? $data['access_token'] : '';
$refresh_token = isset($data['refresh_token']) ? $data['refresh_token'] : '';
$expires_in = isset($data['expires_in']) ? $data['expires_in'] : 0;
if ($access_token) {
$openid = isset($data['openid']) ? $data['openid'] : '';
$unionid = isset($data['unionid']) ? $data['unionid'] : '';
if (stripos($data['scope'], 'snsapi_login') !== false) {
//获取用户信息
$queryarr = [
"access_token" => $access_token,
"openid" => $openid,
"lang" => 'zh_CN'
];
$ret = $this->http->get(self::GET_USERINFO_URL.'?'.http_build_query($queryarr))->getBody()->getContents();
$userinfo = (array)json_decode($ret, true);
if (!$userinfo || isset($userinfo['errcode'])) {
return [];
}
$userinfo = $userinfo ? $userinfo : [];
$userinfo['avatar'] = isset($userinfo['headimgurl']) ? $userinfo['headimgurl'] : '';
$userinfo['avatar'] = str_replace('http://','https://',$userinfo['avatar']);
} else {
$userinfo = [];
}
$data = [
'access_token' => $access_token,
'refresh_token' => $refresh_token,
'expires_in' => $expires_in,
'openid' => $openid,
'unionid' => $unionid,
'userinfo' => $userinfo
];
return $data;
}
}
return [];
}
/**
* 获取access_token
* @param string code
* @return array
*/
public function getAccessToken($code = '')
{
if (!$code) {
return [];
}
$queryarr = array(
"grant_type" => "authorization_code",
"appid" => $this->config['app_id'],
"secret" => $this->config['app_key'],
"code" => $code,
);
$response = $this->http->get(self::GET_ACCESS_TOKEN_URL.'?'.http_build_query($queryarr))->getBody()->getContents();
$ret = (array)json_decode($response, true);
return $ret ? $ret : [];
}
}