8000 增加日志、数据字典等数据表 · czsvn/laravel_template_with_vue@6950ea4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6950ea4

Browse files
committed
增加日志、数据字典等数据表
利用事件进行登录日志的记录
1 parent 6d660a2 commit 6950ea4

17 files changed

+449
-0
lines changed

backend/app/Events/Event.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Queue\SerializesModels;
7+
use Illuminate\Broadcasting\PrivateChannel;
8+
use Illuminate\Broadcasting\PresenceChannel;
9+
use Illuminate\Foundation\Events\Dispatchable;
10+
use Illuminate\Broadcasting\InteractsWithSockets;
11+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
12+
13+
class Event
14+
{
15+
use Dispatchable, InteractsWithSockets, SerializesModels;
16+
17+
/**
18+
* Create a new event instance.
19+
*
20+
* @return void
21+
*/
22+
public function __construct()
23+
{
24+
//
25+
}
26+
27+
/**
28+
* Get the channels the event should broadcast on.
29+
*
30+
* @return \Illuminate\Broadcasting\Channel|array
31+
*/
32+
public function broadcastOn()
33+
{
34+
return new PrivateChannel('channel-name');
35+
}
36+
}

backend/app/Events/UserLogin.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Queue\SerializesModels;
7+
use Illuminate\Broadcasting\PrivateChannel;
8+
use Illuminate\Broadcasting\PresenceChannel;
9+
use Illuminate\Foundation\Events\Dispatchable;
10+
use Illuminate\Broadcasting\InteractsWithSockets;
11+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
12+
13+
class UserLogin
14+
{
15+
use Dispatchable, InteractsWithSockets, SerializesModels;
16+
17+
/**
18+
* Create a new event instance.
19+
*
20+
* @return void
21+
*/
22+
public function __construct()
23+
{
24+
//
25+
}
26+
27+
/**
28+
* Get the channels the event should broadcast on.
29+
*
30+
* @return \Illuminate\Broadcasting\Channel|array
31+
*/
32+
pu F438 blic function broadcastOn()
33+
{
34+
return new PrivateChannel('channel-name');
35+
}
36+
}

backend/app/Events/UserLogout.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\Models\User;
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 UserLogout
15+
{
16+
use Dispatchable, InteractsWithSockets, SerializesModels;
17+
public $user;
18+
/**
19+
* Create a new event instance.
20+
*
21+
* @return void
22+
*/
23+
public function __construct(User $user)
24+
{
25+
//
26+
$this->user = $user;
27+
}
28+
29+
/**
30+
* Get the channels the event should broadcast on.
31+
*
32+
* @return \Illuminate\Broadcasting\Channel|array
33+
*/
34+
public function broadcastOn()
35+
{
36+
return new PrivateChannel('channel-name');
37+
}
38+
}

backend/app/Http/Controllers/AuthController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Events\UserLogin;
56
use App\Models\User;
67
use Illuminate\Http\Request;
78
use Illuminate\Support\Facades\Auth;
@@ -41,7 +42,9 @@ public function handleProviderCallback()
4142
// 自动登录
4243
$userInstance = User::where('id', $id)->firstOrFail();
4344
Auth::login($userInstance);
45+
4446
$token = $userInstance->createToken('token')->accessToken;
47+
event(new UserLogin());
4548
// 得到$token
4649
$data['time'] = session('uuid') ;
4750
$data['token'] = $token;

backend/app/Http/Proxy/TokenProxy.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22

33
namespace App\Http\Proxy;
4+
use App\Events\UserLogin;
5+
use App\Events\UserLogout;
6+
use App\Models\LogLogin;
47
use App\Models\ThreeLogin;
58
use Illuminate\Support\Facades\Auth;
69

@@ -26,6 +29,7 @@ public function __construct(\GuzzleHttp\Client $http)
2629
public function login($email, $password)
2730
{
2831
if (auth()->attempt(['email'=> $email, 'password'=> $password])){
32+
event(new UserLogin());
2933
return $this->proxy('password', [
3034
'username' => $email,
3135
'password' => $password,
@@ -42,6 +46,7 @@ public function loginWithThree($email, $password, $id, $provider)
4246
{
4347
if (auth()->attempt(['email'=> $email, 'password'=> $password])){
4448
$user_id = Auth::user()->id;
49+
event(new UserLogin());
4550
ThreeLogin::firstOrCreate(['platform_id'=>$id, 'provider'=>$provider, 'user_id' => $user_id]);
4651
return $this->proxy('password', [
4752
'username' => $email,
@@ -84,6 +89,10 @@ public function logout()
8489
]);
8590
app('cookie')->forget('refreshToken');
8691
$accessToken->revoke();
92+
// $log = new LogLogin();
93+
// $log->saveLogoutLog($user);
94+
event(new UserLogout($user));
95+
// event(new UserLogout($user));
8796
return response()->json([
8897
'status' => 'success',
8998
'status_code' => 200,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace App\Listeners;
4+
5+
use App\Events\Event;
6+
use Illuminate\Queue\InteractsWithQueue;
7+
use Illuminate\Contracts\Queue\ShouldQueue;
8+
9+
class EventListener
10+
{
11+
/**
12+
* Create the event listener.
13+
*
14+
* @return void
15+
*/
16+
public function __construct()
17+
{
18+
//
19+
}
20+
21+
/**
22+
* Handle the event.
23+
*
24+
* @param Event $event
25+
* @return void
26+
*/
27+
public function handle(Event $event)
28+
{
29+
//
30+
}
31+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace App\Listeners;
4+
5+
use App\Events\UserLogin;
6+
use App\Models\LogLogin;
7+
use Illuminate\Queue\InteractsWithQueue;
8+
use Illuminate\Contracts\Queue\ShouldQueue;
9+
10+
class WriteLoginEventToLog
11+
{
12+
/**
13+
* Create the event listener.
14+
*
15+
* @return void
16+
*/
17+
public function __construct()
18+
{
19+
//
20+
}
21+
22+
/**
23+
* Handle the event.
24+
*
25+
* @param UserLogin $event
26+
* @return void
27+
*/
28+
public function handle(UserLogin $event)
29+
{
30+
//
31+
$log = new LogLogin();
32+
$log->saveLoginLog();
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace App\Listeners;
4+
5+
use App\Events\UserLogout;
6+
use App\Models\LogLogin;
7+
use Illuminate\Queue\InteractsWithQueue;
8+
use Illuminate\Contracts\Queue\ShouldQueue;
9+
10+
class WriteLogoutEventToLog
11+
{
12+
/**
13+
* Create the event listener.
14+
*
15+
* @return void
16+
*/
17+
public function __construct()
18+
{
19+
//
20+
}
21+
22+
/**
23+
* Handle the event.
24+
*
25+
* @param UserLogout $event
26+
* @return void
27+
*/
28+
public function handle(UserLogout $event)
29+
{
30+
//
31+
$log = new LogLogin();
32+
$log->saveLogoutLog($event->user);
33+
}
34+
}

backend/app/Models/Dict.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Dict extends Model
8+
{
9+
//
10+
protected $fillable = ['name', 'desc', 'remark'];
11+
}

backend/app/Models/DictDetails.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class DictDetails extends Model
8+
{
9+
//
10+
}

backend/app/Models/LogLogin.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use App\Http\Requests\Request;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Support\Facades\Auth;
8+
use Carbon\Carbon;
9+
10+
class LogLogin extends Model
11+
{
12+
//
13+
protected $fillable = ['user_id', 'user_name', 'ip', 'type', 'desc'];
14+
public function saveLoginLog()
15+
{
16+
$request = request();
17+
$time = Carbon::now();
18+
$strTime = $time->year.''.$time->month.''.$time->day.''.$time->hour.''.$time->minute.''.$time->second.'';
19+
$data = [
20+
'user_id' => Auth::user()->id,
21+
'user_name' => Auth::user()->name,
22+
'ip' => $request->ip(),
23+
'type' => 'login',
24+
'desc' => Auth::user()->name.''.$strTime.'登录系统',
25+
'created_at' => $time,
26+
'updated_at' => $time
27+
];
28+
$this->insert($data);
29+
}
30+
31+
public function saveLogoutLog($user)
32+
{
33+
//dd($user);
34+
$request = request();
35+
$time = Carbon::now();
36+
$strTime = $time->year.''.$time->month.''.$time->day.''.$time->hour.''.$time->minute.''.$time->second.'';
37+
$data = [
38+
'user_id' => $user->id,
39+
'user_name' => $user->name,
40+
'ip' => $request->ip(),
41+
'type' => 'logout',
42+
'desc' => $user->name.''.$strTime.'退出系统',
43+
'created_at' => $time,
44+
'updated_at' => $time
45+
];
46+
$this->insert($data);
47+
}
48+
}

backend/app/Models/LogWork.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6< B23C span class="diff-text-marker">+
7+
class LogWork extends Model
8+
{
9+
//
10+
protected $fillable = ['user_id', 'user_name', 'ip', 'type', 'route_name', 'desc'];
11+
}

backend/app/Providers/EventServiceProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ class EventServiceProvider extends ServiceProvider
1616
'App\Events\Event' => [
1717
'App\Listeners\EventListener',
1818
],
19+
'App\Events\UserLogin' => [
20+
'App\Listeners\WriteLoginEventToLog',
21+
],
22+
'App\Events\UserLogout' => [
23+
'App\Listeners\WriteLogoutEventToLog',
24+
],
1925
];
2026

2127
/**

0 commit comments

Comments
 (0)
0