Oplog.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2022 广州楚才信息科技有限公司 [ 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 Ip2Region;
  18. use think\admin\Controller;
  19. use think\admin\helper\QueryHelper;
  20. use think\admin\model\SystemOplog;
  21. use think\exception\HttpResponseException;
  22. /**
  23. * 系统日志管理
  24. * Class Oplog
  25. * @package app\admin\controller
  26. */
  27. class Oplog extends Controller
  28. {
  29. /**
  30. * 系统日志管理
  31. * @auth true
  32. * @menu true
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. */
  37. public function index()
  38. {
  39. SystemOplog::mQuery()->layTable(function () {
  40. $this->title = '系统日志管理';
  41. $columns = SystemOplog::mk()->column('action,username', 'id');
  42. $this->users = array_unique(array_column($columns, 'username'));
  43. $this->actions = array_unique(array_column($columns, 'action'));
  44. }, function (QueryHelper $query) {
  45. $query->dateBetween('create_at')->equal('username,action')->like('content,geoip,node');
  46. });
  47. }
  48. /**
  49. * 列表数据处理
  50. * @auth true
  51. * @param array $data
  52. * @throws \Exception
  53. */
  54. protected function _index_page_filter(array &$data)
  55. {
  56. $region = new Ip2Region();
  57. foreach ($data as &$vo) {
  58. $isp = $region->btreeSearch($vo['geoip']);
  59. $vo['geoisp'] = str_replace(['内网IP', '0', '|'], '', $isp['region'] ?? '') ?: '-';
  60. }
  61. }
  62. /**
  63. * 清理系统日志
  64. * @auth true
  65. */
  66. public function clear()
  67. {
  68. try {
  69. SystemOplog::mQuery()->empty();
  70. sysoplog('系统运维管理', '成功清理所有日志');
  71. $this->success('日志清理成功!');
  72. } catch (HttpResponseException $exception) {
  73. throw $exception;
  74. } catch (Exception $exception) {
  75. $this->error("日志清理失败,{$exception->getMessage()}");
  76. }
  77. }
  78. /**
  79. * 删除系统日志
  80. * @auth true
  81. */
  82. public function remove()
  83. {
  84. SystemOplog::mDelete();
  85. }
  86. }