Files
ulthon_admin/extend/base/common/command/admin/menu/AdminMenuDeleteBase.php
2026-03-26 20:22:34 +08:00

85 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace base\common\command\admin\menu;
use app\admin\model\SystemMenu;
use app\common\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
/**
* admin:menu:delete command - 删除菜单
*/
class AdminMenuDeleteBase extends Command
{
protected function configure()
{
parent::configure();
// 指令配置
$this->setName('admin:menu:delete')
->addOption('id', null, Option::VALUE_REQUIRED, '菜单ID')
->setDescription('删除菜单');
}
protected function execute(Input $input, Output $output)
{
// 获取参数
$id = $input->getOption('id');
// 验证参数
if (empty($id)) {
$output->error('菜单ID不能为空');
return false;
}
try {
// 1. 验证菜单是否存在
$menu = SystemMenu::find($id);
if (empty($menu)) {
$output->error('菜单ID ' . $id . ' 不存在');
return false;
}
// 2. 检查是否有子菜单
$childCount = SystemMenu::where('pid', $id)
->where('delete_time', 0)
->count();
if ($childCount > 0) {
$output->error('菜单ID ' . $id . ' 存在 ' . $childCount . ' 个子菜单,请先删除子菜单');
return false;
}
// 3. 保存菜单信息用于输出
$menuInfo = [
'id' => (int)$menu->id,
'title' => $menu->title,
'path' => $menu->href ?? '',
'icon' => $menu->icon ?? '',
'parent_id' => (int)($menu->pid ?? 0),
'sort' => (int)($menu->sort ?? 0),
'node' => $menu->auth_node ?? '',
];
// 4. 执行删除(软删除)
$menu->delete();
// 5. 输出结果
$output->info('菜单删除成功');
$output->info('菜单ID: ' . $menuInfo['id']);
$output->info('菜单名称: ' . $menuInfo['title']);
$output->info('菜单路径: ' . $menuInfo['path']);
} catch (\Throwable $e) {
$output->error('删除菜单失败: ' . $e->getMessage());
return false;
}
return true;
}
}