AdminLogRepository.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace app\common\repositories\system\admin;
  3. use app\common\dao\BaseDao;
  4. use app\common\dao\system\admin\LogDao;
  5. use app\common\repositories\BaseRepository;
  6. use app\Request;
  7. use think\db\exception\DataNotFoundException;
  8. use think\db\exception\DbException;
  9. use think\db\exception\ModelNotFoundException;
  10. use think\Model;
  11. /**
  12. * Class AdminLogRepository
  13. * @package app\common\repositories\system\admin
  14. * @author zfy
  15. * @day 2020-04-16
  16. */
  17. class AdminLogRepository extends BaseRepository
  18. {
  19. /**
  20. * AdminLogRepository constructor.
  21. * @param LogDao $dao
  22. */
  23. public function __construct(LogDao $dao)
  24. {
  25. $this->dao = $dao;
  26. }
  27. /**
  28. * @param $merId
  29. * @param array $where
  30. * @param $page
  31. * @param $limit
  32. * @return array
  33. * @throws DataNotFoundException
  34. * @throws DbException
  35. * @throws ModelNotFoundException
  36. * @author zfy
  37. * @day 2020-04-16
  38. */
  39. public function lst($merId, array $where, $page, $limit)
  40. {
  41. $query = $this->dao->search($where, $merId);
  42. $count = $query->count($this->dao->getPk());
  43. $list = $query->setOption('field', [])->field(['create_time', 'log_id', 'admin_name', 'route', 'method', 'url', 'ip', 'admin_id'])
  44. ->page($page, $limit)->order('create_time DESC')->select();
  45. return compact('count', 'list');
  46. }
  47. /**
  48. * @param Request $request
  49. * @param int $merId
  50. * @return BaseDao|Model
  51. * @author zfy
  52. * @day 2020-04-15
  53. */
  54. public function addLog(Request $request, int $merId = 0)
  55. {
  56. return $this->create($merId, self::parse($request));
  57. }
  58. /**
  59. * @param int $merId
  60. * @param array $data
  61. * @return BaseDao|Model
  62. * @author zfy
  63. * @day 2020-04-16
  64. */
  65. public function create(int $merId, array $data)
  66. {
  67. $data['mer_id'] = $merId;
  68. return $this->dao->create($data);
  69. }
  70. /**
  71. * @param Request $request
  72. * @return array
  73. * @author zfy
  74. * @day 2020-04-16
  75. */
  76. public static function parse(Request $request)
  77. {
  78. return [
  79. 'admin_id' => $request->adminId(),
  80. 'admin_name' => $request->adminInfo()->real_name ?: '未定义',
  81. 'route' => $request->rule()->getName(),
  82. 'ip' => $request->ip(),
  83. 'url' => $request->url(true),
  84. 'method' => $request->method()
  85. ];
  86. }
  87. }