You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
3.5 KiB
144 lines
3.5 KiB
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\AdminOperationLog;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Request;
|
|
|
|
final class OperationLogService
|
|
{
|
|
/**
|
|
* 记录操作日志
|
|
*
|
|
* @param string $action 操作类型
|
|
* @param string $description 操作描述
|
|
* @param Model|null $model 关联模型
|
|
* @param array|null $oldValues 旧值
|
|
* @param array|null $newValues 新值
|
|
* @return AdminOperationLog
|
|
*/
|
|
public function log(
|
|
string $action,
|
|
string $description,
|
|
?Model $model = null,
|
|
?array $oldValues = null,
|
|
?array $newValues = null
|
|
): AdminOperationLog {
|
|
$data = [
|
|
'user_id' => Auth::id() ?? 0,
|
|
'action' => $action,
|
|
'description' => $description,
|
|
'ip' => Request::ip(),
|
|
];
|
|
|
|
if ($model) {
|
|
$data['model_type'] = get_class($model);
|
|
$data['model_id'] = $model->getKey();
|
|
}
|
|
|
|
if ($oldValues) {
|
|
$data['old_values'] = $oldValues;
|
|
}
|
|
|
|
if ($newValues) {
|
|
$data['new_values'] = $newValues;
|
|
}
|
|
|
|
return AdminOperationLog::create($data);
|
|
}
|
|
|
|
/**
|
|
* 记录新增操作
|
|
*
|
|
* @param Model $model 关联模型
|
|
* @param string $description 操作描述
|
|
* @return AdminOperationLog
|
|
*/
|
|
public function logCreated(Model $model, string $description = '创建记录'): AdminOperationLog
|
|
{
|
|
return $this->log(
|
|
'create',
|
|
$description,
|
|
$model,
|
|
null,
|
|
$model->toArray()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 记录新增操作-数据
|
|
*
|
|
* @param Model $model 关联模型
|
|
* @param string $description 操作描述
|
|
* @param array $data 数据
|
|
* @return AdminOperationLog
|
|
*/
|
|
public function logCreatedData(Model $model, string $description = '创建记录', array $data = []): AdminOperationLog
|
|
{
|
|
return $this->log(
|
|
'create',
|
|
$description,
|
|
$model,
|
|
null,
|
|
$data
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 记录更新操作
|
|
*
|
|
* @param Model $model 关联模型
|
|
* @param array $oldValues 旧值
|
|
* @param string $description 操作描述
|
|
* @return AdminOperationLog
|
|
*/
|
|
public function logUpdated(Model $model, array $oldValues, string $description = '更新记录'): AdminOperationLog
|
|
{
|
|
return $this->log(
|
|
'update',
|
|
$description,
|
|
$model,
|
|
$oldValues,
|
|
$model->toArray()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 记录删除操作
|
|
*
|
|
* @param Model $model 关联模型
|
|
* @param string $description 操作描述
|
|
* @return AdminOperationLog
|
|
*/
|
|
public function logDeleted(Model $model, string $description = '删除记录'): AdminOperationLog
|
|
{
|
|
return $this->log(
|
|
'delete',
|
|
$description,
|
|
$model,
|
|
$model->toArray(),
|
|
null
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 记录删除操作
|
|
*
|
|
* @param Model $model 关联模型
|
|
* @param string $description 操作描述
|
|
* @param array $data 数据
|
|
* @return AdminOperationLog
|
|
*/
|
|
public function logDeletedData(Model $model, string $description = '删除记录', array $data = []): AdminOperationLog
|
|
{
|
|
return $this->log(
|
|
'delete',
|
|
$description,
|
|
$model,
|
|
$data,
|
|
null
|
|
);
|
|
}
|
|
}
|
|
|