Operatelog.php
4.7 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
171
172
173
<?php
namespace app\admin\model\facrm;
use app\admin\library\Auth;
use think\Model;
use think\Loader;
class Operatelog extends Model
{
// 表名
protected $name = 'facrm_operatelog';
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'create_time';
protected $updateTime = '';
//自定义日志标题
protected static $title = '';
//自定义日志内容
protected static $content = '';
//忽略的链接正则列表
protected static $ignoreRegex = [
'/^(.*)\/(selectpage|index)$/i',
];
/**
* 是否直接插入
* @var bool
*/
protected static $boolInsert = true;
/**
* 插入的数据
* @var array
*/
protected static $insertData = [];
public static function setBoolInsert($bool = true)
{
self::$boolInsert = $bool;
}
public static function getInsertData()
{
return self::$insertData;
}
/**
* 手动批量插入
* @return bool|int|string
*/
public static function insertData()
{
if (self::$insertData){
return self::insertAll(self::$insertData);
}
return false;
}
/**
* 配置
* @param $title 标题
* @param bool $bool 是否直接插入
*/
public static function setConfig($title,$bool= true){
self::$title = $title;
self::$boolInsert = $bool;
}
public static function setTitle($title)
{
self::$title = $title;
}
public static function getTitle()
{
return self::$title;
}
public static function setContent($content)
{
self::$content = $content;
}
public static function setIgnoreRegex($regex = [])
{
$regex = is_array($regex) ? $regex : [$regex];
self::$ignoreRegex = array_merge(self::$ignoreRegex, $regex);
}
/**
* 记录日志
* @param string $title
* @param string $content
*/
public static function record($customer_id,$contract_id=0,$receivable_id=0,$title = '', $content = '')
{
$auth = Auth::instance();
$admin_id = $auth->isLogin() ? $auth->id : 0;
$username = $auth->isLogin() ? $auth->username : __('Unknown');
$nickname = $auth->isLogin() ? $auth->nickname : __('系统');
$controllername = Loader::parseName(request()->controller());
$actionname = strtolower(request()->action());
$path = str_replace('.', '/', $controllername) . '/' . $actionname;
if (self::$ignoreRegex) {
foreach (self::$ignoreRegex as $index => $item) {
if (preg_match($item, $path)) {
return;
}
}
}
$content = $content ? $content : self::$content;
if (!$content) {
$content = request()->param('', null, 'trim,strip_tags,htmlspecialchars');
$content = self::getPureContent($content);
}
$title = $title ? $title : self::$title;
if (!$title) {
$title = [];
$breadcrumb = Auth::instance()->getBreadcrumb($path);
foreach ($breadcrumb as $k => $v) {
$title[] = $v['title'];
}
$title = implode(' / ', $title);
}
$data = [
'title' => $title,
'customer_id' => $customer_id,
'contract_id' => $contract_id,
'content' => !is_scalar($content) ? json_encode($content, JSON_UNESCAPED_UNICODE) : $content,
'url' => substr(request()->url(), 0, 1500),
'admin_id' => $admin_id,
'username' => $username,
'nickname' => $nickname,
'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255),
'ip' => request()->ip(),
'create_time'=>time()
];
if (self::$boolInsert){
return self::create($data);
}else{
self::$insertData[]=$data;
}
}
/**
* 获取已屏蔽关键信息的数据
* @param $content
* @return false|string
*/
protected static function getPureContent($content)
{
if (!is_array($content)) {
return $content;
}
foreach ($content as $index => &$item) {
if (preg_match("/(password|salt|token)/i", $index)) {
$item = "***";
} else {
if (is_array($item)) {
$item = self::getPureContent($item);
}
}
}
return $content;
}
public function admin()
{
return $this->belongsTo('Admin', 'admin_id')->setEagerlyType(0);
}
}