Log.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Copyright (C) 2018 Baidu, Inc. All Rights Reserved.
  4. */
  5. namespace app\admin\controller;
  6. use think\Db;
  7. class Log extends Admin
  8. {
  9. protected function _initialize()
  10. {
  11. parent::_initialize();
  12. }
  13. /**
  14. * 登录日志
  15. * @return mixedo
  16. */
  17. public function login()
  18. {
  19. $type = input('type', '1','intval');
  20. $condition = [];
  21. $username = $this->request->get('username', '', 'trim');
  22. !empty($username) && $condition['l.username'] = ['like', "%{$username}%"];
  23. $condition['l.type'] = $type;
  24. $loginlogModel = model('Common/loginlog');
  25. if($type==1){
  26. $loginlogList = $loginlogModel->alias('l')
  27. ->join('cy_admin a', 'a.id=l.admin_id', 'left')
  28. ->field('l.*, a.create_time time2')
  29. ->where($condition)->order('l.create_time desc')
  30. ->paginate(10, false, array('query' => input('get.')));
  31. }
  32. else{
  33. $loginlogList = $loginlogModel->alias('l')
  34. ->join('nw_channel_admin a', 'a.id=l.admin_id', 'left')
  35. ->field('l.*, a.create_time time2')
  36. ->where($condition)->order('l.create_time desc')
  37. ->paginate(10, false, array('query' => input('get.')));
  38. }
  39. $this->assign('list', $loginlogList);
  40. $this->assign('page', $loginlogList->render());
  41. return $this->fetch();
  42. }
  43. /**
  44. * 操作日志
  45. * @return mixedo
  46. */
  47. public function operate()
  48. {
  49. $condition = [];
  50. $parent_node_id = input('get.parent_node', 0, 'intval'); // 顶级菜单
  51. $child_node_id = input('get.child_node', 0, 'intval'); // 子菜单
  52. // $parent_node = config('operatelog.parent_node');
  53. // $child_node = config('operatelog.child_node');
  54. // ## 菜单数据 ##
  55. // 顶级菜单
  56. $parent_node = Db::table('nw_admin_auth_rule')->where(['parent_id' => 0, 'status' => 1])->column('id,name');
  57. $parent_node_ids = array_keys($parent_node);
  58. // 子菜单
  59. $child_node_list = Db::table('nw_admin_auth_rule')->where(['parent_id' => ['in', $parent_node_ids], 'status' => 1])->column('id,name,parent_id');
  60. $parent_node_parent_ids = [];
  61. $child_node = [];
  62. foreach ($child_node_list as $k => &$v) {
  63. $parent_node_parent_ids[$v['id']] = $v['parent_id'];
  64. $v['parent_name'] = $parent_node[ $v['parent_id'] ];
  65. $child_node[$v['parent_id']][$v['id']] = $v;
  66. }
  67. if (!empty($child_node_id)) {
  68. $condition['o.type'] = $child_node_id;
  69. } elseif (!empty($parent_node_id)) {
  70. // 选中顶级菜单时,过滤该顶级菜单下所有子菜单
  71. if (!empty($child_node[$parent_node_id])) {
  72. $condition['o.type'] = ['in', array_keys($child_node[$parent_node_id])];
  73. } else {
  74. $condition['o.type'] = -1; // 顶级菜单下无子菜单,查不出数据
  75. }
  76. }
  77. $username = $this->request->get('username', '', 'trim');
  78. !empty($username) && $condition['o.username'] = ['like', "%{$username}%"];
  79. $condition['a.type'] = input('type', '1');
  80. $operatelogModel = model('Common/operatelog');
  81. $loginlogList = $operatelogModel
  82. ->alias('o')
  83. ->join('cy_admin a', 'a.id=o.admin_id', 'left')
  84. ->field('o.*, a.type as type2')
  85. ->where($condition)->order('o.create_time desc')->paginate(10, false, array('query' => input('get.')));
  86. $data = $loginlogList->toArray()['data'];
  87. // 老菜单数据
  88. $child_node_old = config('operatelog.child_node');
  89. $parent_node_old = config('operatelog.parent_node');
  90. $parent_node_map = $child_node_map = [];
  91. foreach ($child_node_old as $k => $node) {
  92. foreach ($node as $id => $v) {
  93. $child_node_map[ $id ] = $v;
  94. $parent_node_map[ $id ] = $k;
  95. }
  96. }
  97. foreach ($data as $key => &$v) {
  98. if (!empty($parent_node[$v['type']])){
  99. $v['child_node_name'] = '-';
  100. $v['parent_node_name'] = $parent_node[$v['type']];
  101. } elseif (!empty($child_node[$v['type']])){
  102. $v['child_node_name'] = $child_node_list[$parent_node_parent_ids[$v['type']]]['name'];
  103. $v['parent_node_name'] = $child_node_list[$parent_node_parent_ids[$v['type']]]['parent_name'];
  104. } else {
  105. $v['child_node_name'] = isset($child_node_map[ $v['type'] ]) ? $child_node_map[ $v['type'] ] : '';
  106. $v['parent_node_name'] = isset($parent_node_map[ $v['type'] ]) ? $parent_node_old[ $parent_node_map[ $v['type'] ] ] : '';
  107. }
  108. }
  109. $this->assign('list', $data);
  110. $this->assign('total', $loginlogList->total()); //总记录数
  111. $this->assign('parent_node', $parent_node);
  112. $this->assign('parent_node_id', $parent_node_id);
  113. $this->assign('child_node_id', $child_node_id);
  114. $this->assign('page', $loginlogList->render());
  115. return $this->fetch();
  116. }
  117. public function getChildNode()
  118. {
  119. $parent_id = input('get.parent_id', 0, 'intval');
  120. if (empty($parent_id)) {
  121. $this->success('ok', '', []);
  122. }
  123. // 子菜单
  124. $child_node_list = Db::table('nw_admin_auth_rule')->where(['parent_id' => $parent_id, 'status' => 1])->column('id,name');
  125. $this->success('ok', '', $child_node_list);
  126. }
  127. }