abstract:作业总结:这次课程其他没问题,就是觉得那个无限极列表有点复杂,看明白了,但是还不是很熟。以后会复习一下。菜单模块:作业总结:
这次课程其他没问题,就是觉得那个无限极列表有点复杂,看明白了,但是还不是很熟。以后会复习一下。
菜单模块:
db->table('menus')->where(['pid'=>$pid])->lists(); $data['pid']=$pid; // 获取父菜单的数据 $data['back']=$this->db->table('menus')->where(['mid'=>$pid])->item(); return $this->fetch('',$data); } public function add() { // 获取pid得到当前的父菜单 $pid=(int)input('get.pid'); // 获取mid判断是添加还是更新 $mid=(int)input('get.mid'); $data['pid']=$pid; $data['mid']=$mid; // 通过pid获取父菜单的标题 $data['title']=$this->db->table('menus')->where(['mid'=>$pid])->item(); // 获取需要编辑的菜单的数据 $data['res']=$this->db->table('menus')->where(['mid'=>$mid])->item(); return $this->fetch('',$data); } public function save() { $data=input('post.'); if($data['title'] == ''){ return ['res'=>1,'msg'=>'菜单名称不能为空']; } if($data['pid']>0 && $data['controller'] == ''){ return ['res'=>1,'msg'=>'控制器不能为空']; } if($data['pid']>0 && $data['method'] == ''){ return ['res'=>1,'msg'=>'方法不能为空']; } // 判断是添加还是更新 if ($data['mid']) { $res = $this->db->table('menus')->update($data); if ($res) { return ['res'=>0,'msg'=>'更新成功']; }else{ return ['res'=>1,'msg'=>'更新失败']; } }else{ $res = $this->db->table('menus')->insert($data); if ($res) { return ['res'=>0,'msg'=>'添加成功']; }else{ return ['res'=>1,'msg'=>'添加失败']; } } } public function del() { $mid=input('get.mid'); $res=$this->db->table('menus')->where(['mid'=>$mid])->delete(); if ($res) { return ['res'=>0,'msg'=>'删除成功']; }else{ return ['res'=>1,'msg'=>'删除失败']; } } }角色模块:
db->table('roles')->lists(); return $this->fetch('',$data); } public function add() { // 获取传过来的角色id $rid = (int)input('get.rid'); // 获取该id的数据, $group = $this->db->table('roles')->where(['rid'=>$rid])->item(); if($group){ $group['rights'] = json_decode($group['rights']); } // 以mid为下标,取出数组 $menu_list = $this->db->table('menus')->where(['status'=>0])->cates('mid'); // 遍历,如果pid不存在,则传入,如果pid存在则保存在父菜单中的[chidren]中 $menu = $this->gettreeitems($menu_list); $results = []; // 遍历,递归,取出每个children数组里面的值,输出数据。 foreach ($menu as $value) { $value['children'] = isset($value['children'])?$this->formatMenus($value['children']):false; $results[] = $value; } $data['menus'] = $results; $data['group'] = $group; return $this->fetch('',$data); } // 把子菜单放入父菜单的children中 private function gettreeitems($items){ $tree = []; foreach ($items as $item) { if(isset($items[$item['pid']])){ $items[$item['pid']]['children'][] = &$items[$item['mid']]; }else{ $tree[] = &$items[$item['mid']]; } } return $tree; } // 取出每个菜单最后的children里面的数据,然后unset,循环 private function formatMenus($items,&$res = array()){ foreach ($items as $item) { if(!isset($item['children'])){ $res[] = $item; }else{ $tem = $item['children']; unset($item['children']); $res[] = $item; $this->formatMenus($tem,$res); } } return $res; } public function save() { // 获取当前rid $rid = (int)input('post.rid'); $data['title'] = trim(input('post.title')); $menus = input('post.menu/a'); if(!$data['title']){ return['code'=>1,'msg'=>'角色名称不能为空']; } $menus && $data['rights'] = json_encode(array_keys($menus)); // 如果$gid存在则 if($rid){ $this->db->table('admin_groups')->where(['rid'=>$rid])->update($data); }else{ $this->db->table('admin_groups')->insert($data); } return['code'=>0,'msg'=>'保存成功'];} public function del() { $mid=input('get.mid'); $res=$this->db->table('menus')->where(['mid'=>$mid])->delete(); if ($res) { return ['res'=>0,'msg'=>'删除成功']; }else{ return ['res'=>1,'msg'=>'删除失败']; } } }
Correcting teacher:天蓬老师Correction time:2018-12-10 15:50:36
Teacher's summary:这些都是开发中,一些最基本的知识点,多做几次就熟悉了