AdminLogRepository.php 2.4 KB

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