SystemLogServices.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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. * @mixin SystemLogDao
  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 $module
  38. * @param string $rule
  39. * @param string $ip
  40. * @param string $type
  41. * @return bool
  42. */
  43. public function recordAdminLog(int $adminId, string $adminName, string $module, string $rule, string $ip, string $type)
  44. {
  45. /** @var SystemMenusServices $service */
  46. $service = app()->make(SystemMenusServices::class);
  47. $data = [
  48. 'method' => $module,
  49. 'add_time' => time(),
  50. 'admin_name' => $adminName,
  51. 'path' => $rule,
  52. 'page' => $service->getVisitName($rule) ?: '未知',
  53. 'ip' => $ip,
  54. 'type' => $type
  55. ];
  56. if ($type == 'store') {
  57. $data['store_id'] = $adminId;
  58. } else {
  59. $data['admin_id'] = $adminId;
  60. }
  61. if ($this->dao->save($data)) {
  62. return true;
  63. } else {
  64. return false;
  65. }
  66. }
  67. /**
  68. * 获取系统日志列表
  69. * @param array $where
  70. * @return array
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\DbException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. */
  75. public function getLogList(array $where, int $level)
  76. {
  77. [$page, $limit] = $this->getPageValue();
  78. if (!$where['admin_id']) {
  79. /** @var SystemAdminServices $service */
  80. $service = app()->make(SystemAdminServices::class);
  81. $where['admin_id'] = $service->getAdminIds($level);
  82. }
  83. $list = $this->dao->getLogList($where, $page, $limit);
  84. $count = $this->dao->count($where);
  85. return compact('list', 'count');
  86. }
  87. }