LogService.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/Think.Admin
  12. // +----------------------------------------------------------------------
  13. namespace service;
  14. use think\Db;
  15. use think\Request;
  16. /**
  17. * 操作日志服务
  18. * Class LogService
  19. * @package service
  20. * @author Anyon <zoujingli@qq.com>
  21. * @date 2017/03/24 13:25
  22. */
  23. class LogService
  24. {
  25. /**
  26. * 获取数据操作对象
  27. * @return \think\db\Query
  28. */
  29. protected static function db()
  30. {
  31. return Db::name('SystemLog');
  32. }
  33. /**
  34. * 写入操作日志
  35. * @param string $action
  36. * @param string $content
  37. * @return bool
  38. */
  39. public static function write($action = '行为', $content = "内容描述")
  40. {
  41. $request = Request::instance();
  42. $node = strtolower(join('/', [$request->module(), $request->controller(), $request->action()]));
  43. $data = ['ip' => $request->ip(), 'node' => $node, 'username' => session('user.username') . '', 'action' => $action, 'content' => $content];
  44. return self::db()->insert($data) !== false;
  45. }
  46. }