作者 邓超

x

... ... @@ -4,6 +4,7 @@ namespace App\Http\Controllers\Bside;
use App\Enums\Common\Code;
use App\Http\Logic\Bside\Nav\NavLogic;
use App\Http\Requests\Bside\Nav\NavRequest;
use App\Models\BNav;
... ... @@ -27,17 +28,8 @@ class NavController extends BaseController
*/
public function index(){
$isTree = $this->param['tree']??false;
// 显示位置
$location = $this->param['location']??'';
$lists = BNav::_all($this->user['project_id'],$location)->toArray();
if($isTree){
$lists = list_to_tree($lists);
}
return $this->success($lists);
return $this->success(NavLogic::instance()->list());
}
... ... @@ -53,33 +45,11 @@ class NavController extends BaseController
* @author:dc
* @time 2023/5/8 17:06
*/
private function save(NavRequest $request){
public function save(NavRequest $request){
$data = $request->validated();
if($data['pid']){
// 验证是否存在上级
$all = BNav::_all($this->user['project_id'],$data['location']);
if(!$all->where('id',$data['pid'])->count()){
return $this->response('上级栏目不存在',Code::SYSTEM_ERROR);
}
// 上级不允许是自己的下级
if(!empty($data['id'])){
$all = list_to_tree($all->toArray(),$data['id']);
$all = tree_to_list($all);
if(in_array($data['pid'],array_column($all,'id'))){
return $this->response('上级栏目不允许为本身的下级',Code::SYSTEM_ERROR);
}
}
}
return $this->success(NavLogic::instance()->save($data));
// 保存
$id = BNav::_save($this->user['project_id'],$data,$data['id']??0);
if($id===-1){
return $this->response('导航菜单不存在',Code::SYSTEM_ERROR);
}
return $this->success(BNav::_find($this->user['project_id'],$id,true));
}
... ... @@ -94,21 +64,9 @@ class NavController extends BaseController
$id = $request->validated()['id'];
$data = BNav::_find($this->user['project_id'],$id);
if(empty($data)){
return $this->response('导航菜单不存在',Code::SYSTEM_ERROR);
}
NavLogic::instance()->delete($id);
if(BNav::isChild($data['id'],$this->user['project_id'])){
return $this->response('存在下级无法删除',Code::SYSTEM_ERROR);
}
if($data->delete()){
return $this->response('删除成功',Code::SUCCESS);
}
return $this->response('删除成功');
}
... ...
<?php
namespace App\Http\Logic\Bside;
namespace App\Http\Logic\Bside\Nav;
use App\Helper\Arr;
use App\Enums\Common\Code;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\BNav;
use App\Models\Inquiry;
/**
* @author:dc
... ... @@ -21,6 +23,52 @@ class NavLogic extends BaseLogic
$this->model = new BNav();
}
/**
* @return array
* @author:dc
* @time 2023/5/12 9:23
*/
public function list(){
$where = [];
if(!empty($this->param['location'])){
$where[] = ['location','=',$this->param['location']];
}
$lists = $this->getList($where,['sort'=>'asc'],['*'],false);
$isTree = $this->param['tree']??false;
if($isTree){
$lists = list_to_tree($lists);
}
return $lists;
}
public function save($data)
{
if($data['pid']){
// 验证是否存在上级
$all = BNav::_all($this->user['project_id'],$data['location']);
if(!$all->where('id',$data['pid'])->count()){
$this->fail('上级栏目不存在');
}
// 上级不允许是自己的下级
if(!empty($data['id'])){
$all = list_to_tree($all->toArray(),$data['id']);
$all = tree_to_list($all);
if(in_array($data['pid'],array_column($all,'id'))){
$this->fail('上级栏目不允许为本身的下级');
}
}
}
// 保存
$id = parent::save($data);
return $this->getInfo($id['id']);
}
/**
* @param $ids
... ...