You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.0 KiB
76 lines
2.0 KiB
<?php
|
|
|
|
namespace App\Utils;
|
|
|
|
use GuzzleHttp\Client;
|
|
use GuzzleHttp\Exception\GuzzleException;
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
|
trait ProxyHelpers
|
|
{
|
|
/**
|
|
* 获取token
|
|
* @param string $username
|
|
* @param string $password
|
|
* @param string $module 模块
|
|
* @param string $config 配置
|
|
* @param string $guard 登陆用户组
|
|
* @return boolean|mixed
|
|
* @throws GuzzleException
|
|
*/
|
|
public function authenticate(string $username, string $password, string $module, string $config, string $guard = ''): mixed
|
|
{
|
|
$client = new Client();
|
|
|
|
try {
|
|
$url = request()->root() . '/'. $module. '/oauth/token';
|
|
|
|
$params = array_merge(config('passport.'. $config), [
|
|
'username' => $username,
|
|
'password' => $password,
|
|
'provider' => $guard
|
|
]);
|
|
|
|
$respond = $client->post($url, ['form_params' => $params]);
|
|
|
|
} catch (RequestException $exception) {
|
|
return false;
|
|
}
|
|
|
|
if ($respond->getStatusCode() === 401) {
|
|
return false;
|
|
}
|
|
|
|
return json_decode($respond->getBody()->getContents(), true);
|
|
}
|
|
|
|
/**
|
|
* 获取刷新token
|
|
* @param string $module 模块
|
|
* @param string $config 配置
|
|
* @return boolean|mixed
|
|
* @throws GuzzleException
|
|
*/
|
|
public function getRefreshToken(string $module, string $config): mixed
|
|
{
|
|
$client = new Client();
|
|
|
|
try {
|
|
$url = request()->root() . '/'. $module. '/oauth/token';
|
|
|
|
$params = array_merge(config('passport.'. $config), [
|
|
'refresh_token' => request('refresh_token'),
|
|
]);
|
|
|
|
$response = $client->post($url, ['form_params' => $params]);
|
|
} catch (RequestException $exception) {
|
|
return false;
|
|
}
|
|
|
|
if ($response->getStatusCode() === 401) {
|
|
return false;
|
|
}
|
|
|
|
return json_decode($response->getBody()->getContents(), true);
|
|
}
|
|
}
|
|
|