SystemLogServices.php 3.1 KB

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