quanwei
18 hours ago c441dea81bd86bdfb12dff35821fed51f4cc91c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
 
namespace app\operations\model\plus\operations;
 
use app\common\model\plus\operations\Access as AccessModel;
 
/**
 * 区域代理权限模型
 */
class Access extends AccessModel
{
    /**
     * 获取权限列表
     */
    public function getList()
    {
        $all = static::getAll();
        $res = $this->recursiveMenuArray($all, 0);
        return array_values($this->foo($res));
    }
 
    /**
     * 新增记录
     */
    public function add($data)
    {
        // 校验路径
        if(!$this->validate($data)){
            return false;
        }
        $data['access_id'] = time();
        $data['app_id'] = self::$app_id;
        return $this->save($data);
    }
 
    /**
     * 更新记录
     */
    public function edit($data)
    {
        if ($data['access_id'] == $data['parent_id']) {
            $this->error = '上级菜单不允许设置为当前菜单';
            return false;
        }
        // 判断上级角色是否为当前子级
        if ($data['parent_id'] > 0) {
            // 获取所有上级id集
            $parentIds = $this->getTopAccessIds($data['parent_id']);
            if (in_array($data['access_id'], $parentIds)) {
                $this->error = '上级菜单不允许设置为当前子菜单';
                return false;
            }
        }
        // 校验路径,不限制大小写
        if(strtolower($data['path']) !== strtolower($this['path'])){
            if(!$this->validate($data)){
                return false;
            }
        }
 
        $data['redirect_name'] = ($data['is_route'] == 1) ? $data['redirect_name'] : '';
        $data['icon'] = $data['icon'];
        return $this->save($data);
    }
 
    /**
     * 验证
     */
    private function validate($data){
         $count = $this->where(['path' => $data['path']])->count();
         if($count > 0){
             $this->error = '路径已存在,请重新更改';
             return false;
         }
         return true;
    }
 
    public function getChildCount($where)
    {
        return $this->where($where)->count();
    }
 
    /**
     * 删除权限
     */
    public function remove($access_id)
    {
        return $this->where('access_id', '=', $access_id)->delete();
    }
 
    /**
     * 获取所有上级id集
     */
    public function getTopAccessIds($access_id, &$all = null)
    {
        static $ids = [];
        is_null($all) && $all = $this->getAll();
 
        foreach ($all as $item) {
            if ($item['access_id'] == $access_id && $item['parent_id'] > 0) {
                $ids[] = $item['parent_id'];
                $this->getTopAccessIds($item['parent_id'], $all);
            }
        }
 
        return $ids;
    }
 
    /**
     * 递归获取获取分类
     */
    public function recursiveMenuArray($data, $pid)
    {
        $re_data = [];
        foreach ($data as $key => $value) {
            if ($value['parent_id'] == $pid) {
                $re_data[$value['access_id']] = $value;
                $re_data[$value['access_id']]['children'] = $this->recursiveMenuArray($data, $value['access_id']);
            } else {
                continue;
            }
        }
        return $re_data;
    }
 
    /**
     * 格式化递归数组下标
     */
    public function foo(&$ar)
    {
        if (!is_array($ar)) return;
        foreach ($ar as $k => &$v) {
            if (is_array($v)) $this->foo($v);
            if ($k == 'children') $v = array_values($v);
        }
        return $ar;
    }
 
    /**
     * 更改显示状态
     */
    public function status($status){
        return $this->save([
            'is_show' => $status
        ]);
    }
 
    /**
     * 获取所有插件
     */
    public static function getAllPlus(){
        $model = new static();
        $plus = $model->where('path', '=', '/plus/plus/index')->find();
        return $model->where('parent_id', '=', $plus['access_id'])
            ->where('plus_category_id', '=', 0)
            ->select();
    }
 
    /**
     * 保存插件分类
     * @param $data
     */
    public function addPlus($data){
        $model = new self();
        return $model->where('access_id', '=', $data['access_id'])->save([
            'plus_category_id' => $data['plus_category_id']
        ]);
    }
}