增加日志功能
This commit is contained in:
+53
-101
@@ -5,6 +5,7 @@ use app\admin\controller\Base;
|
||||
use think\facade\Db;
|
||||
use think\facade\Request;
|
||||
use think\facade\View;
|
||||
use think\facade\Cookie;
|
||||
|
||||
class Log extends Base
|
||||
{
|
||||
@@ -62,43 +63,38 @@ class Log extends Base
|
||||
*/
|
||||
public function operation()
|
||||
{
|
||||
if (Request::isPost()) {
|
||||
$page = input('post.page', 1);
|
||||
$limit = input('post.limit', 10);
|
||||
$username = input('post.username');
|
||||
$module = input('post.module');
|
||||
$operation = input('post.operation');
|
||||
$status = input('post.status');
|
||||
$startTime = input('post.start_time');
|
||||
$endTime = input('post.end_time');
|
||||
$params = input();
|
||||
$page = $params['page'] ?? 1;
|
||||
$limit = $params['limit'] ?? 10;
|
||||
|
||||
$query = Db::name('yz_logs_operation');
|
||||
|
||||
// 构建搜索条件
|
||||
$searchFields = [
|
||||
'username' => ['field' => 'username', 'type' => 'like'],
|
||||
'module' => ['field' => 'module', 'type' => 'like'],
|
||||
'operation' => ['field' => 'operation', 'type' => 'like'],
|
||||
'status' => ['field' => 'status', 'type' => '='],
|
||||
'start_time' => ['field' => 'operation_time', 'type' => '>='],
|
||||
'end_time' => ['field' => 'operation_time', 'type' => '<=']
|
||||
];
|
||||
|
||||
foreach ($searchFields as $param => $config) {
|
||||
if (!empty($params[$param])) {
|
||||
$value = $params[$param];
|
||||
if ($config['type'] === 'like') {
|
||||
$value = "%{$value}%";
|
||||
}
|
||||
$query = $query->where($config['field'], $config['type'], $value);
|
||||
}
|
||||
}
|
||||
|
||||
$query = Db::name('yz_logs_operation');
|
||||
|
||||
// 搜索条件
|
||||
if ($username) {
|
||||
$query = $query->where('username', 'like', "%{$username}%");
|
||||
}
|
||||
if ($module) {
|
||||
$query = $query->where('module', 'like', "%{$module}%");
|
||||
}
|
||||
if ($operation) {
|
||||
$query = $query->where('operation', 'like', "%{$operation}%");
|
||||
}
|
||||
if ($status !== '') {
|
||||
$query = $query->where('status', $status);
|
||||
}
|
||||
if ($startTime) {
|
||||
$query = $query->where('operation_time', '>=', $startTime);
|
||||
}
|
||||
if ($endTime) {
|
||||
$query = $query->where('operation_time', '<=', $endTime);
|
||||
}
|
||||
|
||||
$count = $query->count();
|
||||
$list = $query->order('id desc')
|
||||
->page($page, $limit)
|
||||
->select();
|
||||
$count = $query->count();
|
||||
$list = $query->order('id desc')
|
||||
->page($page, $limit)
|
||||
->select();
|
||||
|
||||
if (Request::isAjax()) {
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '获取成功',
|
||||
@@ -112,12 +108,16 @@ class Log extends Base
|
||||
|
||||
/**
|
||||
* 记录操作日志
|
||||
* @param string $operation 操作名称
|
||||
* @param int $status 状态 1成功 0失败
|
||||
* @param string $error_message 错误信息
|
||||
* @param string $module 模块名称
|
||||
*/
|
||||
protected function recordOperation($operation, $status = 1, $error_message = '')
|
||||
public static function record($operation, $status = 1, $error_message = '', $module = '')
|
||||
{
|
||||
$data = [
|
||||
'username' => session('admin_username'),
|
||||
'module' => '日志管理',
|
||||
'username' => Cookie::get('admin_name') ?: '未知用户',
|
||||
'module' => $module ?: '系统管理',
|
||||
'operation' => $operation,
|
||||
'request_method' => Request::method(),
|
||||
'request_url' => Request::url(true),
|
||||
@@ -133,76 +133,28 @@ class Log extends Base
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除登录日志
|
||||
* 获取操作日志详情
|
||||
*/
|
||||
public function deleteLogin()
|
||||
public function getOperationDetail()
|
||||
{
|
||||
$id = input('post.id');
|
||||
try {
|
||||
if (Db::name('yz_logs_login')->delete($id)) {
|
||||
$this->recordOperation('删除登录日志');
|
||||
return json(['code' => 0, 'msg' => '删除成功']);
|
||||
}
|
||||
$this->recordOperation('删除登录日志', 0, '删除失败');
|
||||
return json(['code' => 1, 'msg' => '删除失败']);
|
||||
} catch (\Exception $e) {
|
||||
$this->recordOperation('删除登录日志', 0, $e->getMessage());
|
||||
return json(['code' => 1, 'msg' => '删除失败:' . $e->getMessage()]);
|
||||
$id = input('id/d', 0);
|
||||
if (!$id) {
|
||||
return json(['code' => 1, 'msg' => '参数错误']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除操作日志
|
||||
*/
|
||||
public function deleteOperation()
|
||||
{
|
||||
$id = input('post.id');
|
||||
try {
|
||||
if (Db::name('yz_logs_operation')->delete($id)) {
|
||||
$this->recordOperation('删除操作日志');
|
||||
return json(['code' => 0, 'msg' => '删除成功']);
|
||||
}
|
||||
$this->recordOperation('删除操作日志', 0, '删除失败');
|
||||
return json(['code' => 1, 'msg' => '删除失败']);
|
||||
} catch (\Exception $e) {
|
||||
$this->recordOperation('删除操作日志', 0, $e->getMessage());
|
||||
return json(['code' => 1, 'msg' => '删除失败:' . $e->getMessage()]);
|
||||
$info = Db::name('yz_logs_operation')
|
||||
->where('id', $id)
|
||||
->find();
|
||||
|
||||
if (!$info) {
|
||||
return json(['code' => 1, 'msg' => '日志不存在']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空登录日志
|
||||
*/
|
||||
public function clearLogin()
|
||||
{
|
||||
try {
|
||||
if (Db::name('yz_logs_login')->where('1=1')->delete()) {
|
||||
$this->recordOperation('清空登录日志');
|
||||
return json(['code' => 0, 'msg' => '清空成功']);
|
||||
}
|
||||
$this->recordOperation('清空登录日志', 0, '清空失败');
|
||||
return json(['code' => 1, 'msg' => '清空失败']);
|
||||
} catch (\Exception $e) {
|
||||
$this->recordOperation('清空登录日志', 0, $e->getMessage());
|
||||
return json(['code' => 1, 'msg' => '清空失败:' . $e->getMessage()]);
|
||||
// 格式化请求参数
|
||||
if (!empty($info['request_params'])) {
|
||||
$info['request_params'] = json_decode($info['request_params'], true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空操作日志
|
||||
*/
|
||||
public function clearOperation()
|
||||
{
|
||||
try {
|
||||
if (Db::name('yz_logs_operation')->where('1=1')->delete()) {
|
||||
$this->recordOperation('清空操作日志');
|
||||
return json(['code' => 0, 'msg' => '清空成功']);
|
||||
}
|
||||
$this->recordOperation('清空操作日志', 0, '清空失败');
|
||||
return json(['code' => 1, 'msg' => '清空失败']);
|
||||
} catch (\Exception $e) {
|
||||
$this->recordOperation('清空操作日志', 0, $e->getMessage());
|
||||
return json(['code' => 1, 'msg' => '清空失败:' . $e->getMessage()]);
|
||||
}
|
||||
return json(['code' => 0, 'msg' => '获取成功', 'data' => $info]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user