zm
2021-03-25 d84ff6053b22269a6c59dc005e9efb8de6595988
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/**
 * link: http://www.zjhejiang.com/
 * copyright: Copyright (c) 2018 浙江禾匠信息科技有限公司
 * author: wxf
 */
 
namespace app\forms;
 
 
use app\forms\common\CommonOption;
use app\forms\common\CommonUser;
use app\models\Model;
use app\models\Option;
 
class MenusForm extends Model
{
    public $currentRouteInfo = [];
    public $currentRoute;
    public $pid = 0;
    public $id = 0;
 
    private $userIdentity;
    private $adminPermissions;
    private $permissionsStatus;
 
    //初始化数据,方便外部调用本类
    public function init()
    {
        $this->userIdentity = CommonUser::getUserIdentity();
        if ($this->userIdentity->is_admin == 1) {
            $arr = (CommonUser::getAdminInfo())->permissions;
            // 有些菜单上也有key标识、但无需进行删除
            $this->adminPermissions = array_merge(json_decode($arr, true), [
                'cache_manage', 'rule_user'
            ]);
            $this->permissionsStatus = CommonOption::get(
                Option::NAME_PERMISSIONS_STATUS,
                \Yii::$app->user->identity->mall_id,
                Option::GROUP_ADMIN,
                0
            );
        }
        parent::init(); // TODO: Change the autogenerated stub
    }
 
    /**
     * @param $type string 有效参数 admin|mall
     * @return mixed
     * @throws \Exception
     */
    public function getMenus($type)
    {
        if (!in_array($type, ['admin', 'mall', 'plugin'])) {
            throw new \Exception('type 传入参数无效');
        }
 
        if ($type === 'admin') {
            $menus = Menus::getAdminMenus();
        }
 
        if ($type === 'mall') {
            $menus = Menus::getMallMenus();
        }
 
        if ($type === 'plugin') {
            $plugin = \Yii::$app->plugin->currentPlugin;
            $menus = $plugin->getMenus();
        }
 
        // TODO 需要加入插件菜单
        $userPermissions = [];
        // 获取员工账号 权限路由
        if ($this->userIdentity->is_operator == 1) {
            $userPermissions = CommonUser::getUserPermissions();
        }
        // 多商户
        if (\Yii::$app->user->identity->mch_id) {
            $userPermissions = CommonUser::getMchPermissions();
        }
 
        // 标识菜单是否显示
        $checkMenus = $this->checkMenus($menus, $userPermissions);
 
        // 去除不需显示的菜单
        $menus = $this->deleteMenus($checkMenus);
 
        // 菜单列表
        $newMenus = $this->resetMenus($menus);
 
        return [
            'menus' => $newMenus,
            'currentRouteInfo' => $this->currentRouteInfo,
        ];
    }
 
    /**
     * 给自定义路由列表 追加ID 及 PID
     * @param array $list 自定义的多维路由数组
     * @param int $id 权限ID
     * @param int $pid 权限PID
     * @return mixed
     */
    private function resetMenus(array $list, &$id = 1, $pid = 0)
    {
        foreach ($list as $key => $item) {
            $list[$key]['id'] = (string)$id;
            $list[$key]['pid'] = (string)$pid;
 
            // 前端选中的菜单
            if (isset($list[$key]['route']) && $this->currentRoute === $list[$key]['route']) {
                $this->currentRouteInfo = $list[$key];
            }
 
            if (isset($item['children'])) {
                $id++;
                $list[$key]['children'] = $this->resetMenus($item['children'], $id, $id - 1);
            }
 
            if (isset($item['action'])) {
                $id++;
                $list[$key]['action'] = $this->resetMenus($item['action'], $id, $id - 1);
            }
 
            isset($item['children']) == false && isset($item['action']) == false ? $id++ : $id;
        }
 
        return $list;
    }
 
    /**
     * 根据权限 标识菜单是否可用
     * 2020年6月30日 16:18:36  开发给数据统计插件菜单使用
     * @param $menus
     * @param $permissions
     * @return mixed
     */
    public function checkMenus($menus, $permissions)
    {
        foreach ($menus as $k => $item) {
            if ($this->userIdentity->is_super_admin === 1) {
                // 超级管理员
                $menus[$k]['is_show'] = true;
            } elseif ($this->userIdentity->is_admin === 1) {
                // 子账号管理员
                $menus[$k]['is_show'] = true;
            } else {
                // 员工账号
                if ($item['route'] && in_array($item['route'], $permissions)) {
                    $menus[$k]['is_show'] = true;
                } else {
                    $menus[$k]['is_show'] = false;
                }
            }
 
            if (isset($item['children'])) {
                $menus[$k]['children'] = $this->checkMenus($item['children'], $permissions);
                foreach ($menus[$k]['children'] as $i) {
                    if ($i['is_show'] == true) {
                        $menus[$k]['route'] = $i['route'];
                        $menus[$k]['is_show'] = true;
                        break;
                    }
                }
            }
        }
 
        return $menus;
    }
 
    /**
     * 根据是否显示标识、去除不显示的菜单.
     * 2020年6月30日 16:18:36  开发给数据统计插件菜单使用
     * @param $menus
     * @param $permissions
     * @return mixed
     */
    public function deleteMenus($menus)
    {
        foreach ($menus as $k => $item) {
            if (isset($item['is_show']) && $item['is_show'] == false) {
                unset($menus[$k]);
                continue;
            }
            //子账号
            if ($this->userIdentity->is_admin === 1) {
                // 去除总管理才有的权限
                if (isset($item['key']) && in_array($item['key'], Menus::MALL_SUPER_ADMIN_KEY)) {
                    unset($menus[$k]);
                    continue;
                }
                //去除总账号未分配给子账号的菜单
                if (isset($item['key']) && !in_array($item['key'], $this->adminPermissions)) {
                    unset($menus[$k]);
                    continue;
                }
            }
 
            // 去除只允许多商户访问的路由
            if (\Yii::$app->user->identity->mch_id <= 0) {
                if (isset($item['key']) && in_array($item['key'], Menus::MALL_MCH_KEY)) {
                    unset($menus[$k]);
                    continue;
                }
            }
 
            //员工账号
            if ($this->userIdentity->is_operator === 1) {
                // 去除总管理才有的权限
                if (isset($item['key']) && in_array($item['key'], Menus::MALL_SUPER_ADMIN_KEY)) {
                    unset($menus[$k]);
                    continue;
                }
            }
 
            if (isset($item['children'])) {
                $menus[$k]['children'] = $this->deleteMenus($item['children']);
            }
        }
        $menus = array_values($menus);
        return $menus;
    }
}