|
@@ -10,6 +10,8 @@ use Illuminate\Support\Facades\Validator;
|
|
|
*/
|
|
*/
|
|
|
trait CanImportMenu
|
|
trait CanImportMenu
|
|
|
{
|
|
{
|
|
|
|
|
+ protected $menu = [];
|
|
|
|
|
+
|
|
|
protected $menuValidationRules = [
|
|
protected $menuValidationRules = [
|
|
|
'parent' => 'nullable',
|
|
'parent' => 'nullable',
|
|
|
'title' => 'required',
|
|
'title' => 'required',
|
|
@@ -17,6 +19,16 @@ trait CanImportMenu
|
|
|
'icon' => 'nullable',
|
|
'icon' => 'nullable',
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取菜单节点.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return array
|
|
|
|
|
+ */
|
|
|
|
|
+ protected function menu()
|
|
|
|
|
+ {
|
|
|
|
|
+ return $this->menu;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 添加菜单
|
|
* 添加菜单
|
|
|
*
|
|
*
|
|
@@ -24,8 +36,10 @@ trait CanImportMenu
|
|
|
*
|
|
*
|
|
|
* @throws \Exception
|
|
* @throws \Exception
|
|
|
*/
|
|
*/
|
|
|
- protected function addMenu(array $menu)
|
|
|
|
|
|
|
+ protected function addMenu(array $menu = [])
|
|
|
{
|
|
{
|
|
|
|
|
+ $menu = $menu ?: $this->menu();
|
|
|
|
|
+
|
|
|
if (! Arr::isAssoc($menu)) {
|
|
if (! Arr::isAssoc($menu)) {
|
|
|
foreach ($menu as $v) {
|
|
foreach ($menu as $v) {
|
|
|
$this->addMenu($v);
|
|
$this->addMenu($v);
|
|
@@ -47,11 +61,23 @@ trait CanImportMenu
|
|
|
'title' => $menu['title'],
|
|
'title' => $menu['title'],
|
|
|
'icon' => (string) ($icon ?? ''),
|
|
'icon' => (string) ($icon ?? ''),
|
|
|
'uri' => (string) ($menu['uri'] ?? ''),
|
|
'uri' => (string) ($menu['uri'] ?? ''),
|
|
|
- 'extension' => $this->getExtensionName(),
|
|
|
|
|
|
|
+ 'extension' => $this->getName(),
|
|
|
]);
|
|
]);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 刷新菜单.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @throws \Exception
|
|
|
|
|
+ */
|
|
|
|
|
+ protected function refreshMenu()
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->flushMenu();
|
|
|
|
|
+
|
|
|
|
|
+ $this->addMenu();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 根据名称获取菜单ID.
|
|
* 根据名称获取菜单ID.
|
|
|
*
|
|
*
|
|
@@ -69,14 +95,14 @@ trait CanImportMenu
|
|
|
|
|
|
|
|
return $menuModel::query()
|
|
return $menuModel::query()
|
|
|
->where('title', $parent)
|
|
->where('title', $parent)
|
|
|
- ->where('extension', $this->getExtensionName())
|
|
|
|
|
|
|
+ ->where('extension', $this->getName())
|
|
|
->value('id') ?: 0;
|
|
->value('id') ?: 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 删除菜单.
|
|
* 删除菜单.
|
|
|
*/
|
|
*/
|
|
|
- protected function deleteMenu()
|
|
|
|
|
|
|
+ protected function flushMenu()
|
|
|
{
|
|
{
|
|
|
$menuModel = $this->getMenuModel();
|
|
$menuModel = $this->getMenuModel();
|
|
|
|
|
|
|
@@ -85,7 +111,7 @@ trait CanImportMenu
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$menuModel::query()
|
|
$menuModel::query()
|
|
|
- ->where('extension', $this->getExtensionName())
|
|
|
|
|
|
|
+ ->where('extension', $this->getName())
|
|
|
->delete();
|
|
->delete();
|
|
|
}
|
|
}
|
|
|
|
|
|