增加菜单到导出和导入;增加粘贴全局操作;优化表单错误表现;

This commit is contained in:
augushong
2024-10-15 16:06:36 +08:00
parent b04321e380
commit 008e781d6b
8 changed files with 185 additions and 3 deletions

View File

@@ -10,6 +10,7 @@ use app\admin\service\TriggerService;
use app\common\constants\MenuConstant;
use app\common\controller\AdminController;
use think\App;
use think\facade\Db;
/**
* Class Menu.
@@ -216,4 +217,68 @@ class MenuBase extends AdminController
'type' => 'success',
]);
}
/**
* @NodeAnotation(title="导出")
*/
public function export()
{
$list_menu_json = SystemMenu::select()->hidden([
'create_time',
'update_time',
'delete_time',
])->toJson();
$download = $this->request->param('download');
if ($download == 1) {
$file_name = 'admin_menu.json';
$file_name = $this->request->host() . '_' . $file_name;
return download($list_menu_json, $file_name, true, 0);
}
$this->assign('list_menu_json', $list_menu_json);
return $this->fetch();
}
/**
* @NodeAnotation(title="import")
*/
public function import()
{
if ($this->request->isPost()) {
$data = $this->request->post('data');
$data_arr = json_decode($data, true);
$rule = [
'pid|上级菜单' => 'require',
'title|菜单名称' => 'require',
'icon|菜单图标' => 'require',
];
foreach ($data_arr as $data_item) {
$this->validate($data_item, $rule);
}
Db::startTrans();
try {
$list_old_menu = SystemMenu::withTrashed()->select();
foreach ($list_old_menu as $menu) {
$menu->force()->delete();
}
foreach ($data_arr as $data_item) {
$menu = new SystemMenu();
$menu->save($data_item);
}
Db::commit();
} catch (\Throwable $th) {
Db::rollback();
$this->error('导入失败:' . $th->getMessage());
}
$this->success('导入成功');
}
return $this->fetch();
}
}