Menu.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\MenuModel;
  4. use think\Db;
  5. class Menu extends Base
  6. {
  7. /**
  8. * [index 菜单列表]
  9. * @return [type] [description]
  10. * @author
  11. */
  12. public function index()
  13. {
  14. if(request()->isAjax ()){
  15. extract(input());
  16. $map = [];
  17. if(isset($key)&&$key!="")
  18. {
  19. $map['title'] = ['like',"%" . $key . "%"];
  20. }
  21. if(isset($start)&&$start!=""&&isset($end)&&$end=="")
  22. {
  23. $map['create_time'] = ['>= time',$start];
  24. }
  25. if(isset($end)&&$end!=""&&isset($start)&&$start=="")
  26. {
  27. $map['create_time'] = ['<= time',$end];
  28. }
  29. if(isset($start)&&$start!=""&&isset($end)&&$end!="")
  30. {
  31. $map['create_time'] = ['between time',[$start,$end]];
  32. }
  33. $nav = new \org\Leftnav;
  34. $menu = new MenuModel();
  35. $Nowpage = 1;
  36. $limits = 1000;
  37. $count = Db::name('auth_rule')->where($map)->count();//计算总页面
  38. $admin_rule = $menu->getMenus($map, $Nowpage, $limits);
  39. foreach($admin_rule as $key=>$vo){
  40. $admin_rule[$key]['placeholder'] = '';
  41. }
  42. $nav->init($admin_rule);
  43. $lists = $nav->get_tree(0);
  44. return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
  45. }
  46. return $this->fetch('menu/index');
  47. }
  48. /**
  49. * [add_rule 添加菜单]
  50. * @return [type] [description]
  51. * @author
  52. */
  53. public function add_rule()
  54. {
  55. if(request()->isPost()){
  56. extract(input());
  57. $pid = trim_explode(',',$pid);
  58. $title = trim_explode(',',$title);
  59. $name = trim_explode(',',$name);
  60. $css = trim_explode(',',$css);
  61. $sort = trim_explode(',',$sort);
  62. $menu = new MenuModel();
  63. $flag = $menu->insertMenu($pid,$title,$name,$css,$sort,$status);
  64. return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
  65. }
  66. $id = input('id');
  67. $nav = new \org\Leftnav;
  68. $menu = new MenuModel();
  69. $map = [];
  70. $admin_rule = $menu->getAllMenu($map);
  71. $nav->init($admin_rule);
  72. $lists = $nav->get_tree(0);
  73. $this->assign ([
  74. 'admin_rule'=>$lists,
  75. 'id'=>$id
  76. ]);
  77. return $this->fetch();
  78. }
  79. /**
  80. * [edit_rule 编辑菜单]
  81. * @return [type] [description]
  82. * @author
  83. */
  84. public function edit_rule()
  85. {
  86. $nav = new \org\Leftnav;
  87. $menu = new MenuModel();
  88. if(request()->isPost()){
  89. $param = input('param.');
  90. // $param['name'] = strtolower($param['name']);
  91. $flag = $menu->editMenu($param);
  92. return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
  93. }
  94. $id = input('id');
  95. $map = [];
  96. $admin_rule = $menu->getAllMenu($map);
  97. $nav->init($admin_rule);
  98. $arr = $nav->get_tree(0);
  99. // $arr = $nav::rule($admin_rule);
  100. $this->assign ('admin_rule',$arr);
  101. $this->assign('menu',$menu->getOneMenu($id));
  102. return $this->fetch();
  103. }
  104. //根据节点查询出所有子节点
  105. static public $childNode=array();//存放父节点和父节点下面的子节点
  106. public function findArrayNode($id,$list){
  107. foreach ($list as $key => $val){
  108. if ($id==$val['pid']){
  109. self::$childNode[]=(int)$val['id'];
  110. self::findArrayNode($val['id'], $list); //递归,传入新节点ID
  111. }
  112. }
  113. }
  114. /**
  115. * [del_menu 删除菜单]
  116. * @return [type] [description]
  117. * @author
  118. */
  119. public function del_menu()
  120. {
  121. $tid = input('param.id');
  122. $allTree = Db::name('auth_rule')
  123. ->field('id,pid')
  124. ->select();
  125. self::$childNode[]=(int)$tid;
  126. self::findArrayNode($tid, $allTree);
  127. $menu = new MenuModel();
  128. $flag = $menu->delMenu($tid,self::$childNode);
  129. return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
  130. }
  131. /**
  132. * editField 快捷编辑
  133. * @return \think\response\Json
  134. */
  135. public function editField(){
  136. extract(input());
  137. $res = Db::name($table)->where(['id' => $id ])->setField($field , $value);
  138. if($res){
  139. writelog('更新字段成功',200);
  140. return json(['code' => 200,'data' => '', 'msg' => '更新字段成功']);
  141. }else{
  142. writelog('更新字段失败',100);
  143. return json(['code' => 100,'data' => '', 'msg' => '更新字段失败']);
  144. }
  145. }
  146. /**
  147. * [rule_state 菜单状态]
  148. * @return [type] [description]
  149. * @author
  150. */
  151. public function rule_state()
  152. {
  153. extract(input());
  154. $menu = new MenuModel();
  155. $flag = $menu->ruleState($id,$num);
  156. return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
  157. }
  158. /**
  159. * batchDelMenu 批量删除菜单
  160. * @return \think\response\Json
  161. */
  162. public function batchDelMenu(){
  163. extract(input());
  164. if(empty($ids)){
  165. return json(['code'=>100,'msg'=>'请选择要删除的记录!']);
  166. }
  167. $ids = explode(',',$ids);
  168. $allTree = Db::name('auth_rule')
  169. ->field('id,pid')
  170. ->select();
  171. foreach($ids as $key=>$vo){
  172. self::$childNode[]=(int)$vo;
  173. self::findArrayNode($vo, $allTree);
  174. }
  175. $menu = new MenuModel();
  176. $flag = $menu->batchDelMenu(array_unique(self::$childNode));
  177. return json(['code' => $flag['code'],'data' => $flag['data'],'msg' => $flag['msg']]);
  178. }
  179. }