SystemLogService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace app\admin\service;
  3. use think\facade\Cache;
  4. use think\facade\Db;
  5. use think\facade\Config;
  6. use think\facade\Env;
  7. /**
  8. * 系统日志表
  9. * Class SystemLogService
  10. * @package app\admin\service
  11. */
  12. class SystemLogService
  13. {
  14. protected static ?SystemLogService $instance = null;
  15. /**
  16. * 表前缀
  17. * @var string
  18. */
  19. protected string $tablePrefix;
  20. /**
  21. * 表后缀
  22. * @var string
  23. */
  24. protected string $tableSuffix;
  25. /**
  26. * 表名
  27. * @var string
  28. */
  29. protected string $tableName;
  30. /**
  31. * 构造方法
  32. * SystemLogService constructor.
  33. */
  34. protected function __construct()
  35. {
  36. $this->tablePrefix = Config::get('database.connections.mysql.prefix');
  37. $this->tableSuffix = date('Ym', time());
  38. $this->tableName = "{$this->tablePrefix}system_log_{$this->tableSuffix}";
  39. }
  40. /**
  41. * 获取实例对象
  42. * @return SystemLogService
  43. */
  44. public static function instance(): SystemLogService
  45. {
  46. if (is_null(self::$instance)) {
  47. self::$instance = new static();
  48. }
  49. return self::$instance;
  50. }
  51. /**
  52. * 保存数据
  53. * @param $data
  54. * @return bool|string
  55. */
  56. public function save($data): bool|string
  57. {
  58. Db::startTrans();
  59. try {
  60. $this->detectTable();
  61. Db::table($this->tableName)->strict(false)->insert($data);
  62. Db::commit();
  63. }catch (\Exception $e) {
  64. Db::rollback();
  65. return $e->getMessage();
  66. }
  67. return true;
  68. }
  69. /**
  70. * 检测数据表
  71. * @return bool
  72. */
  73. public function detectTable(): bool
  74. {
  75. $_key = "system_log_{$this->tableName}_table";
  76. // 手动删除日志表时候 记得清除缓存
  77. $isset = Cache::get($_key);
  78. if ($isset) return true;
  79. $check = Db::query("show tables like '{$this->tableName}'");
  80. if (empty($check)) {
  81. $sql = $this->getCreateSql();
  82. Db::execute($sql);
  83. }
  84. Cache::set($_key, !empty($check));
  85. return true;
  86. }
  87. public function getAllTableList()
  88. {
  89. }
  90. /**
  91. * 根据后缀获取创建表的sql
  92. * @return string
  93. */
  94. protected function getCreateSql(): string
  95. {
  96. return <<<EOT
  97. CREATE TABLE `{$this->tableName}` (
  98. `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
  99. `admin_id` int(10) unsigned DEFAULT '0' COMMENT '管理员ID',
  100. `url` varchar(1500) NOT NULL DEFAULT '' COMMENT '操作页面',
  101. `method` varchar(50) NOT NULL COMMENT '请求方法',
  102. `title` varchar(100) DEFAULT '' COMMENT '日志标题',
  103. `content` json NOT NULL COMMENT '请求数据',
  104. `response` json DEFAULT NULL COMMENT '回调数据',
  105. `ip` varchar(50) NOT NULL DEFAULT '' COMMENT 'IP',
  106. `useragent` varchar(255) DEFAULT '' COMMENT 'User-Agent',
  107. `create_time` int(10) DEFAULT NULL COMMENT '操作时间',
  108. PRIMARY KEY (`id`)
  109. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=COMPACT COMMENT='后台操作日志表 - {$this->tableSuffix}';
  110. EOT;
  111. }
  112. }