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

Appearance settings

Commit b6e89f8

Browse filesBrowse files
committed
增加数据操作日志接口
1 parent 5be4ee8 commit b6e89f8
Copy full SHA for b6e89f8

File tree

Expand file treeCollapse file tree

7 files changed

+130
-3
lines changed
Filter options
Expand file treeCollapse file tree

7 files changed

+130
-3
lines changed

‎backend/app/Events/DataOperation.php

Copy file name to clipboard
+38Lines 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 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 DataOperation
14+
{
15+
use Dispatchable, InteractsWithSockets, SerializesModels;
16+
17+
/**
18+
* Create a new event instance.
19+
*
20+
* @return void
21+
*/
22+
public $info;
23+
public function __construct($info)
24+
{
25+
//
26+
$this->info = $info;
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/LogController.php

Copy file name to clipboardExpand all lines: backend/app/Http/Controllers/LogController.php
+18-2Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,29 @@
99
class LogController extends Controller
1010
{
1111
//
12-
use Result;
12+
use Result,Tools;
1313

1414
public function index(Request $request)
1515
{
1616
$pageSize = $request->input('pageSize', 10);
1717
$page = $request->input('page', 1);
18-
$data = DB::table('log_logins')->select(['id', 'user_name', 'type', 'desc'])->paginate($pageSize);
18+
$data = DB::table('log_logins')->select(['id', 'user_name', 'type', 'desc'])
19+
->when(!$this->isAdmin(), function($query) {
20+
return $query->where('user_id', Auth::user()->id);
21+
})
22+
->paginate($pageSize);
23+
return Response()->json($data);
24+
}
25+
26+
// 操作日志记录
27+
public function show(Request $request){
28+
$pageSize = $request->input('pageSize', 10);
29+
$page = $request->input('page', 1);
30+
$data = DB::table('log_works')->select(['id', 'user_name', 'type', 'desc'])
31+
->when(!$this->isAdmin(), function($query) {
32+
return $query->where('user_id', Auth::user()->id);
33+
})
34+
->paginate($pageSize);
1935
return Response()->json($data);
2036
}
2137

‎backend/app/Http/Controllers/Result.php

Copy file name to clipboardExpand all lines: backend/app/Http/Controllers/Result.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,14 @@ public function deleteByIds($request)
112112
return $data;
113113
}
114114
}
115+
116+
public function log($type, $route_name, $desc)
117+
{
118+
$data = [
119+
'type' => $type,
120+
'route_name' => $route_name,
121+
'desc' => $desc
122+
];
123+
event(new DataOperation($data));
124+
}
115125
}

‎backend/app/Http/Controllers/Tools.php

Copy file name to clipboardExpand all lines: backend/app/Http/Controllers/Tools.php
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use App\Http\Requests\Request;
1212
use App\Models\Session;
1313
use Carbon\Carbon;
14+
use Illuminate\Support\Facades\Auth;
1415

1516
trait Tools
1617
{
@@ -59,4 +60,11 @@ public function getGradeById($id)
5960
return $grades[$id];
6061
}
6162

63+
public function isAdmin()
64+
{
65+
$roles = explode(',', Auth::user()->role);
66+
return in_array('admin', $roles);
67+
}
68+
69+
6270
}
+34Lines 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\DataOperation;
6+
use App\Models\LogWork;
7+
use Illuminate\Queue\InteractsWithQueue;
8+
use Illuminate\Contracts\Queue\ShouldQueue;
9+
10+
class WriteOperationEventToLog
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 DataOperation $event
26+
* @return void
27+
*/
28+
public function handle(DataOperation $event)
29+
{
30+
//
31+
$log = new LogWork();
32+
$log->log($event->info);
33+
}
34+
}

‎backend/app/Models/LogWork.php

Copy file name to clipboardExpand all lines: backend/app/Models/LogWork.php
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,30 @@
22

33
namespace App\Models;
44

5+
use Carbon\Carbon;
56
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Support\Facades\Auth;
68

79
class LogWork extends Model
810
{
911
//
1012
protected $fillable = ['user_id', 'user_name', 'ip', 'type', 'route_name', 'desc'];
13+
14+
public function log($info)
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' => $info['type'],
24+
'desc' => $info['desc'],
25+
'route_name' => $info['route_name'],
26+
'created_at' => $time,
27+
'updated_at' => $time
28+
];
29+
$this->insert($data);
30+
}
1131
}

‎backend/routes/api.php

Copy file name to clipboardExpand all lines: backend/routes/api.php
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,6 @@
6363
Route::post('students/exportAll', 'StudentController@exportAll')->name('students.exportAll');
6464

6565
// 日志管理API
66-
Route::get('logs', 'LogController@index')->name('logs.index');
66+
Route::get('logs/show', 'LogController@show')->name('logs.show'); // 操作日志
67+
Route::get('logs', 'LogController@index')->name('logs.index'); // 登录日志
6768
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.