Browse Source

修改添加分类为多级

liaofei 2 years ago
parent
commit
f701fc87ac

+ 6 - 13
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('修改成功');

+ 12 - 7
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)