PHP:【商城后台管理系统】部署管理员一级菜单和二级菜单列表添加功能
一.1级菜单和2级菜单列表添加页面
二.部署流程
- 数据库后台菜单列表,字段设置,pid 是 菜单列表级别,0代表1级菜单,>0的是二级菜单,并且二级菜单pid字段值需要对应mid字段值,这样才可以相互绑定。每个菜单对应的还有Controller控制器,action方法,都要一一记录,这样才能映射到thinkphp控制器层还有视图层,用来区分每一级菜单的前端视图
- 框架采用ThinkPHP6.0
HTML 1级菜单 代码块
<!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>controller</th>
<th>action</th>
<th>是否隐藏</th>
<th>状态</th>
<th>编辑</th>
</tr>
</thead>
<tbody>
{foreach $data as $menu}
<tr>
<td>{$menu['mid']}</td>
<td>{$menu['title']}</td>
<td>{$menu['controller']}</td>
<td>{$menu['action']}</td>
<td style="color:{$menu['ishidden']==0 ? 'green' : 'red'};">
{$menu['ishidden']==0 ? '不隐藏' : '隐藏'}
</td>
<td style="color:{$menu['status']==0 ? 'green' : 'red'};">
{$menu['status']==0 ? '开启' : '关闭'}
</td>
<td>
<button class="layui-btn layui-btn-primary layui-btn-xs" onclick="lower({$menu['mid']})">下级菜单</button>
<button class="layui-btn layui-btn-xs" onclick="edit({$menu['mid']})">编辑</button>
<button class="layui-btn layui-btn-danger layui-btn-xs"onclick="dle({$menu['mid']})">删除</button>
</td>
</tr>
{/foreach}
</tbody>
</table>
</body>
<script>
let $ = layui.jquery;
//编辑菜单
function edit(mid)
{
layer.open({
type:2,
title: '编辑菜单',
shadeClose: true,
shade: 0.8,
area: ['450px', '450px'],
content: '/admin/Menus/edit?mid='+mid
});
}
//删除菜单
function dle(mid)
{
layer.confirm('确定删除吗?', {
btn: ['确定','取消'],
},
function(){
let date = {};
$.post('/admin/Menus/dle?mid='+mid,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: ['450px', '450px'],
content: '/admin/Menus/add'
});
}
//下级菜单
function lower(mid)
{
layer.open({
type:2,
title: '下级菜单',
shadeClose: true,
shade: 0.8,
area: ['800px', '600px'],
content: '/admin/Menus/lower?mid='+mid
});
}
</script>
</html>
HTML 2级菜单 代码块
<!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;">
<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({$data[0]['pid']})">添加</button>
</div>
<table class="layui-table" >
<thead>
<tr>
<th>ID</th>
<th>菜单名称</th>
<th>controller</th>
<th>action</th>
<th>是否隐藏</th>
<th>状态</th>
<th>编辑</th>
</tr>
</thead>
<tbody>
{foreach $data as $menu}
<tr>
<td>{$menu['mid']}</td>
<td>{$menu['title']}</td>
<td>{$menu['controller']}</td>
<td>{$menu['action']}</td>
<td style="color:{$menu['ishidden']==0 ? 'green' : 'red'};">
{$menu['ishidden']==0 ? '不隐藏' : '隐藏'}
</td>
<td style="color:{$menu['status']==0 ? 'green' : 'red'};">
{$menu['status']==0 ? '开启' : '关闭'}
</td>
<td>
<button class="layui-btn layui-btn-xs" onclick="edit({$menu['mid']})">编辑</button>
<button class="layui-btn layui-btn-danger layui-btn-xs"onclick="dle({$menu['mid']})">删除</button>
</td>
</tr>
{/foreach}
</tbody>
</table>
</body>
<script>
let $ = layui.jquery;
//编辑菜单
function edit(mid)
{
layer.open({
type:2,
title: '编辑菜单',
shadeClose: true,
shade: 0.8,
area: ['450px', '450px'],
content: '/admin/Menus/edit?mid='+mid
});
}
//删除菜单
function dle(mid)
{
layer.confirm('确定删除吗?', {
btn: ['确定','取消'],
},
function(){
let date = {};
$.post('/admin/Menus/dle?mid='+mid,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(pid)
{
layer.open({
type:2,
title: '添加菜单',
shadeClose: true,
shade: 0.8,
area: ['450px', '450px'],
content: '/admin/Menus/add?pid='+pid
});
}
</script>
</html>
PHP 代码块
<?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 Menus extends Base
{
//菜单列表
public function index()
{
//一级菜单
$data['menuList'] = Db::table('admin_menu')->where('pid',0)->select()->toArray();
// print_r($data);
// die;
View::assign([
'data' => $data['menuList']
]);
return View('/menus/index');
}
//菜单添加
public function add()
{
//如果pid 为空 则是一级菜单,如果不为空则是二级菜单
$pid = Request::param('pid');
if (Request::isPost()) {
//菜单信息
$data['pid'] = Request::post('pid');
$data['title'] = Request::post('title');
$data['controller'] = Request::post('controller');
$data['action'] = Request::post('action');
$data['ishidden'] = Request::post('ishidden');
$data['status'] = Request::post('status');
//当前角色信息
$admin = $this->admin;
$MyGroup = $this->MyGroup;
if (!empty($data)) {
$insert = Db::table('admin_menu')->insert($data);
//插入的最大值行数
$count = (int)Db::table('admin_menu')->max('mid');
//再次插入到角色权限中
array_push($MyGroup['rights'],$count);
$MyGroup['rights'] = json_encode($MyGroup['rights']);
//更新角色权限
$mygroup = Db::table('admin_group')->where('gid',$MyGroup['gid'])->update([
'rights' => $MyGroup['rights']
]);
}else {
exit(json_encode(['id' => 0 ,'msg' => '输入的信息错误']));
}
if (!empty($insert)) {
echo json_encode(['id' => 1 ,'msg' => '添加成功']);
}else {
echo json_encode(['id' => 0 ,'msg' => '添加失败']);
}
}else {
//菜单信息
$data['menu'] = Db::table('admin_menu')->where('pid',$pid)->find();
View::assign([
'pid' => $pid,
'data' => $data['menu']
]);
return View('/menus/add');
}
}
//下级菜单
public function lower()
{
//下级菜单查询
$mid = Request::param('mid');
$data['MenuList'] = Db::table('admin_menu')->where('pid',$mid)->select()->toArray();
View::assign([
'data' => $data['MenuList']
]);
return View('/menus/lower');
}
//菜单修改
public function edit()
{
//菜单信息
if (Request::isPost()) {
$data['mid'] = Request::post('mid');
$data['title'] = Request::post('title');
$data['controller'] = Request::post('controller');
$data['action'] = Request::post('action');
$data['ishidden'] = Request::post('ishidden');
$data['status'] = Request::post('status');
if (!empty($data)) {
$update = Db::table('admin_menu')->where('mid',$data['mid'])->update($data);
}else {
exit(json_encode(['id' => 0 ,'msg' => '输入的信息错误']));
}
if (!empty($update)) {
echo json_encode(['id' => 1 ,'msg' => '修改成功']);
}else {
echo json_encode(['id' => 0 ,'msg' => '修改失败']);
}
}else {
//菜单id
$mid = Request::get('mid');
//菜单信息
$data['menu'] = Db::table('admin_menu')->where('mid',$mid)->find();
View::assign([
'data' => $data['menu']
]);
return View('/menus/edit');
}
}
//菜单删除
public function dle ()
{
$mid = (int)Request::param('mid');
//当前角色信息
$admin = $this->admin;
$MyGroup = $this->MyGroup;
//删除角色对应的菜单权限
$GroupKey = (int)array_search($mid,$MyGroup['rights'],true);
array_splice($MyGroup['rights'],$GroupKey,1);
$MyGroup['rights'] = json_encode($MyGroup['rights']);
if (!empty($mid)) {
$delete = Db::table('admin_menu')->where('mid',$mid)->delete();
//更新角色权限
$mygroup = Db::table('admin_group')->where('gid',$MyGroup['gid'])->update([
'rights' => $MyGroup['rights']
]);
}
if (!empty($delete)) {
echo json_encode(['id' => 1 ,'msg' => '删除成功']);
}else {
echo json_encode(['id' => 0 ,'msg' => '删除失败']);
}
}
}