OperationLog.php
3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace App\Helpers;
use App\Models\SystemOperateLog;
use App\Models\User;
use Auth;
use Illuminate\Database\Eloquent\Model;
use Str;
class OperationLog
{
protected string $guard;
protected string $action = '';
protected string $content = '';
protected Model $subject;
protected int $causer;
protected array $append = [];
private function __construct(string $guard)
{
$this->guard = $guard;
$this->causer = request()?->user()?->getKey() ?? 0;
}
/**
* @return self
*/
public static function admin(): self
{
return new self('Admin');
}
/**
* @return self
*/
public static function manage(): self
{
return new self('Manage');
}
/**
* @return self
*/
public static function guard(): self
{
return new self(Str::ucfirst(Auth::getDefaultDriver()));
}
/**
* @return $this
*/
public function createAction(): OperationLog
{
$this->action = '新增操作';
return $this;
}
/**
* @return $this
*/
public function updateAction(): OperationLog
{
$this->action = '内容调整';
return $this;
}
/**
* @return $this
*/
public function statusAction(): OperationLog
{
$this->action = '状态变更';
return $this;
}
/**
* @return $this
*/
public function deleteAction(): OperationLog
{
$this->action = '删除操作';
return $this;
}
/**
* @param string $action
* @return $this
*/
public function action(string $action): OperationLog
{
$this->action = $action;
return $this;
}
/**
* @param \Illuminate\Database\Eloquent\Model $subject
* @return $this
*/
public function subject(Model $subject): OperationLog
{
$this->subject = $subject;
return $this;
}
/**
* @param int|\App\Models\User $causer
* @return $this
*/
public function causer(User|int $causer): OperationLog
{
$this->causer = $causer instanceof User ? $causer->getKey() : (int)$causer;
return $this;
}
/**
* @param array $append
* @return $this
*/
public function append(array $append): OperationLog
{
$this->append = $append;
return $this;
}
/**
* @param string $format
* @param mixed ...$values
* @return SystemOperateLog
*/
public function content(string $format, mixed ...$values): SystemOperateLog
{
$this->content = sprintf($format, ...$values);
return $this->save();
}
/**
* @return SystemOperateLog
*/
private function save(): SystemOperateLog
{
if (!in_array($this->guard, ['Admin', 'Manage'], true)) {
return new SystemOperateLog([
'guard' => $this->guard,
'user_id' => $this->causer,
'action' => $this->action,
'content' => $this->content,
'subject_type' => $this->subject->getMorphClass(),
'subject_id' => $this->subject->getKey(),
] + $this->append);
}
return SystemOperateLog::query()->create([
'guard' => $this->guard,
'user_id' => $this->causer,
'path' => explode(',', request()?->header('Route', '')),
'action' => $this->action,
'content' => $this->content,
'subject_type' => $this->subject->getMorphClass(),
'subject_id' => $this->subject->getKey(),
] + $this->append);
}
}