SystemLogServices.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. use app\services\system\SystemRouteServices;
  17. /**
  18. * 系统日志
  19. * Class SystemLogServices
  20. * @package app\services\system\log
  21. * @method deleteLog() 定期删除日志
  22. */
  23. class SystemLogServices extends BaseServices
  24. {
  25. /**
  26. * 构造方法
  27. * SystemLogServices constructor.
  28. * @param SystemLogDao $dao
  29. */
  30. public function __construct(SystemLogDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. /**
  35. * 记录访问日志
  36. * @param int $adminId
  37. * @param string $adminName
  38. * @param string $type
  39. * @return bool
  40. */
  41. public function recordAdminLog(int $adminId, string $adminName, string $type)
  42. {
  43. $request = app()->request;
  44. $module = app('http')->getName();
  45. $rule = trim(strtolower($request->rule()->getRule()));
  46. /** @var SystemMenusServices $service */
  47. $service = app()->make(SystemMenusServices::class);
  48. $data = [
  49. 'method' => $module,
  50. 'admin_id' => $adminId,
  51. 'add_time' => time(),
  52. 'admin_name' => $adminName,
  53. 'path' => $rule,
  54. 'page' => $service->getVisitName($rule) ?: '未知',
  55. 'ip' => $request->ip(),
  56. 'type' => $type
  57. ];
  58. if ($this->dao->save($data)) {
  59. return true;
  60. } else {
  61. return false;
  62. }
  63. }
  64. /**
  65. * 获取系统日志列表
  66. * @param array $where
  67. * @return array
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. */
  72. public function getLogList(array $where, int $level)
  73. {
  74. [$page, $limit] = $this->getPageValue();
  75. if (!$where['admin_id']) {
  76. /** @var SystemAdminServices $service */
  77. $service = app()->make(SystemAdminServices::class);
  78. $where['admin_id'] = $service->getAdminIds($level);
  79. }
  80. $routeArr = app()->make(SystemRouteServices::class)->getNameList('adminapi');
  81. $list = $this->dao->getLogList($where, $page, $limit);
  82. $count = $this->dao->count($where);
  83. foreach($list as &$item){
  84. $item['path_name'] = $routeArr[$item['path']] ?? '未知';
  85. }
  86. return compact('list', 'count');
  87. }
  88. }