SystemLogService.php 3.2 KB

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