NavLogic.php 7.3 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 :获取当前id下所有子集
     * @name   :getSubList
     * @author :lyh
     * @method :post
     * @time   :2023/12/4 15:11
     */
    public function getSubList(){
        //编辑时
        if(isset($this->param['id']) && !empty($this->param['id'])) {
            $str = [];
            //排序掉当前id下所有子集
            $str = $this->getAllSub($this->param['id'], $str);
            $str[] = $this->param['id'];
            $this->param['id'] = ['not in', $str];
        }
        $this->param['project_id'] = $this->user['project_id'];
        $list = $this->model->list($this->param);
        $data = array();
        foreach ($list as $v){
            $v = (array)$v;
            if ($v['pid'] == 0) {
                $v['sub'] = _get_child($v['id'], $list);
                $data[]   = $v;
            }
        }
        return $this->success($data);
    }

    /**
     * @remark :获取当前id下所有子集
     * @name   :getAllSub
     * @author :lyh
     * @method :post
     * @time   :2023/10/18 15:10
     */
    public function getAllSub($id,&$str = []){
        $list = $this->model->list(['pid'=>$id,'status'=>1],['id','pid']);
        if(!empty($list)){
            foreach ($list as $v){
                $str[] = $v['id'];
                $this->getAllSub($v['id'],$str);
            }
        }
        return $str;
    }

    /**
     * @remark :保存数据
     * @name   :navSave
     * @author :lyh
     * @method :post
     * @time   :2023/8/23 11:14
     */
    public function navSave()
    {
        $data = $this->handleParam($this->param);//处理保存字符串
        if(!empty($this->param['location'])){
            if($this->param['location'] == 'header'){
                $data['group_id'] = BNavGroup::DEFAULT_HEADER_ID;
            }
            if($this->param['location'] == 'footer'){
                $data['group_id'] = BNavGroup::DEFAULT_FOOTER_ID;
            }
        }
        $data['image'] = str_replace_url(isset($this->param['image']) ? $this->param['image'] : '');
        $data['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($data,['id'=>$this->param['id']]);
        }else{
            $data['project_id'] = $this->user['project_id'];
            $this->model->add($data);
        }
        //编辑菜单后,通知更新
        $this->addUpdateNotify(RouteMap::SOURCE_NAV, 'all');
        return $this->success();
    }

    /**
     * @remark :保存时处理数据
     * @name   :saveHandleParam
     * @author :lyh
     * @method :post
     * @time   :2023/12/15 14:26
     */
    public function handleParam($param){
        $data = [
            'pid'=>$param['pid'] ?? 0,
            'name'=>$param['name'] ?? '',
            'location'=>$param['location'] ?? '',
            'url'=>$param['url'],
            'status'=>$param['status'] ?? 1,
            'target'=>$param['target'] ?? 1,
            'remark'=>$param['remark'] ?? '',
        ];
        return $this->success($data);
    }

    /**
     * @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('不允许成为自己的上级');
        }
        return $this->success();
    }

    /**
     * @remark :删除菜单
     * @name   :navDelete
     * @author :lyh
     * @method :post
     * @time   :2023/8/23 11:12
     */
    public function navDelete()
    {
        $str = [];
        //排序掉当前id下所有子集
        $str = $this->getAllSub($this->param['id'], $str);
        foreach ($str as $v){
            $this->model->del(['id'=>$v]);
        }
        //编辑菜单后,通知更新
        $this->addUpdateNotify(RouteMap::SOURCE_NAV, '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();
    }



    /**
     * 一键导入 对应分类
     * @throws \App\Exceptions\AsideGlobalException
     * @throws \App\Exceptions\BsideGlobalException
     */
    public function importNav(){
        $nav = $this->getInfo($this->param['id']);
        if(!in_array($nav['url'], array_keys(BNav::ableImportMap()))){
            $this->fail('该菜单不支持一键导入');
        }

        $class = BNav::ableImportMap($nav['url']);
        $category = new $class();
        $fields = ['id', 'name', 'pid', 'alias'];
        if($nav['url'] == 'products'){
            $fields = ['id', 'title as name', 'pid', 'route as alias'];
        }
        $this->addByPid($category, $nav, $fields);
        return $this->success();
    }

    /**
     * 一级一级的加
     * @author zbj
     * @date 2023/11/22
     */
    protected function addByPid($category, $nav, $fields, $pid = 0)
    {
        $nav_pid = $nav['id'];
        if($pid){
            $p_cate = $category->where('id', $pid)->select($fields)->first();
            if($p_cate){
                $nav_pid = $this->model->where('import_id', $nav['id'])->where('url', $p_cate['alias'])->value('id') ?: $nav_pid;
            }
        }
        $list = $category->list(['pid' => $pid], 'id', $fields, 'asc');
        $data = [];
        $time = date('Y-m-d H:i:s');
        foreach ($list as $item) {
            if(in_array($item['alias'], array_keys(BNav::ableImportMap()))){
                continue;
            }
            $exists_info = $this->model->where('import_id', $nav['id'])->where('url', $item['alias'])->first();
            if($exists_info){
                continue;
            }
            $data[] = [
                'pid' => $nav_pid,
                'name' => $item['name'],
                'url' => $item['alias'],
                'project_id' => $this->project['id'],
                'location' => $nav['location'],
                'group_id' => $nav['group_id'],
                'status' => BNav::STATUS_ACTIVE,
                'import_id' => $nav['id'],
                'created_at' => $time,
                'updated_at' => $time,
            ];
        }
        //每500条更一次
        $data_chunk = array_chunk($data,500);
        foreach ($data_chunk as $chunk){
            $this->model->insert($chunk);
        }

        foreach ($list as $item) {
            $this->addByPid($category, $nav, $fields, $item['id']);
        }
    }






}