①首页
②角色添加
③角色编辑
④角色删除
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>管理员账号列表</title>
<link rel="stylesheet" href="/static/layui-v2.6.8/layui/css/layui.css" />
<script src="/static/layui-v2.6.8/layui/layui.js"></script>
</head>
<body style="padding: 10px;min-width:737px">
<div class="layui-item" >
<span style="color:#777;font-size:20px;height:30px;line-height:30px">首页/</span>
<span style="color:#ccc">角色列表</span>
<button class="layui-btn layui-btn-sm" style="float:right;margin:5px" onclick="add()">添加</button>
</div>
<table class="layui-table" >
<thead>
<tr>
<th>ID</th>
<th>角色编号</th>
<th>角色名称</th>
<th>编辑</th>
</tr>
</thead>
<tbody>
{foreach $data as $v}
<tr>
<td>{$v['id']}</td>
<td>{$v['gid']}</td>
<td>{$v['title']}</td>
<td>
<button class="layui-btn layui-btn-sm " onclick="edit({$v['id']})">编辑</button>
<button class="layui-btn layui-btn-sm layui-btn-danger" onclick="dle({$v['id']})">删除</button>
</td>
</tr>
{/foreach}
</tbody>
</table>
</body>
<script>
$ = layui.jquery;
//编辑角色
function edit(id) {
layer.open({
type:2,
title: '编辑角色',
shadeClose: true,
shade: 0.8,
area: ['500px', '500px'],
content: '/admin/Group/edit?id='+id
});
}
//删除
function dle(id) {
layer.confirm('确定删除吗?', {
btn: ['确定','取消'],
},
function(){
let date = {};
$.post('/admin/Group/dle?id='+id,date,function(res){
if (res.id == 1) {
layer.alert(res.msg,{icon:1});
setTimeout(() => {
window.location.reload();
}, 1000);
}else{
layer.alert(res.msg,{icon:2});
}
},'json')
});
}
//添加角色
function add()
{
layer.open({
type:2,
title: '添加角色',
shadeClose: true,
shade: 0.8,
area: ['500px', '500px'],
content: '/admin/Group/add'
});
}
</script>
</html>
<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use think\facade\Request;
use think\facade\Db;
use think\facade\View;
use think\facade\Session;
/**
* 角色管理
*/
class Group extends Base
{
//角色列表
public function index()
{
//角色渲染
$data['group'] = Db::table('admin_group')->select()->toArray();
View::assign([
'data' => $data['group']
]);
return View('/group/index');
}
//角色添加
public function add()
{
if (Request::isPost()) {
$data['gid'] = Request::post('gid');
$data['title'] = Request::post('title');
$data['rights'] = array_keys(Request::post('rights'));
$data['rights'] = json_encode($data['rights']);
if (empty($data['gid'])) {
exit(json_encode(['id' => 0 ,'msg' => '角色编号不能为空']));
}
if (empty($data['title'])) {
exit(json_encode(['id' => 0 ,'msg' => '角色名称不能为空']));
}
$insert = Db::table('admin_group')->insert($data);
if (!empty($insert)) {
echo json_encode(['id' => 1 ,'msg' => '角色添加成功']);
}else {
echo json_encode(['id' => 0 ,'msg' => '角色添加失败']);
}
}else {
//角色权限菜单视图层渲染
$tmp_menu_list = Db::table('admin_menu')->where('status',0)->select()->toArray();
//将角色权限菜单数组下标换成角色的mid
$menu_list = $this->SetKey($tmp_menu_list,array());
//构造树菜单(无限级)
$menus = $this->buildMenuTree($menu_list);
//递归 构造树菜单(无限级)变成二级菜单
$result = $this->SetMenu($menus,array());
View::assign([
'data' => $result,
]);
return View('/group/add');
}
}
//角色编辑
public function edit()
{
if (Request::isPost()) {
$id = Request::post('id');
$data['title'] = Request::post('title');
$data['rights'] = array_keys(Request::post('rights'));
$data['rights'] = json_encode($data['rights']);
if (empty($data['title'])) {
exit(json_encode(['id' => 0 ,'msg' => '角色名称不能为空']));
}
$update = Db::table('admin_group')->where('id',$id)->update($data);
if (!empty($update)) {
echo json_encode(['id' => 1,'msg' => '保存成功']);
}else {
echo json_encode(['id' => 0,'msg' => '保存失败']);
}
}else {
$id = Request::get('id');
$data['group'] = Db::table('admin_group')->where('id',$id)->find();
$data['group']['rights'] = json_decode($data['group']['rights'],true);
//角色权限菜单视图层渲染
$tmp_menu_list = Db::table('admin_menu')->where('status',0)->select()->toArray();
//将角色权限菜单数组下标换成角色的mid
$menu_list = $this->SetKey($tmp_menu_list,array());
//构造树菜单(无限级)
$menus = $this->buildMenuTree($menu_list);
//递归 构造树菜单(无限级)变成二级菜单
$result = $this->SetMenu($menus,array());
View::assign([
'result' => $result,
'id' => $id,
'group' => $data['group'],
'rights' => $data['group']['rights']
]);
return View('/group/edit');
}
}
//角色删除
public function dle()
{
$id = Request::param("id");
if (!empty($id)) {
$delete = Db::table("admin_group")->where('id',$id)->delete();
}
if (!empty($delete)) {
echo json_encode(['id' => 1 ,'msg'=>'删除成功']);
}else {
echo json_encode(['id' => 0 ,'msg'=>'删除失败']);
}
}
//将数组下标换成角色的mid
private function SetKey($item,$array)
{
foreach ($item as $v) {
$array[$v['mid']] = $v;
}
return $array;
}
//遍历递归menu菜单
private function SetMenu($item,$array)
{
foreach ($item as $value) {
//如果构造树菜单的子菜单有children 则进行递归 如果没有则定位空数组
$value['children'] = isset($value['children']) ?$this->formatMenus($value['children']) : [];
//剩余则赋值给$result
$array[] = $value;
}
return $array;
}
//构造树菜单(无限级)
private function buildMenuTree($items)
{
$tree = [];
foreach ($items as $item) {
//如果items 的 下标 是$item pid 则 创建新的children子数组存放 $item
if (isset($items[$item['pid']])) {
$items[$item['pid']]['children'][] = &$items[$item['mid']];
}else {
$tree[] = &$items[$item['mid']];
}
}
return $tree;
}
//递归 构造树菜单(无限级)变成二级菜单
private function formatMenus($items,&$res=[])
{
foreach ($items as $item) {
if (!isset($item['children'])) {
//如果没有children 则存放在 $res
$res[] = $item;
}else {
//如果有children 则进行递归处理
$temp = $item['children'];
//释放$item['children']
unset($item['children']);
$res[] = $item;
$this->formatMenus($temp,$res);
}
}
return $res;
}
}