47.修改权限数据表,增加显示名字字段
ALTER TABLE `permissions` ADD COLUMN `display_name` VARCHAR(100) NOT NULL DEFAULT '' COMMENT '权限显示的名称'; ALTER TABLE `permissions` ADD COLUMN `parent_id` int(11) NOT NULL DEFAULT '0' COMMENT '对应父类名称';
48.建立权限模型
/app/Models/Permission.php
namespace App\Models; use Spatie\Permission\Models\Permission as SpatiePermission; class Permission extends SpatiePermission { }
49.在控制器中添加权限分类获取方法
namespace App\Http\Controllers; class Controller extends BaseController { ....... /** * 获取权限分类列表方法方法 * @param array $list * @param string $pk * @param string $pid * @param string $child * @param int $root * @return array */ public function tree($list=[], $pk = 'id', $pid = 'parent_id',$child='_child', $root = 0) { $tree = []; if(empty($list)){ $list = Permission::get()->toArray(); } if(is_array($list)) { $refer = array(); // ID作为键值的数组 foreach ($list as $key => $data) { $refer[$data[$pk]] = & $list[$key]; } foreach ($list as $key => $data) { $parentId = $data[$pid]; if($parentId == $root) { $tree[] = & $list[$key]; }else{ if(isset($refer[$parentId])){ $parent = & $refer[$parentId]; $parent[$child][] = & $list[$key]; } } } } return $tree; } }
50.建立权限控制器
/app/Http/Controllers/Admin/PermissionController.php
namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class PermissionController extends Controller { /** * 展示权限列表 * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View */ public function index() { return view('admin.permission.index'); } /** * 获取列表数据 * @param Request $request */ public function data(Request $request) { $parent_id = $request->get('parent_id',0); if(intval($parent_id) < 0) { $parent_id = 0; }else{ $parent_id = intval($parent_id); } $sql = "select * from permissions where parent_id = :parent_id "; $sqlCount = "select count(*) as total from permissions where parent_id = :parent_id "; $whereField = ['parent_id' => $parent_id]; $page = 1; $pagesize = 10; if($request->get('page')){ $page = intval($request->get('page')); if($page < 1){ $page = 1; } } if($request->get('limit')){ $pagesize = $request->get('limit'); if($pagesize < 1) { $pagesize = 10; } } $start_num = ($page - 1) * $pagesize; $sql .= " limit {$start_num},{$pagesize}"; if($whereField) { $listResult= DB::select($sql,$whereField); $totalCount = DB::selectOne($sqlCount,$whereField); }else{ $listResult= DB::select($sql); $totalCount = DB::selectOne($sqlCount); } $listData = []; foreach ($listResult as $data_row) { $listData[] = $data_row; } $data = [ 'code' => 0, 'msg' => '正在请求中...', 'count' => $totalCount->total, 'data' => $listData ]; return response()->json($data); } }
51. 修改模板路由
/resources/views/admin/layout.blade.php
<a lay-href="">权限管理</a>
修改为
<a lay-href="">权限管理</a>
52.添加以上模板文件
/resources/views/admin/permission/index.blade.php
@extends('admin.base') @section('content') <div class="layui-card"> <div class="layui-card-header layuiadmin-card-header-auto"> <div class="layui-btn-group "> <button class="layui-btn layui-btn-sm layui-btn-danger" id="listDelete">删 除</button> <a class="layui-btn layui-btn-sm" href="{{ route('admin.permission.create') }}">添 加</a> <button class="layui-btn layui-btn-sm" id="returnParent" pid="0">返回上级</button> </div> </div> <div class="layui-card-body"> <table id="dataTable" lay-filter="dataTable"></table> <script type="text/html" id="options"> <div class="layui-btn-group"> <a class="layui-btn layui-btn-sm" lay-event="children">子权限</a> <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a> <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a> </div> </script> </div> </div> @endsection @section('script') <script> layui.use(['layer','table','form'],function () { var layer = layui.layer; var form = layui.form; var table = layui.table; //用户表格初始化 var dataTable = table.render({ elem: '#dataTable' ,height: 500 ,url: "{{ route('admin.permission.data') }}" //数据接口 ,page: true //开启分页 ,cols: [[ //表头 {checkbox: true,fixed: true} ,{field: 'id', title: 'ID', sort: true,80} ,{field: 'name', title: '权限名称'} ,{field: 'display_name', title: '显示名称'} ,{field: 'created_at', title: '创建时间'} ,{field: 'updated_at', title: '更新时间'} ,{fixed: 'right', 260, align:'center', toolbar: '#options'} ]] }); //监听工具条 table.on('tool(dataTable)', function(obj){ //注:tool是工具条事件名,dataTable是table原始容器的属性 lay-filter="对应的值" var data = obj.data //获得当前行数据 ,layEvent = obj.event; //获得 lay-event 对应的值 if(layEvent === 'del'){ layer.confirm('确认删除吗?', function(index){ $.post("{{ route('admin.permission.destroy') }}",{_method:'delete',ids:[data.id]},function (result) { if (result.code==0){ obj.del(); //删除对应行(tr)的DOM结构 } layer.close(index); layer.msg(result.msg,{icon:6}) }); }); } else if(layEvent === 'edit'){ location.href = '/admin/permission/'+data.id+'/edit'; } else if (layEvent === 'children'){ var pid = $("#returnParent").attr("pid"); if (data.parent_id!=0){ $("#returnParent").attr("pid",pid+'_'+data.parent_id); } dataTable.reload({ where:{model:"permission",parent_id:data.id}, page:{curr:1} }) } }); //按钮批量删除 $("#listDelete").click(function () { layer.msg("由于权限重要性,系统已禁止批量删除",{icon:5}); }); //返回上一级 $("#returnParent").click(function () { var pid = $(this).attr("pid"); if (pid!='0'){ ids = pid.split('_'); parent_id = ids.pop(); $(this).attr("pid",ids.join('_')); }else { parent_id=pid; } dataTable.reload({ where:{model:"permission",parent_id:parent_id}, page:{curr:1} }) }) }) </script> @endsection
此时可以访问权限列表