SystemLogServices.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\system\log;
  12. use app\dao\system\log\SystemLogDao;
  13. use app\services\BaseServices;
  14. use app\services\system\admin\SystemAdminServices;
  15. use app\services\system\SystemMenusServices;
  16. /**
  17. * 系统日志
  18. * Class SystemLogServices
  19. * @package app\services\system\log
  20. * @method deleteLog() 定期删除日志
  21. */
  22. class SystemLogServices extends BaseServices
  23. {
  24. /**
  25. * 构造方法
  26. * SystemLogServices constructor.
  27. * @param SystemLogDao $dao
  28. */
  29. public function __construct(SystemLogDao $dao)
  30. {
  31. $this->dao = $dao;
  32. }
  33. /**
  34. * 记录访问日志
  35. * @param int $adminId
  36. * @param string $adminName
  37. * @param string $type
  38. * @return bool
  39. */
  40. public function recordAdminLog(int $adminId, string $adminName, string $type)
  41. {
  42. $request = app()->request;
  43. $module = app('http')->getName();
  44. $rule = trim(strtolower($request->rule()->getRule()));
  45. /** @var SystemMenusServices $service */
  46. $service = app()->make(SystemMenusServices::class);
  47. $data = [
  48. 'method' => $module,
  49. 'admin_id' => $adminId,
  50. 'add_time' => time(),
  51. 'admin_name' => $adminName,
  52. 'path' => $rule,
  53. 'page' => $service->getVisitName($rule) ?: '未知',
  54. 'ip' => $request->ip(),
  55. 'type' => $type
  56. ];
  57. if ($this->dao->save($data)) {
  58. return true;
  59. } else {
  60. return false;
  61. }
  62. }
  63. /**
  64. * 获取系统日志列表
  65. * @param array $where
  66. * @return array
  67. * @throws \think\db\exception\DataNotFoundException
  68. * @throws \think\db\exception\DbException
  69. * @throws \think\db\exception\ModelNotFoundException
  70. */
  71. public function getLogList(array $where, int $level)
  72. {
  73. [$page, $limit] = $this->getPageValue();
  74. if (!$where['admin_id']) {
  75. /** @var SystemAdminServices $service */
  76. $service = app()->make(SystemAdminServices::class);
  77. $where['admin_id'] = $service->getAdminIds($level);
  78. }
  79. $list = $this->dao->getLogList($where, $page, $limit);
  80. $count = $this->dao->count($where);
  81. return compact('list', 'count');
  82. }
  83. }