Эх сурвалжийг харах

修改接口:修改菜单、获取菜单列表

FengR 3 долоо хоног өмнө
parent
commit
90d453be00

+ 49 - 6
app/JsonRpc/AuthorityService.php

@@ -78,7 +78,7 @@ class AuthorityService implements AuthorityServiceInterface
         try{
             $child_key = 0;
             if($data['pid_arr'] != $menu['pid_arr']){
-                $all_child_pid = Menu::where('pid','!=',0)->where('id','!=',$where['id'])->select('pid_arr','id','pid')->orderBy('pid')->get()->toArray();
+                $all_child_pid = Menu::where('pid','!=',0)->where('id','!=',$where['id'])->select('pid_arr','id','pid')->orderByRaw("JSON_LENGTH(pid_arr)")->get()->toArray();
                 if($data['pid'] == 0){
                     $pid_arr = []; 
                 }
@@ -99,6 +99,11 @@ class AuthorityService implements AuthorityServiceInterface
                             $one_pidarr[$count_childarr] = array_merge($pid_arr,[intval($where['id'])]);
                             $pid_arrnum[$child_key] = $count_childarr;
                         }else{
+                            // 确保 $child_data[$value['pid']] 已定义
+                            if(!isset($child_data[$value['pid']])){
+                                // 若未定义,先初始化为空数组
+                                $child_data[$value['pid']] = [];
+                            }
                             if(in_array($count_childarr,$pid_arrnum)){
                                 $child_data[$value['id']] = array_merge($one_pidarr[$count_childarr-1],[intval($value['pid'])]);
                             }else{
@@ -113,6 +118,7 @@ class AuthorityService implements AuthorityServiceInterface
                     }
                 }
                 $caseSql = "CASE {$caseSql} END";
+                // Db::rollBack();
                 // return Result::success($child_data);
                 $affectedRows = Menu::whereIn('id', $child_ids)
                     ->update([
@@ -120,12 +126,21 @@ class AuthorityService implements AuthorityServiceInterface
                     ]);
                 if($affectedRows != count($child_ids)){
                     Db::rollBack();
-                    throw new \Exception('更新子菜单失败');
+                    return Result::error('更新子菜单失败');
+                }
+                $result = Menu::where($where)->update($data);
+                if(empty($result)){
+                    Db::rollBack();
+                    return Result::error('更新菜单失败');
                 }
+                Db::commit();
+                return Result::success($result);
+            }else{
+                // 当 pid_arr 没有变化时,也要执行更新并返回结果
                 $result = Menu::where($where)->update($data);
                 if(empty($result)){
                     Db::rollBack();
-                    throw new \Exception('更新菜单失败');
+                    return Result::error('更新菜单失败');
                 }
                 Db::commit();
                 return Result::success($result);
@@ -211,9 +226,37 @@ class AuthorityService implements AuthorityServiceInterface
      */
     public function getAllMenuList(array $data): array
     {
-        $result = Menu::get();
-        if($result){
-            return Result::success($result);
+
+        // $all_menu = Menu::get();
+        // return Result::success($data);
+        $top_menu =  Menu::where('pid',0)->pluck('id')->toArray();
+        $pid_arr =  Menu::where('pid', '!=', 0)
+            ->where('url', '<>', '')
+            ->select('pid_arr','id')
+            ->get()
+            ->toArray();
+        // return Result::success($pid_arr);
+        $child_ids = array_column($pid_arr,'id');
+        $pids = array_column($pid_arr,'pid_arr');
+        $all_pid = [];
+        foreach ($pids as $key => $val) {
+            $value = json_decode($val, true) ?: [];
+            if (is_array($value)) {
+                $all_pid = array_merge($all_pid,$value);
+            }
+        }
+        $all_pid = array_values(array_unique($all_pid));
+        $all_ids = array_merge($child_ids,$all_pid);
+        // return Result::success($child_ids);
+        $all_menu =  Menu::whereIn('id',$all_ids)->get()->toArray();
+        // $all_menuids = array_column($all_menu,'id');
+        if($all_menu){
+            // return Result::success([
+            //     'child_ids'=>$child_ids,
+            //     'all_pid'=>$all_pid,
+            //     'all_menuids'=>$all_menuids,
+            // ]);
+            return Result::success($all_menu);
         }else{
             return Result::error("没有权限",0,[]);
         }