Oplog.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2021 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免费声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  13. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  14. // +----------------------------------------------------------------------
  15. namespace app\admin\controller;
  16. use Exception;
  17. use think\admin\Controller;
  18. use think\admin\helper\QueryHelper;
  19. use think\admin\service\AdminService;
  20. use think\exception\HttpResponseException;
  21. /**
  22. * 系统日志管理
  23. * Class Oplog
  24. * @package app\admin\controller
  25. */
  26. class Oplog extends Controller
  27. {
  28. /**
  29. * 绑定数据表
  30. * @var string
  31. */
  32. private $table = 'SystemOplog';
  33. /**
  34. * 系统日志管理
  35. * @auth true
  36. * @menu true
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public function index()
  42. {
  43. $this->_query($this->table)->layTable(function () {
  44. $this->title = '系统日志管理';
  45. $this->isSupper = AdminService::instance()->isSuper();
  46. // 读取数据类型
  47. $this->users = $this->app->db->name($this->table)->distinct(true)->column('username');
  48. $this->actions = $this->app->db->name($this->table)->distinct(true)->column('action');
  49. }, function (QueryHelper $query) {
  50. // 数据列表处理
  51. $query->dateBetween('create_at')->equal('username,action')->like('content,geoip,node');
  52. });
  53. }
  54. /**
  55. * 列表数据处理
  56. * @auth true
  57. * @param array $data
  58. * @throws Exception
  59. */
  60. protected function _index_page_filter(array &$data)
  61. {
  62. $region = new \Ip2Region();
  63. foreach ($data as &$vo) {
  64. $isp = $region->btreeSearch($vo['geoip']);
  65. $vo['geoisp'] = str_replace(['内网IP', '0', '|'], '', $isp['region'] ?? '') ?: '-';
  66. $vo['create_at'] = format_datetime($vo['create_at']);
  67. }
  68. }
  69. /**
  70. * 清理系统日志
  71. * @auth true
  72. */
  73. public function clear()
  74. {
  75. try {
  76. $this->_query($this->table)->empty();
  77. sysoplog('系统运维管理', '成功清理所有日志数据');
  78. $this->success('日志清理成功!');
  79. } catch (HttpResponseException $exception) {
  80. throw $exception;
  81. } catch (Exception $exception) {
  82. $this->error("日志清理失败,{$exception->getMessage()}");
  83. }
  84. }
  85. /**
  86. * 删除系统日志
  87. * @auth true
  88. * @throws \think\db\exception\DbException
  89. */
  90. public function remove()
  91. {
  92. $this->_delete($this->table);
  93. }
  94. }