From f701fc87aceb2338be0c65eb4ee0d73a746eac46 Mon Sep 17 00:00:00 2001 From: liaofei <136327134@qq.com> Date: Mon, 10 Apr 2023 11:47:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B7=BB=E5=8A=A0=E5=88=86?= =?UTF-8?q?=E7=B1=BB=E4=B8=BA=E5=A4=9A=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/v1/setting/SystemRouteCate.php | 19 ++++++------------- .../system/SystemRouteCateServices.php | 19 ++++++++++++------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/crmeb/app/adminapi/controller/v1/setting/SystemRouteCate.php b/crmeb/app/adminapi/controller/v1/setting/SystemRouteCate.php index d5280b38d..985fe1d94 100644 --- a/crmeb/app/adminapi/controller/v1/setting/SystemRouteCate.php +++ b/crmeb/app/adminapi/controller/v1/setting/SystemRouteCate.php @@ -73,7 +73,7 @@ class SystemRouteCate extends AuthController public function save(Request $request) { $data = $request->postMore([ - ['pid', 0], + ['path', []], ['name', ''], ['sort', 0], ['app_name', ''], @@ -84,11 +84,9 @@ class SystemRouteCate extends AuthController } $data['add_time'] = time(); - $res = $this->services->save($data); - $path = $this->services->getPathValue($data['pid']); - $path = $this->services->setPathValue($path, $res->id); - $res->path = $path; - $res->save(); + $data['pid'] = $data['path'][count($data['path']) - 1] ?? 0; + $this->services->save($data); + return app('json')->success('保存成功'); @@ -117,7 +115,7 @@ class SystemRouteCate extends AuthController public function update(Request $request, $id) { $data = $request->postMore([ - ['pid', 0], + ['path', []], ['name', ''], ['sort', 0], ['app_name', ''], @@ -127,12 +125,7 @@ class SystemRouteCate extends AuthController return app('json')->fail('缺少分类名称'); } - $pid = $this->services->value($id, 'pid'); - if ($data['pid'] != $pid) { - $path = $this->services->getPathValue($data['pid']); - $data['path'] = $this->services->setPathValue($path, $id); - } - + $data['pid'] = $data['path'][count($data['path']) - 1] ?? 0; $this->services->update($id, $data); return app('json')->success('修改成功'); diff --git a/crmeb/app/services/system/SystemRouteCateServices.php b/crmeb/app/services/system/SystemRouteCateServices.php index 12f79166b..b8f25d299 100644 --- a/crmeb/app/services/system/SystemRouteCateServices.php +++ b/crmeb/app/services/system/SystemRouteCateServices.php @@ -37,12 +37,8 @@ class SystemRouteCateServices extends BaseServices $this->dao = $dao; } - public function getPathValue(int $pid) + public function getPathValue(array $path) { - if (!$pid) { - return []; - } - $path = $this->dao->value($pid, 'path'); $pathAttr = explode('/', $path); $pathData = []; foreach ($pathAttr as $item) { @@ -91,14 +87,23 @@ class SystemRouteCateServices extends BaseServices { $url = '/system/route_cate'; $cateInfo = []; + $path = []; if ($id) { $cateInfo = $this->dao->get($id); $cateInfo = $cateInfo ? $cateInfo->toArray() : []; $url .= '/' . $id; + $path = explode('/', $cateInfo['path']); + $newPath = []; + foreach ($path as $item) { + if ($item) { + $newPath[] = $item; + } + } + $path = $newPath; } - $options = $this->dao->selectList(['app_name' => $appName], 'name as label,id as value')->toArray(); + $options = $this->dao->selectList(['app_name' => $appName], 'name as label,id as value,id,pid')->toArray(); $rule = [ - FormBuilder::select('pid', '上级分类', (int)($cateInfo['pid'] ?? 0))->options($options), + FormBuilder::cascader('path', '上级分类', $path)->data(get_tree_children($options)), FormBuilder::input('name', '分类名称', $cateInfo['name'] ?? '')->required(), FormBuilder::number('sort', '排序', (int)($cateInfo['sort'] ?? 0)), FormBuilder::hidden('app_name', $appName)