AdminLogRepository.php 3.0 KB

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