Oplog.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace app\admin\controller;
  3. use Exception;
  4. use think\admin\Controller;
  5. use think\admin\helper\QueryHelper;
  6. use think\admin\service\AdminService;
  7. use think\exception\HttpResponseException;
  8. /**
  9. * 系统日志管理
  10. * Class Oplog
  11. * @package app\admin\controller
  12. */
  13. class Oplog extends Controller
  14. {
  15. /**
  16. * 绑定数据表
  17. * @var string
  18. */
  19. private $table = 'SystemOplog';
  20. /**
  21. * 系统日志管理
  22. * @auth true
  23. * @menu true
  24. * @throws \think\db\exception\DataNotFoundException
  25. * @throws \think\db\exception\DbException
  26. * @throws \think\db\exception\ModelNotFoundException
  27. */
  28. public function index()
  29. {
  30. $this->_query($this->table)->layTable(function () {
  31. $this->title = '系统日志管理';
  32. $this->isSupper = AdminService::instance()->isSuper();
  33. // 读取数据类型
  34. $this->users = $this->app->db->name($this->table)->distinct(true)->column('username');
  35. $this->actions = $this->app->db->name($this->table)->distinct(true)->column('action');
  36. }, function (QueryHelper $query) {
  37. // 数据列表处理
  38. $query->dateBetween('create_at')->equal('username,action')->like('content,geoip,node');
  39. });
  40. }
  41. /**
  42. * 列表数据处理
  43. * @auth true
  44. * @param array $data
  45. * @throws Exception
  46. */
  47. protected function _index_page_filter(array &$data)
  48. {
  49. $region = new \Ip2Region();
  50. foreach ($data as &$vo) {
  51. $isp = $region->btreeSearch($vo['geoip']);
  52. $vo['geoisp'] = str_replace(['内网IP', '0', '|'], '', $isp['region'] ?? '') ?: '-';
  53. $vo['create_at'] = format_datetime($vo['create_at']);
  54. }
  55. }
  56. /**
  57. * 清理系统日志
  58. * @auth true
  59. */
  60. public function clear()
  61. {
  62. try {
  63. $this->_query($this->table)->empty();
  64. sysoplog('系统运维管理', '成功清理所有日志数据');
  65. $this->success('日志清理成功!');
  66. } catch (HttpResponseException $exception) {
  67. throw $exception;
  68. } catch (Exception $exception) {
  69. $this->error("日志清理失败,{$exception->getMessage()}");
  70. }
  71. }
  72. /**
  73. * 删除系统日志
  74. * @auth true
  75. * @throws \think\db\exception\DbException
  76. */
  77. public function remove()
  78. {
  79. $this->_delete($this->table);
  80. }
  81. }