160 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			160 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
namespace app\admin\controller;
 | 
						|
 | 
						|
use app\admin\controller\Base;
 | 
						|
use think\facade\Db;
 | 
						|
use think\facade\Request;
 | 
						|
use think\facade\View;
 | 
						|
use think\facade\Cookie;
 | 
						|
use app\admin\model\Log\LogsLogin;
 | 
						|
use app\admin\model\Log\LogsOperation;
 | 
						|
 | 
						|
class LogController extends Base
 | 
						|
{
 | 
						|
    /**
 | 
						|
     * 登录日志列表
 | 
						|
     */
 | 
						|
    public function login()
 | 
						|
    {
 | 
						|
        if (Request::isPost()) {
 | 
						|
            $page = input('post.page', 1);
 | 
						|
            $limit = input('post.limit', 10);
 | 
						|
            $username = input('post.username');
 | 
						|
            $ip = input('post.ip');
 | 
						|
            $status = input('post.status');
 | 
						|
            $startTime = input('post.start_time');
 | 
						|
            $endTime = input('post.end_time');
 | 
						|
 | 
						|
            // 搜索条件
 | 
						|
            if ($username) {
 | 
						|
                $query = LogsLogin::where('username', 'like', "%{$username}%");
 | 
						|
            }
 | 
						|
            if ($ip) {
 | 
						|
                $query = LogsLogin::where('ip_address', 'like', "%{$ip}%");
 | 
						|
            }
 | 
						|
            if ($status !== '') {
 | 
						|
                $query = LogsLogin::where('login_status', $status);
 | 
						|
            }
 | 
						|
            if ($startTime) {
 | 
						|
                $query = LogsLogin::where('login_time', '>=', $startTime);
 | 
						|
            }
 | 
						|
            if ($endTime) {
 | 
						|
                $query = LogsLogin::where('login_time', '<=', $endTime);
 | 
						|
            }
 | 
						|
 | 
						|
            $count = LogsLogin::count();
 | 
						|
            $list = LogsLogin::order('id desc')
 | 
						|
                ->page($page, $limit)
 | 
						|
                ->select();
 | 
						|
 | 
						|
            return json([
 | 
						|
                'code' => 0,
 | 
						|
                'msg' => '获取成功',
 | 
						|
                'count' => $count,
 | 
						|
                'data' => $list
 | 
						|
            ]);
 | 
						|
        }
 | 
						|
 | 
						|
        return View::fetch();
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 操作日志列表
 | 
						|
     */
 | 
						|
    public function operation()
 | 
						|
    {
 | 
						|
        $params = input();
 | 
						|
        $page = $params['page'] ?? 1;
 | 
						|
        $limit = $params['limit'] ?? 10;
 | 
						|
        
 | 
						|
        // 构建搜索条件
 | 
						|
        $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' => '<=']
 | 
						|
        ];
 | 
						|
        
 | 
						|
        $query = LogsOperation::where('1=1');
 | 
						|
        
 | 
						|
        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);
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        $count = LogsOperation::where('1=1')->count();
 | 
						|
        $list = LogsOperation::where('1=1')
 | 
						|
            ->order('id desc')
 | 
						|
            ->page($page, $limit)
 | 
						|
            ->select();
 | 
						|
 | 
						|
        if (Request::isAjax()) {
 | 
						|
            return json([
 | 
						|
                'code' => 0,
 | 
						|
                'msg' => '获取成功',
 | 
						|
                'count' => $count,
 | 
						|
                'data' => $list
 | 
						|
            ]);
 | 
						|
        }
 | 
						|
 | 
						|
        return View::fetch();
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 记录操作日志
 | 
						|
     * @param string $operation 操作名称
 | 
						|
     * @param int $status 状态 1成功 0失败
 | 
						|
     * @param string $error_message 错误信息
 | 
						|
     * @param string $module 模块名称
 | 
						|
     */
 | 
						|
    public static function record($operation, $status = 1, $error_message = '', $module = '')
 | 
						|
    {
 | 
						|
        $data = [
 | 
						|
            'username' => Cookie::get('admin_name') ?: '未知用户',
 | 
						|
            'module' => $module ?: '系统管理',
 | 
						|
            'operation' => $operation,
 | 
						|
            'request_method' => Request::method(),
 | 
						|
            'request_url' => Request::url(true),
 | 
						|
            'request_params' => json_encode(Request::param(), JSON_UNESCAPED_UNICODE),
 | 
						|
            'ip_address' => Request::ip(),
 | 
						|
            'status' => $status,
 | 
						|
            'error_message' => $error_message,
 | 
						|
            'operation_time' => date('Y-m-d H:i:s'),
 | 
						|
            'execution_time' => 0
 | 
						|
        ];
 | 
						|
        
 | 
						|
        LogsOperation::insert($data);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 获取操作日志详情
 | 
						|
     */
 | 
						|
    public function getOperationDetail()
 | 
						|
    {
 | 
						|
        $id = input('id/d', 0);
 | 
						|
        if (!$id) {
 | 
						|
            return json(['code' => 1, 'msg' => '参数错误']);
 | 
						|
        }
 | 
						|
 | 
						|
        $info = LogsOperation::where('id', $id)
 | 
						|
            ->find();
 | 
						|
            
 | 
						|
        if (!$info) {
 | 
						|
            return json(['code' => 1, 'msg' => '日志不存在']);
 | 
						|
        }
 | 
						|
 | 
						|
        // 格式化请求参数
 | 
						|
        if (!empty($info['request_params'])) {
 | 
						|
            $info['request_params'] = json_decode($info['request_params'], true);
 | 
						|
        }
 | 
						|
 | 
						|
        return json(['code' => 0, 'msg' => '获取成功', 'data' => $info]);
 | 
						|
    }
 | 
						|
} 
 |