NavLogic.php 3.5 KB
<?php

namespace App\Http\Logic\Bside\Nav;


use App\Http\Logic\Bside\BaseLogic;
use App\Models\Nav\BNav;
use App\Models\Nav\BNavGroup;
use App\Models\RouteMap\RouteMap;
use Illuminate\Support\Facades\DB;


/**
 * @remark :菜单管理
 * @name   :NavLogic
 * @author :lyh
 * @method :post
 * @time   :2023/8/23 11:14
 */
class NavLogic extends  BaseLogic
{
    public function __construct()
    {
        parent::__construct();
        $this->param = $this->requestAll;
        $this->model = new BNav();
    }

    /**
     * @remark :保存数据
     * @name   :navSave
     * @author :lyh
     * @method :post
     * @time   :2023/8/23 11:14
     */
    public function navSave()
    {
        if(!empty($this->param['location'])){
            if($this->param['location'] == 'header'){
                $this->param['group_id'] = BNavGroup::DEFAULT_HEADER_ID;
            }
            if($this->param['location'] == 'footer'){
                $this->param['group_id'] = BNavGroup::DEFAULT_FOOTER_ID;
            }
        }
        $this->param['image'] = str_replace_url(isset($this->param['image']) ? $this->param['image'] : '');
        $this->param['remark_image'] = str_replace_url(isset($this->param['remark_image']) ? $this->param['remark_image'] : '');
        if(isset($this->param['id']) && !empty($this->param['id'])){
            $this->handleEditParam();//验证是否可编辑分类
            $this->model->edit($this->param,['id'=>$this->param['id']]);
        }else{
            $this->param['project_id'] = $this->user['project_id'];
            $this->model->add($this->param);
        }
        //编辑菜单后,通知更新
        $this->updateNotify(['project_id'=>$this->user['project_id'], 'type'=>RouteMap::SOURCE_NAV, 'route'=>'all']);
        return $this->success();
    }

    /**
     * @remark :验证是否可编辑
     * @name   :handleEditParam
     * @author :lyh
     * @method :post
     * @time   :2023/9/7 14:36
     */
    public function handleEditParam(){
        $info = $this->model->read(['id'=>$this->param['id']]);
        if($this->param['pid'] == $info['id']){
            $this->fail('不允许成为自己的上级');
        }
        $pid_info = $this->model->read(['pid'=>$this->param['id']]);
        if(($pid_info !== false) && $this->param['pid'] != $info['pid']){
            $this->fail('当前菜单拥有子集不允许修改上级');
        }
        return $this->success();
    }

    /**
     * @remark :删除菜单
     * @name   :navDelete
     * @author :lyh
     * @method :post
     * @time   :2023/8/23 11:12
     */
    public function navDelete()
    {
        //判断当前菜单是否拥有下级
        $info = $this->model->read(['pid'=>$this->param['id']]);
        if($info !== false){
            $this->fail('当前菜单存在下级,不允许删除');
        }
        $rs = $this->model->del($this->param);
        if($rs === false){
            $this->fail('error');
        }
        //编辑菜单后,通知更新
        $this->updateNotify(['project_id'=>$this->user['project_id'], 'type'=>RouteMap::SOURCE_NAV, 'route'=>'all']);
        return $this->success();
    }


    /**
     * @remark :排序
     * @name   :navSort
     * @author :lyh
     * @method :post
     * @time   :2023/8/22 9:54
     */
    public function navSort(){
        $rs = $this->model->edit(['sort'=>$this->param['sort']],['id'=>$this->param['id']]);
        if($rs === false){
            $this->fail('error');
        }
        return $this->success();
    }
}