AdminLog.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * lemocms
  4. * ============================================================================
  5. * 版权所有 2018-2027 lemocms,并保留所有权利。
  6. * 网站地址: https://www.lemocms.com
  7. * ----------------------------------------------------------------------------
  8. * 采用最新Thinkphp6实现
  9. * ============================================================================
  10. * Author: yuege
  11. * Date: 2019/8/2
  12. */
  13. namespace app\admin\model;
  14. use lemo\helper\DataHelper;
  15. use think\facade\Request;
  16. use think\facade\Session;
  17. use think\facade\Db;
  18. use think\facade\Route;
  19. use app\admin\model\AuthRule;
  20. class AdminLog extends AdminModel
  21. {
  22. protected static $log_title = '';
  23. //自定义日志内容
  24. protected static $log_content = '';
  25. protected static $log_url = '';
  26. /*
  27. * 管理员日志记录
  28. */
  29. public static function record()
  30. {
  31. //入库信息
  32. $admin_id = Session::get('admin.id',0);
  33. $username = Session::get('admin.username','Unknown');
  34. $url = Request::url();
  35. $title = self::$log_title;
  36. $ip = Request::ip();
  37. $agent = Request::server('HTTP_USER_AGENT');
  38. $content = Request::param();
  39. if(stripos($url,'?')){
  40. $url = explode('?',$url)[0];
  41. $url = strtolower($url);
  42. }
  43. if ($content) {
  44. //去除登录密码
  45. foreach ($content as $k => $v) {
  46. if (stripos($k, 'password') !== false) {
  47. unset($content[$k]);
  48. }
  49. }
  50. $content = json_encode($content);
  51. }elseif (!$content && Request::isGet()){
  52. $content = '点击菜单';
  53. }elseif (!$content && Request::isPost()){
  54. $content = '清除缓存|切换语言';
  55. }
  56. //登录处理
  57. if (strpos($url, 'login/index') !== false && Request::isPost()) {
  58. $title = '[登录成功]';
  59. }else{
  60. //权限
  61. $auth = AuthRule::column('href','href');
  62. foreach ($auth as $k=>$v){
  63. $auth[$k] = strtolower((string)url($v));
  64. }
  65. $key = array_search($url,$auth);
  66. if($key){
  67. $auth = AuthRule::where('href',$key)->find();
  68. if($auth) $title=$auth->title;
  69. }
  70. }
  71. //插入数据
  72. if (!empty($title)) {
  73. self::create([
  74. 'log_title' => $title ? $title : '',
  75. 'log_content' => $content,
  76. 'log_url' => $url,
  77. 'admin_id' => $admin_id,
  78. 'username' => $username,
  79. 'log_agent' => $agent,
  80. 'log_ip' => $ip,
  81. ]);
  82. }
  83. }
  84. }