mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-01 23:42:48 +08:00
107 lines
3.8 KiB
PHP
107 lines
3.8 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:create command - 创建菜单
|
||
*/
|
||
class AdminMenuCreateBase extends Command
|
||
{
|
||
protected function configure()
|
||
{
|
||
parent::configure();
|
||
|
||
// 命令配置
|
||
$this->setName('admin:menu:create')
|
||
->addOption('title', null, Option::VALUE_REQUIRED, '菜单标题(必填)')
|
||
->addOption('path', null, Option::VALUE_OPTIONAL, '菜单路径', '')
|
||
->addOption('icon', null, Option::VALUE_OPTIONAL, '菜单图标', '')
|
||
->addOption('parent-id', null, Option::VALUE_OPTIONAL, '父菜单ID(默认为0)', 0)
|
||
->addOption('sort', null, Option::VALUE_OPTIONAL, '排序(默认为100)', 100)
|
||
->addOption('node', null, Option::VALUE_OPTIONAL, '权限节点', '')
|
||
->addOption('remark', null, Option::VALUE_OPTIONAL, '备注说明', '')
|
||
->setDescription('创建菜单');
|
||
}
|
||
|
||
protected function execute(Input $input, Output $output)
|
||
{
|
||
// 获取参数
|
||
$title = $input->getOption('title');
|
||
$path = $input->getOption('path');
|
||
$icon = $input->getOption('icon');
|
||
$parentId = $input->getOption('parent-id');
|
||
$sort = $input->getOption('sort');
|
||
$node = $input->getOption('node');
|
||
$remark = $input->getOption('remark');
|
||
|
||
// 验证必填参数
|
||
if (empty($title)) {
|
||
$output->error('菜单标题不能为空');
|
||
return false;
|
||
}
|
||
|
||
try {
|
||
// 准备菜单数据
|
||
$menuData = [
|
||
'title' => $title,
|
||
'path' => $path,
|
||
'icon' => $icon,
|
||
'parent_id' => (int)$parentId,
|
||
'sort' => (int)$sort,
|
||
'node' => $node,
|
||
'remark' => $remark,
|
||
];
|
||
|
||
// 实例化菜单服务(使用 app\admin\service\MenuService,它继承自 base\common\service\MenuServiceBase)
|
||
// 由于 MenuServiceBase 需要 adminId 参数,我们使用 0 表示命令行操作
|
||
$menuService = new \app\common\service\MenuService(0);
|
||
|
||
// 创建菜单
|
||
$menuId = $menuService->create($menuData);
|
||
|
||
// 获取创建的菜单详情
|
||
$menuModel = SystemMenu::find($menuId);
|
||
$menu = $menuModel ? $menuModel->toArray() : null;
|
||
|
||
if (empty($menu)) {
|
||
throw new \Exception('菜单创建成功,但无法获取菜单详情');
|
||
}
|
||
|
||
$outputData = [
|
||
'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'] ?? '',
|
||
];
|
||
|
||
// 输出结果
|
||
$output->info('菜单创建成功');
|
||
$output->info('菜单ID: ' . $outputData['id']);
|
||
$output->info('菜单标题: ' . $outputData['title']);
|
||
$output->info('菜单路径: ' . $outputData['path']);
|
||
$output->info('菜单图标: ' . $outputData['icon']);
|
||
$output->info('父菜单ID: ' . $outputData['parent_id']);
|
||
$output->info('排序: ' . $outputData['sort']);
|
||
if (!empty($outputData['node'])) {
|
||
$output->info('权限节点: ' . $outputData['node']);
|
||
}
|
||
} catch (\Throwable $e) {
|
||
$output->error('创建菜单失败: ' . $e->getMessage());
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
}
|