NavController.php 4.0 KB
<?php

namespace App\Http\Controllers\Bside;


use App\Enums\Common\Code;
use App\Http\Requests\Bside\Nav\NavRequest;
use App\Models\BNav;
use Illuminate\Validation\Validator;

/**
 * 导航栏目 b端编辑 c端显示
 * @author:dc
 * @time 2023/5/8 16:31
 * Class NavController
 * @package App\Http\Controllers\Bside
 */
class NavController extends BaseController
{


    /**
     * 列表数据
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     * @author:dc
     * @time 2023/5/8 16:37
     */
    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);

    }



    /**
     * 创建导航栏
     * @author:dc
     * @time 2023/5/8 16:39
     */
    public function create(){
        return $this->save();
    }


    /**
     * 修改
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     * @author:dc
     * @time 2023/5/8 17:06
     */
    public function update(){
        return $this->save(true);
    }

    /**
     * 新增修改
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     * @author:dc
     * @time 2023/5/8 17:06
     */
    private function save($edit=false){

        $data = $this->verify(NavRequest::class,$edit?NavRequest::$UPDATE:null);

        if($data['pid']){
            // 验证是否存在上级
            $all = BNav::_all($this->user['project_id'],$data['location']);
            if(!$all->where('id',$data['pid'])->count()){
                return $this->response('上级栏目不存在','B_NAV_PID_NOTFOUND');
            }
            // 上级不允许是自己的下级
            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('上级栏目不允许为本身的下级','B_NAV_PID_IS_CHILD');
                }
            }
        }

        // 保存
        $id = BNav::_save($this->user['project_id'],$data,$data['id']??0);

        if($id===-1){
            return $this->response('导航菜单不存在','B_NAV_NOTFOUND');
        }

        return $this->success(BNav::_find($this->user['project_id'],$id,true));
    }


    /**
     * 删除数据
     * @return \Illuminate\Http\JsonResponse
     * @author:dc
     * @time 2023/5/9 9:20
     */
    public function delete(){
        $id = $this->param['id']??0;
        $data   =   BNav::_find($this->user['project_id'],$id);

        if(empty($data)){
            return $this->response('导航菜单不存在','B_NAV_NOTFOUND');
        }


        if(BNav::isChild($data['id'],$this->user['project_id'])){
            return $this->response('存在下级无法删除','B_NAV_DELETE_CHILD');
        }


        if($data->delete()){
            return $this->response('删除成功',Code::SUCCESS);
        }

    }


    /**
     * @author:dc
     * @time 2023/5/9 16:14
     */
    public function urls(){
        // todo::需要配合 c端来
        return $this->success([
           [
               'url'    =>  '/',
               'name'    =>  '首页'
           ],
           [
               'url'    =>  '/list',
               'name'    =>  '列表'
           ],
           [
               'url'    =>  '/page',
               'name'    =>  '单页'
           ],
           [
               'url'    =>  '/goods',
               'name'    =>  '商品'
           ],
        ]);
    }








}