Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b937acd

Browse files
committed
增加第三方登陆gitee相关接口和页面--完善中
1 parent 6cd4b54 commit b937acd

File tree

33 files changed

+822
-102
lines changed

33 files changed

+822
-102
lines changed

‎api/app/Events/ThreeLogin.php‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\Models\Admin;
6+
use Illuminate\Broadcasting\Channel;
7+
use Illuminate\Queue\SerializesModels;
8+
use Illuminate\Broadcasting\PrivateChannel;
9+
use Illuminate\Broadcasting\PresenceChannel;
10+
use Illuminate\Foundation\Events\Dispatchable;
11+
use Illuminate\Broadcasting\InteractsWithSockets;
12+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
13+
14+
class ThreeLogin implements ShouldBroadcast
15+
{
16+
use Dispatchable, InteractsWithSockets, SerializesModels;
17+
18+
/**
19+
* Create a new event instance.
20+
*
21+
* @return void
22+
*/
23+
public $loginType;
24+
public $uuid;
25+
public function __construct($type, $uuid = "abc")
26+
{
27+
//
28+
$this->loginType = $type;
29+
$this->uuid = $uuid;
30+
}
31+
32+
/**
33+
* Get the channels the event should broadcast on.
34+
*
35+
* @return \Illuminate\Broadcasting\Channel|array
36+
*/
37+
public function broadcastOn()
38+
{
39+
return new Channel('success.'.$this->uuid);
40+
}
41+
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Admin;
4+
5+
6+
use App\Events\ThreeLogin;
7+
use Illuminate\Http\Request;
8+
9+
use GuzzleHttp\Client;
10+
use Illuminate\Support\Facades\Cache;
11+
use Laravel\Socialite\Facades\Socialite;
12+
13+
class OauthController extends Controller
14+
{
15+
public $github_id = '52c2e3cc172fee5bccd7';
16+
public $github_secret = 'fa5a77a1ddbea1728fc1c33bb53eef6e8b1e5757';
17+
public $github_callback = 'http://localhost:8006/oauth/github/callback';
18+
19+
public $gitee_id = '9fb826c130d2c709f579a07ac75a0961055fdb756e8c2107202f50bb634deb88';
20+
public $gitee_secret = 'bdaeab8f1427802f5f30b4e3b524ec513ea143648b1d5c54b5dae8caa7865489';
21+
public $gitee_callback = "https://lv6.halian.net/api/admin/oauth/gitee";
22+
/**
23+
* Display a listing of the resource.
24+
*
25+
* @return \Illuminate\Http\Response
26+
*/
27+
28+
public $uuid = null;
29+
30+
public function test1()
31+
{
32+
global $uuid;
33+
$uuid = request('uuid');
34+
$uuid = $uuid;
35+
36+
}
37+
38+
public function test2()
39+
{
40+
dd($this->uuid);
41+
}
42+
43+
public function redirectToProvider()
44+
{
45+
Cache::set('github_url', request('address'));
46+
return Socialite::driver('github')->stateless()->redirect();
47+
}
48+
49+
public function getUserInfoByGithub()
50+
{
51+
$result = Socialite::driver('github')->stateless()->user();
52+
$token = $result->token;
53+
$user = $result->user;
54+
Cache::set($token, $user['id']);
55+
$url = Cache::get('github_url').'?token='.$token;
56+
Header("HTTP/1.1 303 See Other");
57+
header("Location: ".$url);
58+
exit();
59+
}
60+
61+
public function redirectToGitee()
62+
{
63+
Cache::set('gitee_url', request('address'));
64+
$uuid = request('uuid');
65+
$redirect = $this->gitee_callback."?uuid=$uuid";
66+
$url = "https://gitee.com/oauth/authorize?client_id=".$this->gitee_id."&redirect_uri=".$redirect."&response_type=code";
67+
Header("HTTP/1.1 303 See Other");
68+
header("Location: $url");
69+
exit();
70+
}
71+
72+
public function getUserInfoByGitee()
73+
{
74+
75+
$code = request('code');
76+
$uuid = request('uuid');
77+
// $_SERVER['REDIRECT_QUERY_STRING'] = "code=$code";
78+
// $_SERVER['QUERY_STRING'] = "code=$code";
79+
// $_SERVER['REQUEST_URI'] = "/api/admin/oauth/gitee?code=$code";
80+
// request()->except('uuid');
81+
$client = new Client();
82+
// 根据code取得令牌
83+
$response = $client->request("POST", "https://gitee.com/oauth/token", [
84+
"form_params" => [
85+
'grant_type' => 'authorization_code',
86+
'code' => $code,
87+
'client_id' => $this->gitee_id,
88+
'redirect_uri' => $this->gitee_callback."?uuid=$uuid",
89+
'client_secret' => $this->gitee_secret
90+
]]);
91+
$result = json_decode($response->getBody()->getContents(), true);
92+
$access_token = $result['access_token'];
93+
// 根据令牌取得个人信息,并存储于缓存
94+
$response = $client->request("GET", "https://gitee.com/api/v5/user?access_token=$access_token");
95+
$result = json_decode($response->getBody()->getContents(), true);
96+
Cache::set("gitee_$access_token", $result['id']);
97+
Header("HTTP/1.1 303 See Other");
98+
broadcast(new ThreeLogin('gitee', $uuid));
99+
$url = Cache::get('gitee_url')."?token=$access_token";
100+
header("Location: ".$url);
101+
exit();
102+
}
103+
104+
public function index()
105+
{
106+
//
107+
}
108+
109+
/**
110+
* Store a newly created resource in storage.
111+
*
112+
* @param \Illuminate\Http\Request $request
113+
* @return \Illuminate\Http\Response
114+
*/
115+
public function store(Request $request)
116+
{
117+
//
118+
}
119+
120+
/**
121+
* Display the specified resource.
122+
*
123+
* @param int $id
124+
* @return \Illuminate\Http\Response
125+
*/
126+
public function show($id)
127+
{
128+
//
129+
}
130+
131+
/**
132+
* Update the specified resource in storage.
133+
*
134+
* @param \Illuminate\Http\Request $request
135+
* @param int $id
136+
* @return \Illuminate\Http\Response
137+
*/
138+
public function update(Request $request, $id)
139+
{
140+
//
141+
}
142+
143+
/**
144+
* Remove the specified resource from storage.
145+
*
146+
* @param int $id
147+
* @return \Illuminate\Http\Response
148+
*/
149+
public function destroy($id)
150+
{
151+
//
152+
}
153+
}

‎api/composer.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"guzzlehttp/guzzle": "~6.0",
1414
"laravel/framework": "6.20.*",
1515
"laravel/passport": "9.*",
16+
"laravel/socialite": "^5.2",
1617
"laravel/tinker": "^2.5",
1718
"mews/captcha": "3.2",
1819
"overtrue/easy-sms": "^1.3",

‎api/composer.lock‎

Lines changed: 158 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎api/config/services.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,10 @@
3030
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
3131
],
3232

33+
'github' => [
34+
'client_id' => env('GITHUB_CLIENT_ID', '52c2e3cc172fee5bccd7'), // Your GitHub Client ID
35+
'client_secret' => env('GITHUB_CLIENT_SECRET', 'fa5a77a1ddbea1728fc1c33bb53eef6e8b1e5757'), // Your GitHub Client Secret
36+
'redirect' => env('GITHUB_CLIENT_CALLBACK', 'https://lv6.halian.net/api/admin/oauth/github'),
37+
],
38+
3339
];

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /