审查视图

application/common/library/Menu.php 3.4 KB
Karson authored
1 2 3 4 5 6
<?php

namespace app\common\library;

use app\admin\model\AuthRule;
use fast\Tree;
7
use think\Exception;
Karson authored
8 9 10 11 12 13 14 15 16 17 18 19
use think\exception\PDOException;

class Menu
{

    /**
     * 创建菜单
     * @param array $menu
     * @param mixed $parent 父类的name或pid
     */
    public static function create($menu, $parent = 0)
    {
20
        if (!is_numeric($parent)) {
Karson authored
21 22
            $parentRule = AuthRule::getByName($parent);
            $pid = $parentRule ? $parentRule['id'] : 0;
23
        } else {
Karson authored
24 25 26
            $pid = $parent;
        }
        $allow = array_flip(['file', 'name', 'title', 'icon', 'condition', 'remark', 'ismenu']);
27
        foreach ($menu as $k => $v) {
Karson authored
28 29 30 31 32 33 34 35
            $hasChild = isset($v['sublist']) && $v['sublist'] ? true : false;

            $data = array_intersect_key($v, $allow);

            $data['ismenu'] = isset($data['ismenu']) ? $data['ismenu'] : ($hasChild ? 1 : 0);
            $data['icon'] = isset($data['icon']) ? $data['icon'] : ($hasChild ? 'fa fa-list' : 'fa fa-circle-o');
            $data['pid'] = $pid;
            $data['status'] = 'normal';
36
            try {
Karson authored
37
                $menu = AuthRule::create($data);
38
                if ($hasChild) {
Karson authored
39 40
                    self::create($v['sublist'], $menu->id);
                }
41
            } catch (PDOException $e) {
42
                throw new Exception($e->getMessage());
Karson authored
43 44 45 46 47 48
            }
        }
    }

    /**
     * 删除菜单
49
     * @param string $name 规则name
Karson authored
50 51 52 53
     * @return boolean
     */
    public static function delete($name)
    {
Karson authored
54
        $ids = self::getAuthRuleIdsByName($name);
55
        if (!$ids) {
Karson authored
56 57 58 59 60
            return false;
        }
        AuthRule::destroy($ids);
        return true;
    }
61
Karson authored
62 63 64 65 66 67 68 69
    /**
     * 启用菜单
     * @param string $name
     * @return boolean
     */
    public static function enable($name)
    {
        $ids = self::getAuthRuleIdsByName($name);
70
        if (!$ids) {
Karson authored
71 72 73 74 75
            return false;
        }
        AuthRule::where('id', 'in', $ids)->update(['status' => 'normal']);
        return true;
    }
76
Karson authored
77 78 79 80 81 82 83 84
    /**
     * 禁用菜单
     * @param string $name
     * @return boolean
     */
    public static function disable($name)
    {
        $ids = self::getAuthRuleIdsByName($name);
85
        if (!$ids) {
Karson authored
86 87 88 89 90 91 92
            return false;
        }
        AuthRule::where('id', 'in', $ids)->update(['status' => 'hidden']);
        return true;
    }

    /**
93 94 95 96 97 98 99
     * 导出指定名称的菜单规则
     * @param string $name
     * @return array
     */
    public static function export($name)
    {
        $ids = self::getAuthRuleIdsByName($name);
100
        if (!$ids) {
101 102 103 104
            return [];
        }
        $menuList = [];
        $menu = AuthRule::getByName($name);
105
        if ($menu) {
106 107 108 109 110 111 112
            $ruleList = collection(AuthRule::where('id', 'in', $ids)->select())->toArray();
            $menuList = Tree::instance()->init($ruleList)->getTreeArray($menu['id']);
        }
        return $menuList;
    }

    /**
Karson authored
113 114 115 116 117 118 119
     * 根据名称获取规则IDS
     * @param string $name
     * @return array
     */
    public static function getAuthRuleIdsByName($name)
    {
        $ids = [];
Karson authored
120
        $menu = AuthRule::getByName($name);
121
        if ($menu) {
Karson authored
122
            // 必须将结果集转换为数组
123
            $ruleList = collection(AuthRule::order('weigh', 'desc')->field('id,pid,name')->select())->toArray();
Karson authored
124 125 126
            // 构造菜单数据
            $ids = Tree::instance()->init($ruleList)->getChildrenIds($menu['id'], true);
        }
Karson authored
127
        return $ids;
Karson authored
128 129 130
    }

}