新版本

This commit is contained in:
候学杰 2019-01-22 18:59:03 +08:00
parent 04dbc7ab65
commit 6bc02585ad
7 changed files with 1092 additions and 105 deletions

View File

@ -3,16 +3,22 @@
"authors": [ "authors": [
{ {
"name": "候学杰", "name": "候学杰",
"email": "houxuejie@xpai.tv" "email": "houxuejie@hawumc.com.tv"
} }
], ],
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"PdAuth\\": "src/PdAuth" "PdAuth\\": "src/"
} }
}, },
"require": { "require": {
"illuminate/support": "^5.5", "illuminate/support": "^5.5",
"guzzlehttp/guzzle": "^6.3" "paidian/json-rpc": "dev-master"
},
"repositories": {
"php-json-rpc": {
"type": "vcs",
"url": "git@git.int.haowumc.com:composer/php-json-rpc.git"
}
} }
} }

1013
composer.lock generated

File diff suppressed because it is too large Load Diff

14
config/auth.php Normal file
View File

@ -0,0 +1,14 @@
<?php
return [
/**
* 支持的应用配置
*/
'apps' => [
'erp' => [
'id' => '100002',
'secret' => '123456',
],
],
];

141
src/Auth.php Normal file
View File

@ -0,0 +1,141 @@
<?php
namespace PdAuth;
use JsonRpc\Client;
use PdAuth\Middleware\Authenticate;
class Auth
{
protected $config;
protected $host;
protected $id;
protected $secret;
/**
* @var Client
*/
protected $rpc;
public function __construct($config)
{
$this->config = $config;
$this->configure();
}
/**
* @throws \Exception
*/
protected function configure()
{
switch (env('APP_ENV')) {
case 'local':
case 'develop':
$this->host = 'http://auth.lo.haowumc.com';
break;
case 'production':
$this->host = 'https://auth.int.haowumc.com';
break;
default:
throw new \Exception('"APP_ENV" is not defined or not allow');
}
//为了公司内部调用的统一,更换协议为 JSON RPC
if (function_exists('app')) {
$this->rpc = app('rpc.auth');
} else {
$this->rpc = new Client([
'client' => [
'auth' => [
'local' => true,
'base_uri' => env('RPC_AUTH_URI'),
],
],
]);
}
}
/**
* 指定 app
* @param $key
*/
public function choose($key)
{
$this->id = $this->config['apps'][$key]['id'];
$this->secret = $this->config['apps'][$key]['secret'];
}
/**
* 生成web授权的链接
* @param $redirect
* @return string
*/
public function connect($redirect)
{
$id = $this->id;
$redirect = urlencode($redirect);
return "{$this->host}/connect?appid={$id}&redirect=$redirect";
}
/**
* @param $code
* @return array
* @throws \JsonRpc\Exception\RpcServerException
*/
public function getAccessToken($code)
{
$token = $this->rpc->call('oauth.get_access_token', [$this->id, $this->secret, $code]);
return $token;
}
/**
* 根据用户token获取用户信息
* @param $token
* @return array|null
* @throws \JsonRpc\Exception\RpcServerException
*/
public function getUserInfo($token)
{
$info = $this->rpc->call('user.info',[$token]);
return $info;
}
/**
* 获取用户组
* @param null $token
* @return null
*/
public function getGroupUsers($token = null)
{
if ($token == null) {
$token = $_COOKIE[Authenticate::CookieName];
}
$token = urlencode($token);
$resp = $this->get("$this->host/api/group/users?access_token=$token");
if ($resp['code'] == 0) {
return $resp['data'];
}
return null;
}
/**
* @绑定好物平台用户
*
* @param int $user_id
* @param int $hwuser_id
* @return null
*/
public function bindHwUser(int $user_id, int $hwuser_id)
{
if ($user_id <= 0 || $hwuser_id <= 0) {
return null;
}
$resp = $this->post("$this->host/api/bind/hwuser", [
'id' => $user_id,
'hwmc_id' => $hwuser_id,
]);
return $resp;
}
}

View File

@ -35,8 +35,6 @@ class OAuth
* @param $redirect * @param $redirect
* @return string * @return string
*/ */
public function connect($redirect) public function connect($redirect)
{ {
$redirect = urlencode($redirect); $redirect = urlencode($redirect);

View File

@ -1,7 +0,0 @@
<?php
return [
'appid' => env('PDAUTH_APP_ID', '100002'),
'secret' => env('PDAUTH_SECRET', '123456'),
'host' => env('PDAUTH_HOST', 'http://auth.dev.haowumc.com'),
];

8
tests/1.php Normal file
View File

@ -0,0 +1,8 @@
<?php
require_once '../vendor/autoload.php';
use \PdAuth\Auth;
$auth = new Auth(require '../config/auth.php');
$auth->choose('erp');
exec('open '.$auth->connect('/'));