Log.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace app\admin\controller\system;
  3. use app\admin\model\SystemLog;
  4. use app\admin\service\annotation\MiddlewareAnnotation;
  5. use app\admin\service\tool\CommonTool;
  6. use app\common\controller\AdminController;
  7. use app\admin\service\annotation\ControllerAnnotation;
  8. use app\admin\service\annotation\NodeAnnotation;
  9. use app\Request;
  10. use jianyan\excel\Excel;
  11. use think\App;
  12. use think\db\exception\DbException;
  13. use think\db\exception\PDOException;
  14. use think\facade\Db;
  15. use think\response\Json;
  16. #[ControllerAnnotation(title: '操作日志管理')]
  17. class Log extends AdminController
  18. {
  19. public function __construct(App $app)
  20. {
  21. parent::__construct($app);
  22. self::$model = SystemLog::class;
  23. }
  24. #[NodeAnnotation(title: '列表', auth: true)]
  25. public function index(Request $request): Json|string
  26. {
  27. if ($request->isAjax()) {
  28. if (input('selectFields')) {
  29. return $this->selectList();
  30. }
  31. [$page, $limit, $where, $excludeFields] = $this->buildTableParams(['month']);
  32. $month = !empty($excludeFields['month']) ? date('Ym', strtotime($excludeFields['month'])) : date('Ym');
  33. $model = (new self::$model)->setSuffix("_$month")->with('admin')->where($where);
  34. try {
  35. $count = $model->count();
  36. $list = $model->page($page, $limit)->order($this->sort)->select();
  37. }catch (PDOException|DbException $exception) {
  38. $count = 0;
  39. $list = [];
  40. }
  41. $data = [
  42. 'code' => 0,
  43. 'msg' => '',
  44. 'count' => $count,
  45. 'data' => $list,
  46. ];
  47. return json($data);
  48. }
  49. return $this->fetch();
  50. }
  51. #[NodeAnnotation(title: '导出', auth: true)]
  52. public function export()
  53. {
  54. if (env('EASYADMIN.IS_DEMO', false)) {
  55. $this->error('演示环境下不允许操作');
  56. }
  57. [$page, $limit, $where, $excludeFields] = $this->buildTableParams(['month']);
  58. $month = !empty($excludeFields['month']) ? date('Ym', strtotime($excludeFields['month'])) : date('Ym');
  59. $tableName = (new self::$model)->setSuffix("_$month")->getName();
  60. $tableName = CommonTool::humpToLine(lcfirst($tableName));
  61. $prefix = config('database.connections.mysql.prefix');
  62. $dbList = Db::query("show full columns from {$prefix}{$tableName}");
  63. $header = [];
  64. foreach ($dbList as $vo) {
  65. $comment = !empty($vo['Comment']) ? $vo['Comment'] : $vo['Field'];
  66. if (!in_array($vo['Field'], $this->noExportFields)) {
  67. $header[] = [$comment, $vo['Field']];
  68. }
  69. }
  70. $model = (new self::$model)->setSuffix("_$month")->with('admin')->where($where);
  71. try {
  72. $list = $model
  73. ->limit(10000)
  74. ->order('id', 'desc')
  75. ->select()
  76. ->toArray();
  77. foreach ($list as &$vo) {
  78. $vo['content'] = json_encode($vo['content'], JSON_UNESCAPED_UNICODE);
  79. $vo['response'] = json_encode($vo['response'], JSON_UNESCAPED_UNICODE);
  80. }
  81. exportExcel($header, $list, '操作日志');
  82. }catch (\Throwable $exception) {
  83. $this->error($exception->getMessage());
  84. }
  85. }
  86. #[NodeAnnotation(title: '删除指定日志', auth: true)]
  87. public function deleteMonthLog(Request $request)
  88. {
  89. if (!$request->isAjax()) {
  90. return $this->fetch();
  91. }
  92. if ($this->isDemo) $this->error('演示环境下不允许操作');
  93. $monthsAgo = $request->param('month/d', 0);
  94. if ($monthsAgo < 1) $this->error('月份错误');
  95. $currentDate = new \DateTime();
  96. $currentDate->modify("-$monthsAgo months");
  97. $dbPrefix = env('DB_PREFIX');
  98. $dbLike = "{$dbPrefix}system_log_";
  99. $tables = Db::query("SHOW TABLES LIKE '$dbLike%'");
  100. $threshold = date('Ym', strtotime("-$monthsAgo month"));
  101. $tableNames = [];
  102. try {
  103. foreach ($tables as $table) {
  104. $tableName = current($table);
  105. if (!preg_match("/^$dbLike\d{6}$/", $tableName)) continue;
  106. $datePart = substr($tableName, -6);
  107. $issetTable = Db::query("SHOW TABLES LIKE '$tableName'");
  108. if (!$issetTable) continue;
  109. if ($datePart - $threshold <= 0) {
  110. Db::execute("DROP TABLE `$tableName`");
  111. $tableNames[] = $tableName;
  112. }
  113. }
  114. }catch (PDOException) {
  115. }
  116. if (empty($tableNames)) $this->error('没有需要删除的表');
  117. $this->success('操作成功 - 共删除 ' . count($tableNames) . ' 张表<br/>' . implode('<br>', $tableNames));
  118. }
  119. #[MiddlewareAnnotation(ignore: MiddlewareAnnotation::IGNORE_LOG)]
  120. #[NodeAnnotation(title: '框架日志', auth: true, ignore: NodeAnnotation::IGNORE_NODE)]
  121. public function record(): Json|string
  122. {
  123. return (new \Wolfcode\PhpLogviewer\thinkphp\LogViewer())->fetch();
  124. }
  125. }