mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-01 23:42:48 +08:00
130 lines
4.3 KiB
PHP
130 lines
4.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 app\common\service\MenuService;
|
||
use think\console\Input;
|
||
use think\console\input\Option;
|
||
use think\console\Output;
|
||
|
||
/**
|
||
* admin:menu:update command - 更新菜单
|
||
*/
|
||
class AdminMenuUpdateBase extends Command
|
||
{
|
||
protected function configure()
|
||
{
|
||
parent::configure();
|
||
|
||
// 指令配置
|
||
$this->setName('admin:menu:update')
|
||
->addOption('id', null, Option::VALUE_REQUIRED, '菜单ID(必填)')
|
||
->addOption('title', null, Option::VALUE_OPTIONAL, '菜单标题')
|
||
->addOption('path', null, Option::VALUE_OPTIONAL, '菜单路径')
|
||
->addOption('icon', null, Option::VALUE_OPTIONAL, '菜单图标')
|
||
->addOption('parent-id', null, Option::VALUE_OPTIONAL, '父级菜单ID')
|
||
->addOption('sort', null, Option::VALUE_OPTIONAL, '排序')
|
||
->setDescription('编辑菜单');
|
||
}
|
||
|
||
protected function execute(Input $input, Output $output)
|
||
{
|
||
// 获取参数
|
||
$id = (int)($input->getOption('id') ?? 0);
|
||
$title = $input->getOption('title');
|
||
$path = $input->getOption('path');
|
||
$icon = $input->getOption('icon');
|
||
$parentId = $input->getOption('parent-id');
|
||
$sort = $input->getOption('sort');
|
||
|
||
// 验证参数:id 是必填的
|
||
if (empty($id)) {
|
||
$output->error('菜单ID不能为空');
|
||
return false;
|
||
}
|
||
|
||
try {
|
||
// 1. 验证菜单是否存在
|
||
$menu = SystemMenu::find($id);
|
||
if (empty($menu)) {
|
||
$output->writeln('<error>菜单ID ' . $id . ' 不存在</error>');
|
||
return false;
|
||
}
|
||
|
||
// 2. 准备更新数据
|
||
$updateData = [];
|
||
|
||
if ($title !== null) {
|
||
$updateData['title'] = $title;
|
||
}
|
||
|
||
if ($path !== null) {
|
||
$updateData['path'] = $path;
|
||
}
|
||
|
||
if ($icon !== null) {
|
||
$updateData['icon'] = $icon;
|
||
}
|
||
|
||
if ($parentId !== null) {
|
||
$updateData['parent_id'] = (int)$parentId;
|
||
}
|
||
|
||
if ($sort !== null) {
|
||
$updateData['sort'] = (int)$sort;
|
||
}
|
||
|
||
// 检查是否有需要更新的字段
|
||
if (empty($updateData)) {
|
||
$output->error('没有提供需要更新的字段');
|
||
return false;
|
||
}
|
||
|
||
// 3. 调用 MenuService 更新菜单
|
||
$menuService = new MenuService(0);
|
||
$success = $menuService->update($id, $updateData);
|
||
|
||
if (!$success) {
|
||
$output->writeln('<error>菜单更新失败</error>');
|
||
return false;
|
||
}
|
||
|
||
// 4. 重新查询菜单数据
|
||
$updatedMenu = SystemMenu::find($id);
|
||
if (empty($updatedMenu)) {
|
||
$output->writeln('<error>菜单更新后查询失败</error>');
|
||
return false;
|
||
}
|
||
|
||
$outputData = [
|
||
'id' => (int)$updatedMenu->id,
|
||
'title' => $updatedMenu->title ?? '',
|
||
'path' => $updatedMenu->href ?? '',
|
||
'icon' => $updatedMenu->icon ?? '',
|
||
'parent_id' => (int)($updatedMenu->pid ?? 0),
|
||
'sort' => (int)($updatedMenu->sort ?? 0),
|
||
'node' => $updatedMenu->auth_node ?? '',
|
||
];
|
||
|
||
// 5. 输出结果
|
||
$output->writeln('<info>菜单编辑成功</info>');
|
||
$output->writeln('<info>菜单ID: ' . $outputData['id'] . '</info>');
|
||
$output->writeln('<info>菜单标题: ' . $outputData['title'] . '</info>');
|
||
$output->writeln('<info>菜单路径: ' . $outputData['path'] . '</info>');
|
||
$output->writeln('<info>菜单图标: ' . $outputData['icon'] . '</info>');
|
||
$output->writeln('<info>父级菜单ID: ' . $outputData['parent_id'] . '</info>');
|
||
$output->writeln('<info>排序: ' . $outputData['sort'] . '</info>');
|
||
|
||
} catch (\Throwable $e) {
|
||
$output->error('编辑菜单失败: ' . $e->getMessage());
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
}
|