新增log日志导出

This commit is contained in:
wolfcode
2023-09-15 23:31:14 +08:00
parent a6ebe52d20
commit 5b085c4cb4
2 changed files with 41 additions and 2 deletions

View File

@@ -3,12 +3,16 @@
namespace app\admin\controller\system;
use app\admin\model\SystemLog;
use app\admin\service\tool\CommonTool;
use app\admin\traits\Curd;
use app\common\controller\AdminController;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnnotation;
use jianyan\excel\Excel;
use think\App;
use think\db\exception\DbException;
use think\db\exception\PDOException;
use think\facade\Db;
/**
* @ControllerAnnotation(title="操作日志管理")
@@ -17,6 +21,7 @@ use think\db\exception\PDOException;
*/
class Log extends AdminController
{
use Curd;
public function __construct(App $app)
{
@@ -39,7 +44,7 @@ class Log extends AdminController
try {
$count = $model->count();
$list = $model->page($page, $limit)->order($this->sort)->select();
} catch (PDOException | DbException $exception) {
} catch (PDOException|DbException $exception) {
$count = 0;
$list = [];
}
@@ -54,4 +59,37 @@ class Log extends AdminController
return $this->fetch();
}
/**
* @NodeAnnotation(title="导出")
*/
public function export()
{
[$page, $limit, $where, $excludeFields] = $this->buildTableParams(['month']);
$tableName = $this->model->getName();
$tableName = CommonTool::humpToLine(lcfirst($tableName));
$prefix = config('database.connections.mysql.prefix');
$dbList = Db::query("show full columns from {$prefix}{$tableName}");
$header = [];
foreach ($dbList as $vo) {
$comment = !empty($vo['Comment']) ? $vo['Comment'] : $vo['Field'];
if (!in_array($vo['Field'], $this->noExportFields)) {
$header[] = [$comment, $vo['Field']];
}
}
$month = !empty($excludeFields['month']) ? date('Ym', strtotime($excludeFields['month'])) : date('Ym');
$model = $this->model->setMonth($month)->with('admin')->where($where);
try {
$list = $model
->where($where)
->limit(100000)
->order('id', 'desc')
->select()
->toArray();
} catch (PDOException|DbException $exception) {
$this->error($exception->getMessage());
}
$fileName = time();
return Excel::exportData($list, $header, $fileName, 'xlsx');
}
}

View File

@@ -5,6 +5,7 @@ define(["jquery", "easy-admin"], function ($, ea) {
table_elem: '#currentTable',
table_render_id: 'currentTableRenderId',
index_url: 'system.log/index',
export_url: 'system.log/export',
};
var Controller = {
@@ -12,7 +13,7 @@ define(["jquery", "easy-admin"], function ($, ea) {
var util = layui.util;
ea.table.render({
init: init,
toolbar: ['refresh'],
toolbar: ['refresh', 'export'],
cols: [[
{field: 'id', width: 80, title: 'ID', search: false},
{field: 'month', title: '日志月份', hide: true, search: 'time', timeType: 'month', searchValue: util.toDateString(new Date(), 'yyyy-MM')},