超管后台 - 主菜单返工、删除超管后台冗余的模型对象
This commit is contained in:
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
|
||||
namespace app\superadmin\controller\Menu;
|
||||
|
||||
use app\common\model\Menu as MenuModel;
|
||||
use app\common\model\AdministratorPermissions as AdministratorPermissionsModel;
|
||||
use app\superadmin\controller\BaseController;
|
||||
use think\facade\Cache;
|
||||
|
||||
/**
|
||||
* 菜单控制器
|
||||
*/
|
||||
class GetMenuTreeController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 组织成树状结构
|
||||
*
|
||||
* @param array $menus
|
||||
* @param int $parentId
|
||||
* @return array
|
||||
*/
|
||||
private function buildMenuTree(array $menus, int $parentId = 0): array
|
||||
{
|
||||
$tree = [];
|
||||
|
||||
foreach ($menus as $menu) {
|
||||
if ($menu['parentId'] == $parentId) {
|
||||
$children = $this->buildMenuTree($menus, $menu['id']);
|
||||
|
||||
if (!empty($children)) {
|
||||
$menu['children'] = $children;
|
||||
}
|
||||
|
||||
$tree[] = $menu;
|
||||
}
|
||||
}
|
||||
|
||||
return $tree;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取管理员权限
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getPermissions(): array
|
||||
{
|
||||
$record = AdministratorPermissionsModel::where('adminId', $this->getAdminInfo('id'))->find();
|
||||
|
||||
if (!$record || empty($record->permissions)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$permissions = $record->permissions ? json_decode($record->permissions, true) : [];
|
||||
|
||||
if (isset($permissions['ids']) && !empty($permissions['ids'])) {
|
||||
return is_string($permissions['ids']) ? explode(',', $permissions['ids']) : $permissions['ids'];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有菜单,并组织成树状结构
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getMenuTree(): array
|
||||
{
|
||||
// 获取所有菜单
|
||||
$allMenus = MenuModel::where('status', 1)->order('sort', 'asc')->select()->toArray();
|
||||
|
||||
// 组织成树状结构
|
||||
return $allMenus ? $this->buildMenuTree($allMenus) : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有一级菜单(用户拥有权限的)
|
||||
*
|
||||
* @param array $permissionIds
|
||||
* @return array
|
||||
*/
|
||||
protected function getTopMenusInPermissionIds(array $permissionIds): array
|
||||
{
|
||||
$where = [
|
||||
'id' => ['in', $permissionIds],
|
||||
'parentId' => 0,
|
||||
'status' => 1,
|
||||
];
|
||||
|
||||
return MenuModel::where($where)->order('sort', 'asc')->select()->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有子菜单.
|
||||
*
|
||||
* @param array $topMenuIds
|
||||
* @return array
|
||||
*/
|
||||
protected function getAllChildrenInPermissionIds(array $topMenuIds): array
|
||||
{
|
||||
$where = [
|
||||
'parentId' => ['in', $topMenuIds],
|
||||
'status' => 1,
|
||||
];
|
||||
|
||||
return MenuModel::where($where)->order('sort', 'asc')->select()->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户菜单
|
||||
*
|
||||
* @param array $permissionIds
|
||||
* @return array
|
||||
*/
|
||||
protected function getUserMenus(array $permissionIds): array
|
||||
{
|
||||
$topMenus = $this->getTopMenusInPermissionIds($permissionIds);
|
||||
|
||||
// 菜单ID集合,用于获取子菜单
|
||||
$menuIds = array_column($topMenus, 'id');
|
||||
|
||||
return $this->getAllChildrenInPermissionIds($menuIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建菜单树.
|
||||
*
|
||||
* @param array $childMenus
|
||||
* @return array
|
||||
*/
|
||||
protected function _makeMenuTree(array $childMenus): array
|
||||
{
|
||||
// 将子菜单按照父ID进行分组
|
||||
$childMenusGroup = [];
|
||||
|
||||
foreach ($childMenus as $menu) {
|
||||
$childMenusGroup[$menu['parentId']][] = $menu;
|
||||
}
|
||||
|
||||
foreach ($topMenus as $topMenu) {
|
||||
if (isset($childMenusGroup[$topMenu['id']])) {
|
||||
$topMenu['children'] = $childMenusGroup[$topMenu['id']];
|
||||
}
|
||||
|
||||
$menuTree[] = $topMenu;
|
||||
}
|
||||
|
||||
return $menuTree ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据权限ID获取相应的菜单树
|
||||
*
|
||||
* @param array $permissionIds 权限ID数组
|
||||
* @return array
|
||||
*/
|
||||
protected function getMenuTreeByPermissions(array $permissionIds): array
|
||||
{
|
||||
if ($permissionIds) {
|
||||
$childMenus = $this->getUserMenus($permissionIds);
|
||||
|
||||
// 构建菜单树
|
||||
return $this->_makeMenuTree($childMenus);
|
||||
}
|
||||
|
||||
// 如果没有权限,返回空数组
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取菜单列表(树状结构)
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
if ($this->getAdminInfo('id') == 1) {
|
||||
$menuTree = $this->getMenuTree();
|
||||
} else {
|
||||
$menuTree = $this->getMenuTreeByPermissions(
|
||||
$this->getPermissions()
|
||||
);
|
||||
}
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '获取成功',
|
||||
'data' => $menuTree
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -1,183 +0,0 @@
|
||||
<?php
|
||||
namespace app\superadmin\controller;
|
||||
|
||||
use think\Controller;
|
||||
use app\superadmin\model\Menu as MenuModel;
|
||||
|
||||
/**
|
||||
* 菜单控制器
|
||||
*/
|
||||
class MenuController extends Controller
|
||||
{
|
||||
/**
|
||||
* 获取菜单列表(树状结构)
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function getMenuTree()
|
||||
{
|
||||
// 参数处理
|
||||
$onlyEnabled = $this->request->param('only_enabled', 1);
|
||||
$useCache = $this->request->param('use_cache', 0); // 由于要根据用户权限过滤,默认不使用缓存
|
||||
|
||||
// 获取当前登录的管理员信息
|
||||
$adminInfo = $this->request->adminInfo;
|
||||
|
||||
// 调用模型获取菜单树
|
||||
if ($adminInfo->id == 1) {
|
||||
// 超级管理员获取所有菜单
|
||||
$menuTree = MenuModel::getMenuTree($onlyEnabled, $useCache);
|
||||
} else {
|
||||
// 非超级管理员根据权限获取菜单
|
||||
$permissionIds = \app\superadmin\model\AdministratorPermissions::getPermissions($adminInfo->id);
|
||||
$menuTree = MenuModel::getMenuTreeByPermissions($permissionIds, $onlyEnabled);
|
||||
}
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '获取成功',
|
||||
'data' => $menuTree
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有菜单(平铺结构,便于后台管理)
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function getMenuList()
|
||||
{
|
||||
// 查询条件
|
||||
$where = [];
|
||||
$status = $this->request->param('status');
|
||||
if ($status !== null && $status !== '') {
|
||||
$where[] = ['status', '=', intval($status)];
|
||||
}
|
||||
|
||||
// 获取所有菜单
|
||||
$menus = MenuModel::where($where)
|
||||
->order('sort', 'asc')
|
||||
->select();
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '获取成功',
|
||||
'data' => $menus
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加或更新菜单
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function saveMenu()
|
||||
{
|
||||
if (!$this->request->isPost()) {
|
||||
return json(['code' => 405, 'msg' => '请求方法不允许']);
|
||||
}
|
||||
|
||||
// 获取参数
|
||||
$data = $this->request->post();
|
||||
|
||||
// 验证参数
|
||||
$validate = $this->validate($data, [
|
||||
'title|菜单名称' => 'require|max:50',
|
||||
'path|路由路径' => 'require|max:100',
|
||||
'parent_id|父菜单ID' => 'require|number',
|
||||
'status|状态' => 'require|in:0,1',
|
||||
'sort|排序' => 'require|number',
|
||||
]);
|
||||
|
||||
if ($validate !== true) {
|
||||
return json(['code' => 400, 'msg' => $validate]);
|
||||
}
|
||||
|
||||
// 保存菜单
|
||||
$result = MenuModel::saveMenu($data);
|
||||
|
||||
if ($result) {
|
||||
return json(['code' => 200, 'msg' => '保存成功']);
|
||||
} else {
|
||||
return json(['code' => 500, 'msg' => '保存失败']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除菜单
|
||||
* @param int $id 菜单ID
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function deleteMenu($id)
|
||||
{
|
||||
if (!$this->request->isDelete()) {
|
||||
return json(['code' => 405, 'msg' => '请求方法不允许']);
|
||||
}
|
||||
|
||||
if (empty($id) || !is_numeric($id)) {
|
||||
return json(['code' => 400, 'msg' => '参数错误']);
|
||||
}
|
||||
|
||||
$result = MenuModel::deleteMenu($id);
|
||||
|
||||
if ($result) {
|
||||
return json(['code' => 200, 'msg' => '删除成功']);
|
||||
} else {
|
||||
return json(['code' => 500, 'msg' => '删除失败,可能存在子菜单']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新菜单状态
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function updateStatus()
|
||||
{
|
||||
if (!$this->request->isPost()) {
|
||||
return json(['code' => 405, 'msg' => '请求方法不允许']);
|
||||
}
|
||||
|
||||
$id = $this->request->post('id');
|
||||
$status = $this->request->post('status');
|
||||
|
||||
if (empty($id) || !is_numeric($id) || !in_array($status, [0, 1])) {
|
||||
return json(['code' => 400, 'msg' => '参数错误']);
|
||||
}
|
||||
|
||||
$menu = MenuModel::find($id);
|
||||
if (!$menu) {
|
||||
return json(['code' => 404, 'msg' => '菜单不存在']);
|
||||
}
|
||||
|
||||
$menu->status = $status;
|
||||
$result = $menu->save();
|
||||
|
||||
// 清除缓存
|
||||
MenuModel::clearMenuCache();
|
||||
|
||||
if ($result) {
|
||||
return json(['code' => 200, 'msg' => '状态更新成功']);
|
||||
} else {
|
||||
return json(['code' => 500, 'msg' => '状态更新失败']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一级菜单(供权限设置使用)
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function getTopLevelMenus()
|
||||
{
|
||||
// 获取所有启用的一级菜单
|
||||
$menus = \app\superadmin\model\Menu::where([
|
||||
['parent_id', '=', 0],
|
||||
['status', '=', 1]
|
||||
])
|
||||
->field('id, title')
|
||||
->order('sort', 'asc')
|
||||
->select();
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '获取成功',
|
||||
'data' => $menus
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user