Skip to content

Commit

Permalink
单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
2540463097 committed Jan 15, 2021
1 parent 25aed2d commit 68407b2
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 101 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ composer.lock
.subsplit
.php_cs.cache
test.php
Test
test
131 changes: 44 additions & 87 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ $ composer require pltrue/thirdparty_oauth
| 2020-12-13| feat | 新增`Line账户`登录 | [742481030](https://github.com/742481030) | 已合并到master分支 |
| 2020-12-29| fix | 增加兼容支付宝qq app混合应用兼容 | [742481030](https://github.com/742481030) | 已合并到master分支 |
| 2020-12-29| feat | 新增`京东账户`登录 | [742481030](https://github.com/742481030) | 已合并到master分支 |
| 2020-12-29| fix | 兼容laravel7* | [742481030](https://github.com/pl1998) | 已合并到master分支 |



Expand Down Expand Up @@ -116,12 +117,12 @@ use Pl1998\ThirdpartyOauth\SocialiteAuth;

$api = new SocialiteAuth([
'client_id' => '74ee75f10437b4862d653a682111e5ddca1d24422f00ec884453ad232ae07ac9',
'redirect_uri' => 'http://oauth.Test/Test.php'
'redirect_uri' => 'http://oauth.test/test.php',
'client_secret'=>''
]);

$json = $api->redirect('weibo');
return $api->redirect('github');

var_dump($json);
```

#### 回调接口方法
Expand All @@ -134,19 +135,12 @@ use Pl1998\ThirdpartyOauth\SocialiteAuth;

$api = new SocialiteAuth([
'client_id' => '74ee75f10437b4862d653a682111e5ddca1d24422f00ec884453ad232ae07ac9',
'redirect_uri' => 'http://oauth.Test/Test.php',
'redirect_uri' => 'http://oauth.test/test.php',
'client_secret' => ''
]);

$user = $api->driver('jd')->user();
$user = $api->driver('github')->user();

interface auth
{
public function login();

}
OAuth::huawei()->login();
OAuth::jd()->login();
var_dump($user);die;

```
Expand All @@ -172,14 +166,9 @@ Route::get('gitee/callback','api/TestController/giteeCallback')->name('授权回
return [
'github' => [
'client_id' => '2365a07a73dc25a27e5c7a968248b96beb53a1ad300de7ba6bf4ffe247a4b386',
'redirect_uri' => 'http://Test.Test/gitee/callback',
'redirect_uri' => 'http://test.test/gitee/callback',
'client_secret' => ''
],
'github' => [
'client_id' => '2365a07a73dc25a27e5c7a968248b96beb53a1ad300de7ba6bf4ffe247a4b386',
'redirect_uri' => 'http://Test.Test/gitee/callback',
'client_secret' => ''
]
]
];

```
Expand Down Expand Up @@ -241,107 +230,75 @@ class TestController
```php

.
.
.
'oauth' => [
'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'redirect_uri' => env('GITHUB_REDIRECT_URI'),
'client_secret'=>env('GITHUB_CLIENT_SECRET')
]
]
.....
```

#### 在 <kbd>.env</kdb>中配置

```shell
GITHUB_CLIENT_ID=xxxx
GITHUB_REDIRECT_URI=xxx
GITHUB_CLIENT_SECRET=xxx
GITHUB_CLIENT_ID=684a49aa60ce
GITHUB_CLINET_SECRETS=86c3800b0da6b6687e7572a9251860
GITHUB_CALLBACK_URL=http://test.test/callback/github
```

## 创建路由

```php
Route::get('auth/github','IndexController@auth')->name('github授权');
Route::get('callback/github','IndexController@callback')->name('github回调接口');
Route::get('auth/github-redirect','OauthController@redirect');
Route::get('callback/github','OauthController@auth');
```


##如果要使用laravel app 注入。先在`config/app.php`注册服务提供者

```php
....
'providers' => [
\Pl1998\ThirdpartyOauth\ServiceProvider::class
]
```


## 控制器方法

```php
/**
* 授权方法
* @return mixed
*/
public function auth()
{
//普通写法
// $auth = new SocialiteAuth(config('services.oauth'));
// $auth->redirect('github');
<?php

//laravel 容器使用
app('socialiteAuth')->redirect('github');

namespace App\Http\Controllers;
use Pl1998\ThirdpartyOauth\SocialiteAuth;

class OauthController extends Controller
{
public function redirect()
{
//return app('SocialiteAuth')->redirect('weibo');
$api = new SocialiteAuth(config('services.aouth.github'));
return $api->redirect('github');
}
/**
* 回调方法
*/
public function callback()
public function auth()
{
//普通写法
//$auth = new SocialiteAuth(config('services.oauth.github'));
//$user = $auth->driver('github')->user();
//var_dump($user);
//laravel 容器使用
$user = app('socialiteAuth')->driver('github')->user();
var_dump($user);
// $api = app('SocialiteAuth')->driver('weibo');
//dd($api->user())
$api = new SocialiteAuth(config('services.aouth.github'));
$user = $api->driver('github');
dd($user->user());
}
```
}


```

## 返回示例

```json
{
"login": "pl1998",
"id": 43993206,
"node_id": "MDQ6VXNlcjQzOTkzMjA2",
"avatar_url": "https://avatars1.githubusercontent.com/u/43993206?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/pl1998",
"html_url": "https://github.com/pl1998",
"followers_url": "https://api.github.com/users/pl1998/followers",
"following_url": "https://api.github.com/users/pl1998/following{/other_user}",
"gists_url": "https://api.github.com/users/pl1998/gists{/gist_id}",
"starred_url": "https://api.github.com/users/pl1998/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/pl1998/subscriptions",
"organizations_url": "https://api.github.com/users/pl1998/orgs",
"repos_url": "https://api.github.com/users/pl1998/repos",
"events_url": "https://api.github.com/users/pl1998/events{/privacy}",
"received_events_url": "https://api.github.com/users/pl1998/received_events",
"type": "User",
"site_admin": false,
"name": "pltrue",
"company": null,
"blog": "pltrue.top",
"location": "深圳",
"email": null,
"hireable": null,
"bio": null,
"twitter_username": null,
"public_repos": 6,
"public_gists": 0,
"followers": 1,
"following": 1,
"created_at": "2018-10-09T12:42:14Z",
"updated_at": "2020-09-17T04:49:23Z"
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210115174351473.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyMDMyMTE3,size_16,color_FFFFFF,t_70#pic_center)

```
<br/>

## License
Expand Down
Binary file added img_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ class ServiceProvider extends \Illuminate\Support\ServiceProvider

public function register()
{
$this->app->singleton(SocialiteAuth::class, function () {
$this->app->singleton(SocialiteAuth::class, function ($app) {
return new SocialiteAuth(config('services.aouth'));
});

$this->app->alias(SocialiteAuth::class, 'socialiteAuth');
$this->app->alias(SocialiteAuth::class, 'SocialiteAuth');
}

public function provides()
{
return [SocialiteAuth::class, 'socialiteAuth'];
return [SocialiteAuth::class, 'SocialiteAuth'];
}
}
24 changes: 14 additions & 10 deletions src/SocialiteAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
class SocialiteAuth implements Socialite
{
/**
* 用户json数据.
*
* user info object.
* @var
*/
protected $userJson;
protected $userObject;


/**
* 目前支持的授权平台.
Expand All @@ -33,27 +33,31 @@ class SocialiteAuth implements Socialite
'huawei', 'douyin', 'line', 'qqapp', 'alipayapp', 'jd'];

/**
* 配置文件.
*
* 初始化的配置数组.
* @var array
*/
protected $config = [];
protected $config = [
'client_id' => '',
'redirect_uri' => '',
'client_secret'=> ''
];

public function __construct(array $config)
public function __construct($config = [])
{
$this->config = $config;
if (is_array($config)) $this->config = $config;
}

public function driver($deiver): SocialiteAuth
{
//兼容laravel app容器参数注入
if (array_key_exists($deiver, $this->config)) {

$this->config = $this->config[$deiver];
}
$this->verified($deiver);
try {
$api = new SocialiteApi($deiver, $this->config);
$this->userJson = $api->getUserInfo();
$this->userObject = $api->getUserInfo();
} catch (InvalidArgumentException $exception) {
throw new InvalidArgumentException($exception->getMessage(), $exception->getCode());
}
Expand All @@ -68,7 +72,7 @@ public function driver($deiver): SocialiteAuth
*/
public function user()
{
return $this->userJson;
return $this->userObject;
}

/**
Expand Down

0 comments on commit 68407b2

Please sign in to comment.