init: 项目初始代码
This commit is contained in:
115
extend/sso/SSOClient.php
Normal file
115
extend/sso/SSOClient.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
namespace sso;
|
||||
|
||||
use apiRes\Api;
|
||||
use think\facade\Session;
|
||||
|
||||
class SSOClient
|
||||
{
|
||||
private $sso_url = '';
|
||||
private $app_id = '';
|
||||
private $app_secret = '';
|
||||
private $public_ssl_key = '';
|
||||
private $private_ssl_key = '';
|
||||
|
||||
public function __construct($data)
|
||||
{
|
||||
// $this->sso_url = config('sso.sso_url');
|
||||
// if (config('sso.app_id')) $this->app_id = config('sso.app_id');
|
||||
// if (config('sso.app_secret')) $this->app_secret = config('sso.app_secret');
|
||||
// if (config('sso.public_key')) $this->public_ssl_key = config('sso.public_key');
|
||||
// if (config('sso.private_key')) $this->private_ssl_key = config('sso.private_key');
|
||||
|
||||
$this->app_id = $data['app_id'];
|
||||
$this->sso_url = $data['sso_url'];
|
||||
$this->app_secret = $data['app_secret'];
|
||||
$this->public_ssl_key = $data['public_key'];
|
||||
$this->private_ssl_key = $data['private_key'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验code,返回access_token
|
||||
*/
|
||||
public function getAccessToken($code)
|
||||
{
|
||||
if (empty($code)) {
|
||||
return Api::error('参数code不能为空', [], true);
|
||||
}
|
||||
$data = [
|
||||
'app_id' => $this->app_id,
|
||||
'app_secret' => $this->app_secret,
|
||||
'code' => $code
|
||||
];
|
||||
return $this->http('authorize/access_token', $data, true, ['cookie'=>'PHPSESSID=' . Session::getId()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新access_token
|
||||
*/
|
||||
public function refreshToken($refresh_token, $sessid='')
|
||||
{
|
||||
if (empty($refresh_token)) {
|
||||
return Api::error('参数refresh_token不能为空', [], true);
|
||||
}
|
||||
$data = [
|
||||
'app_id' => $this->app_id,
|
||||
'app_secret' => $this->app_secret,
|
||||
'refresh_token' => $refresh_token
|
||||
];
|
||||
return $this->http('authorize/refresh_token', $data, true, ['cookie'=>'PHPSESSID=' . $sessid]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通知sso退出登录
|
||||
*/
|
||||
public function notifySSOServerSigninOut($access_token, $sessid='')
|
||||
{
|
||||
if (empty($access_token)) {
|
||||
return Api::error('参数access_token不能为空', [], true);
|
||||
}
|
||||
$data = [
|
||||
'app_id' => $this->app_id,
|
||||
'app_secret' => $this->app_secret,
|
||||
'access_token' => $access_token,
|
||||
];
|
||||
if (empty($sessid)) {
|
||||
$sessid = Session::getId();
|
||||
}
|
||||
return $this->http('authorize/signin_out', $data, true, ['cookie'=>'PHPSESSID=' . $sessid]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求数据
|
||||
*/
|
||||
private function http($url, $data, $is_post=false, $options=[])
|
||||
{
|
||||
$data = json_encode($data);
|
||||
$headerArray = [
|
||||
'Content-type: application/json;charset=utf-8',
|
||||
'Accept: application/json'
|
||||
];
|
||||
$url = $this->sso_url . $url;
|
||||
$curl = curl_init();
|
||||
curl_setopt($curl, CURLOPT_URL, $url);
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
|
||||
if ($is_post) {
|
||||
curl_setopt($curl, CURLOPT_POST, 1);
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
|
||||
}
|
||||
curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
||||
if (!empty($options['cookie'])) {
|
||||
curl_setopt($curl, CURLOPT_COOKIE, $options['cookie']);
|
||||
}
|
||||
$output = curl_exec($curl);
|
||||
$http_status = curl_errno($curl);
|
||||
$http_msg = curl_error($curl);
|
||||
curl_close($curl);
|
||||
if ($http_status == 0) {
|
||||
return json_decode($output, true);
|
||||
} else {
|
||||
return ['errno' => $http_status, 'errmsg' => $http_msg, 'data' => []];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user