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.
108 lines
3.5 KiB
108 lines
3.5 KiB
<?php
|
|
/*----------------------------------------------------------------------
|
|
* 项目名称: CloudAdmin
|
|
* +----------------------------------------------------------------------
|
|
* 版权所有: 2014~2020 安徽云掌开发团队
|
|
* +----------------------------------------------------------------------
|
|
* 官方网站: [ http://www.yaoyz.com、http://www.ahyunzhang.com ]
|
|
* +----------------------------------------------------------------------
|
|
* Date: 2018-12-04 22:20
|
|
* +----------------------------------------------------------------------
|
|
* Des: 日志操作类
|
|
+----------------------------------------------------------------------*/
|
|
|
|
namespace service;
|
|
|
|
|
|
use think\Db;
|
|
|
|
class LogService
|
|
{
|
|
|
|
/**
|
|
* 日志记录表
|
|
* @var string
|
|
*/
|
|
private static $table_log = 'ActionLog';
|
|
|
|
/**
|
|
* 获取数据库操作对象
|
|
*/
|
|
protected static function db()
|
|
{
|
|
return Db::name('Action');
|
|
}
|
|
|
|
/**
|
|
* 写入操作日志
|
|
* @param null $action
|
|
* @param null $model
|
|
* @param null $record_id 记录id,有可能是报错信息
|
|
* @param null $user_id
|
|
* @return string
|
|
*/
|
|
public static function write($action = null, $record_id = null)
|
|
{
|
|
//参数检查
|
|
if (empty($action)) {
|
|
return '参数不能为空';
|
|
}
|
|
|
|
$user_id = session('manager.id');
|
|
|
|
//查询行为,判断是否执行
|
|
$action_info = self::db()->getByName($action);
|
|
if ($action_info['status'] != 1) {
|
|
return '该行为被禁用或删除';
|
|
}
|
|
|
|
$request = app('request');
|
|
$time = $_SERVER['REQUEST_TIME'];
|
|
$model = strtolower(join('/', [$request->module(), $request->controller(), $request->action()]));
|
|
//插入行为日志
|
|
$data['action_id'] = $action_info['id'];
|
|
$data['user_id'] = $user_id;
|
|
$data['action_ip'] = $request->ip(1);
|
|
$data['model'] = $model;
|
|
$data['record_id'] = (int)$record_id;
|
|
$data['create_time'] = $time;
|
|
|
|
//解析日志规则,生成日志备注
|
|
if (!empty($action_info['log'])) {
|
|
if (preg_match_all('/\[(\S+?)\]/', $action_info['log'], $match)) {
|
|
//如果record_id为空,或者不为数字替换record内容
|
|
$record_id = !empty($record_id) ? $record_id : ':未更改任何数据!!!';
|
|
$log['user'] = $user_id;
|
|
$log['record'] = $record_id;
|
|
$log['model'] = $model;
|
|
$log['time'] = $time;
|
|
$log['data'] = ['user' => $user_id, 'model' => $model, 'record' => $record_id, 'time' => $time];
|
|
foreach ($match[1] as $value) {
|
|
$param = explode('|', $value);
|
|
if (isset($param[1])) {
|
|
$replace[] = call_user_func($param[1], $log[$param[0]]);
|
|
} else {
|
|
$replace[] = $log[$param[0]];
|
|
}
|
|
}
|
|
$data['remark'] = str_replace($match[0], $replace, $action_info['log']);
|
|
} else {
|
|
$data['remark'] = $action_info['log'];
|
|
}
|
|
} else {
|
|
//未定义日志规则,记录操作url
|
|
$data['remark'] = '操作url:' . $_SERVER['REQUEST_URI'];
|
|
}
|
|
Db::name(self::$table_log)->insert($data);
|
|
|
|
if (!empty($action_info['rule'])) {
|
|
//解析行为
|
|
$rules = parse_action($action, $user_id);
|
|
|
|
//执行行为
|
|
$res = execute_action($rules, $action_info['id'], $user_id);
|
|
}
|
|
}
|
|
|
|
|
|
}
|