AdminLogs.php
3.93 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
<?php
namespace common\components\adminlog;
use Yii;
use yii\db\ActiveRecord;
use yii\helpers\Url;
use common\helpers\Utils;
use common\models\AdminLog as AdminLogModel;
use function sprintf;
/**
* 后台管理员用户操作日志
* Class AdminLogs
* @package common\components\adminlog
*/
class AdminLogs
{
public static function write($event)
{
// 排除日志表自身, 没有主键的表不记录
if($event->sender instanceof AdminLogModel || !$event->sender->primaryKey()) {
return;
}
// 显示详情有待优化,不过基本功能完整齐全
if ($event->name == ActiveRecord::EVENT_AFTER_INSERT) {
$description = "%s新增了表{%s}%s:%s的%s";
} elseif($event->name == ActiveRecord::EVENT_AFTER_UPDATE) {
$description = "%s修改了表{%s}%s:%s的%s";
} else {
$description = "%s删除了表{%s}%s:%s%s";
}
if (!empty($event->changedAttributes)) {
$desc = '';
foreach($event->changedAttributes as $name => $value) {
$desc .= $name . ' : ' . $value . ' => ' . $event->sender->getAttribute($name) . ' | ';
}
$desc = substr($desc, 0, -1);
} else {
$desc = '';
}
$userName = Yii::$app->user->identity->username;
$tableName = $event->sender->tableSchema->name;
$description = sprintf($description, $userName, $tableName, $event->sender->primaryKey()[0], json_encode($event->sender->getPrimaryKey()), $desc);
$route = Url::to();
$userId = Yii::$app->user->id;
$ip = Utils::clientIp();
$model = new AdminLogModel();
$model->route = $route;
$model->description = $description;
$model->user_id = $userId;
$model->ip = $ip;
$model->save();
}
/**
* 后台用户-导入数据操作日志
* @param $dataName 数据名称说明
* @param $dataNum 插入数据条数
* @param $updateCount 更新数据条数
*/
public static function doImport($dataName, $dataNum, $updateCount = 0)
{
$userName = Yii::$app->user->identity->username;
$description = $userName . '导入[' . $dataName . ']数据[' . $dataNum . ']条';
if ($updateCount > 0) {
$description = $userName . '导入[' . $dataName . ']新增数据[' . $dataNum . ']条,更新['.$updateCount.']条';
}
$route = Url::to();
$userId = Yii::$app->user->id;
$ip = Utils::clientIp();
$model = new AdminLogModel();
$model->route = $route;
$model->description = $description;
$model->user_id = $userId;
$model->ip = $ip;
$model->save();
}
/**
* 后台用户-导出数据操作日志
* @param $dataName 数据名称说明
* @param $dataNum 数据条数
*/
public static function doExport($dataName, $dataNum)
{
$userName = Yii::$app->user->identity->username;
$description = $userName . '导出[' . $dataName . ']数据[' . $dataNum . ']条';
$route = Url::to();
$userId = Yii::$app->user->id;
$ip = Utils::clientIp();
$model = new AdminLogModel();
$model->route = $route;
$model->description = $description;
$model->user_id = $userId;
$model->ip = $ip;
$model->save();
}
/**
* 后台操作日志
* @param $dataName 数据名称说明
* @param $dataNum 数据条数
*/
public static function doWrite($description)
{
$userName = Yii::$app->user->identity->username;
$description = $userName . $description;
$route = Url::to();
$userId = Yii::$app->user->id;
$ip = Utils::clientIp();
$model = new AdminLogModel();
$model->route = $route;
$model->description = $description;
$model->user_id = $userId;
$model->ip = $ip;
$model->save();
}
}