作者 邓超

x

@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Bside; @@ -4,6 +4,7 @@ namespace App\Http\Controllers\Bside;
4 4
5 5
6 use App\Enums\Common\Code; 6 use App\Enums\Common\Code;
  7 +use App\Http\Logic\Bside\Nav\NavLogic;
7 use App\Http\Requests\Bside\Nav\NavRequest; 8 use App\Http\Requests\Bside\Nav\NavRequest;
8 use App\Models\BNav; 9 use App\Models\BNav;
9 10
@@ -27,17 +28,8 @@ class NavController extends BaseController @@ -27,17 +28,8 @@ class NavController extends BaseController
27 */ 28 */
28 public function index(){ 29 public function index(){
29 30
30 - $isTree = $this->param['tree']??false;  
31 - // 显示位置  
32 - $location = $this->param['location']??'';  
33 31
34 - $lists = BNav::_all($this->user['project_id'],$location)->toArray();  
35 -  
36 - if($isTree){  
37 - $lists = list_to_tree($lists);  
38 - }  
39 -  
40 - return $this->success($lists); 32 + return $this->success(NavLogic::instance()->list());
41 33
42 } 34 }
43 35
@@ -53,33 +45,11 @@ class NavController extends BaseController @@ -53,33 +45,11 @@ class NavController extends BaseController
53 * @author:dc 45 * @author:dc
54 * @time 2023/5/8 17:06 46 * @time 2023/5/8 17:06
55 */ 47 */
56 - private function save(NavRequest $request){ 48 + public function save(NavRequest $request){
57 $data = $request->validated(); 49 $data = $request->validated();
58 50
59 - if($data['pid']){  
60 - // 验证是否存在上级  
61 - $all = BNav::_all($this->user['project_id'],$data['location']);  
62 - if(!$all->where('id',$data['pid'])->count()){  
63 - return $this->response('上级栏目不存在',Code::SYSTEM_ERROR);  
64 - }  
65 - // 上级不允许是自己的下级  
66 - if(!empty($data['id'])){  
67 - $all = list_to_tree($all->toArray(),$data['id']);  
68 - $all = tree_to_list($all);  
69 - if(in_array($data['pid'],array_column($all,'id'))){  
70 - return $this->response('上级栏目不允许为本身的下级',Code::SYSTEM_ERROR);  
71 - }  
72 - }  
73 - }  
74 -  
75 - // 保存  
76 - $id = BNav::_save($this->user['project_id'],$data,$data['id']??0);  
77 -  
78 - if($id===-1){  
79 - return $this->response('导航菜单不存在',Code::SYSTEM_ERROR);  
80 - }  
81 -  
82 - return $this->success(BNav::_find($this->user['project_id'],$id,true)); 51 + return $this->success(NavLogic::instance()->save($data));
  52 +
83 } 53 }
84 54
85 55
@@ -94,21 +64,9 @@ class NavController extends BaseController @@ -94,21 +64,9 @@ class NavController extends BaseController
94 64
95 $id = $request->validated()['id']; 65 $id = $request->validated()['id'];
96 66
97 - $data = BNav::_find($this->user['project_id'],$id);  
98 -  
99 - if(empty($data)){  
100 - return $this->response('导航菜单不存在',Code::SYSTEM_ERROR);  
101 - }  
102 -  
103 -  
104 - if(BNav::isChild($data['id'],$this->user['project_id'])){  
105 - return $this->response('存在下级无法删除',Code::SYSTEM_ERROR);  
106 - }  
107 - 67 + NavLogic::instance()->delete($id);
108 68
109 - if($data->delete()){  
110 - return $this->response('删除成功',Code::SUCCESS);  
111 - } 69 + return $this->response('删除成功');
112 70
113 } 71 }
114 72
1 <?php 1 <?php
2 2
3 -namespace App\Http\Logic\Bside; 3 +namespace App\Http\Logic\Bside\Nav;
4 4
5 -use App\Helper\Arr; 5 +
  6 +use App\Enums\Common\Code;
  7 +use App\Http\Logic\Bside\BaseLogic;
6 use App\Models\BNav; 8 use App\Models\BNav;
7 -use App\Models\Inquiry; 9 +
8 10
9 /** 11 /**
10 * @author:dc 12 * @author:dc
@@ -21,6 +23,52 @@ class NavLogic extends BaseLogic @@ -21,6 +23,52 @@ class NavLogic extends BaseLogic
21 $this->model = new BNav(); 23 $this->model = new BNav();
22 } 24 }
23 25
  26 + /**
  27 + * @return array
  28 + * @author:dc
  29 + * @time 2023/5/12 9:23
  30 + */
  31 + public function list(){
  32 + $where = [];
  33 + if(!empty($this->param['location'])){
  34 + $where[] = ['location','=',$this->param['location']];
  35 + }
  36 +
  37 + $lists = $this->getList($where,['sort'=>'asc'],['*'],false);
  38 +
  39 + $isTree = $this->param['tree']??false;
  40 +
  41 + if($isTree){
  42 + $lists = list_to_tree($lists);
  43 + }
  44 +
  45 + return $lists;
  46 + }
  47 +
  48 +
  49 + public function save($data)
  50 + {
  51 + if($data['pid']){
  52 + // 验证是否存在上级
  53 + $all = BNav::_all($this->user['project_id'],$data['location']);
  54 + if(!$all->where('id',$data['pid'])->count()){
  55 + $this->fail('上级栏目不存在');
  56 + }
  57 + // 上级不允许是自己的下级
  58 + if(!empty($data['id'])){
  59 + $all = list_to_tree($all->toArray(),$data['id']);
  60 + $all = tree_to_list($all);
  61 + if(in_array($data['pid'],array_column($all,'id'))){
  62 + $this->fail('上级栏目不允许为本身的下级');
  63 + }
  64 + }
  65 + }
  66 +
  67 + // 保存
  68 + $id = parent::save($data);
  69 +
  70 + return $this->getInfo($id['id']);
  71 + }
24 72
25 /** 73 /**
26 * @param $ids 74 * @param $ids