FastAdmin菜单规则树形结构分类显示

控制器controller文件Classification.php

<?php

namespace app\admin\controller\classification;

use app\common\controller\Backend;
use fast\Tree;
use think\Db;
use app\admin\model\AuthRule;
use think\Cache;

/**
 * 模块分类管理
 *
 * @icon fa fa-circle-o
 */
class Classification extends Backend
{

    /**
     * Classification模型对象
     * @var \app\admin\model\classification\Classification
     */
    protected $model = null;
    protected $classificationlist = [];
    protected $multiFields = 'ismenu,status';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\classification\Classification;

        list($where, $sort, $order, $offset, $limit) = $this->buildparams();

        // 必须将结果集转换为数组
        $classificationList = \think\Db::name("classification")->field('createtime,updatetime', true)->where($where)->order('weigh DESC,id ASC')->select();

        $search_id_arr = [];
        $filter_arr = json_decode($this->request->get("filter"), true);
        $this->assignconfig("show", 0); // 1 支持搜索 搜索后正常展示数据 0 支持搜索 搜索后不展示数据只记录条数
        if ($filter_arr != []) {
            $this->assignconfig("show", 1);
        }

        foreach ($classificationList as $k => &$v) {
            $v['name'] = __($v['name']);
            if ($filter_arr != [] && isset($filter_arr['name'])) {
                $search_id_arr[] = $v['pid'];
            }
        }
        unset($v);
        Tree::instance()->init($classificationList)->icon = ['&nbsp;&nbsp;&nbsp;&nbsp;', '&nbsp;&nbsp;&nbsp;&nbsp;', '&nbsp;&nbsp;&nbsp;&nbsp;'];
        $this->classificationlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0, '', $search_id_arr), 'name');
        $classificationdata = [0 => __('None')];

        foreach ($this->classificationlist as $k => &$v) {
            if (!$v['ismenu']) {
                continue;
            }
            $classificationdata[$v['id']] = $v['name'];
            unset($v['spacer']);
        }
        unset($v);

        $this->view->assign('ruledata', $classificationdata);
        $this->view->assign("menutypeList", $this->model->getMenutypeList());

        $getPromotionPlatformList = Db::table('fa_classification')->where('pid', '0')->column('promotion_platform');
        $this->view->assign("promotionPlatformList", array_combine($getPromotionPlatformList, $getPromotionPlatformList));
        $this->view->assign("statusList", $this->model->getStatusList());

    }


    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $list = $this->classificationlist;
            $total = count($this->classificationlist);
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a", [], 'strip_tags');
            if ($params) {
                if (!$params['ismenu'] && !$params['pid']) {
                    $this->error(__('The non-menu rule must have parent'));
                }
                $result = $this->model->save($params);
                if ($result === false) {
                    $this->error($this->model->getError());
                }
                Cache::rm('__menu__');
                $this->success();
            }
            $this->error();
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        // 类不存在:app\common\validate\Classification

        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a", [], 'strip_tags');
            if ($params) {
                if (!$params['ismenu'] && !$params['pid']) {
                    $this->error(__('The non-menu rule must have parent'));
                }
                if ($params['pid'] == $row['id']) {
                    $this->error(__('Can not change the parent to self'));
                }
                if ($params['pid'] != $row['pid']) {
                    $childrenIds = Tree::instance()->init(collection(AuthRule::select())->toArray())->getChildrenIds($row['id']);
                    if (in_array($params['pid'], $childrenIds)) {
                        $this->error(__('Can not change the parent to child'));
                    }
                }

                //这里需要针对name做唯一验证
//                $ruleValidate = \think\Loader::validate('Classification');
//                $ruleValidate->rule([
//                    'name' => 'require|unique:AuthRule,name,' . $row->id,
//                ]);
                $result = $row->save($params);
                if ($result === false) {
                    $this->error($row->getError());
                }
                Cache::rm('__menu__');
                $this->success();
            }
            $this->error();
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        if ($ids) {
            $delIds = [];
            foreach (explode(',', $ids) as $k => $v) {
                $delIds = array_merge($delIds, Tree::instance()->getChildrenIds($v, true));
            }
            $delIds = array_unique($delIds);
            $count = $this->model->where('id', 'in', $delIds)->delete();
            if ($count) {
                Cache::rm('__menu__');
                $this->success();
            }
        }
        $this->error();
    }


}

模型model文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

withoutfear

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值