commit | author | age
|
90c639
|
1 |
<?php |
Z |
2 |
/** |
|
3 |
* link: http://www.zjhejiang.com/ |
|
4 |
* copyright: Copyright (c) 2018 浙江禾匠信息科技有限公司 |
|
5 |
* author: wxf |
|
6 |
*/ |
|
7 |
|
|
8 |
namespace app\forms; |
|
9 |
|
|
10 |
|
|
11 |
use app\forms\common\CommonOption; |
|
12 |
use app\forms\common\CommonUser; |
|
13 |
use app\models\Model; |
|
14 |
use app\models\Option; |
|
15 |
|
|
16 |
class MenusForm extends Model |
|
17 |
{ |
|
18 |
public $currentRouteInfo = []; |
|
19 |
public $currentRoute; |
|
20 |
public $pid = 0; |
|
21 |
public $id = 0; |
|
22 |
|
|
23 |
private $userIdentity; |
|
24 |
private $adminPermissions; |
|
25 |
private $permissionsStatus; |
|
26 |
|
|
27 |
//初始化数据,方便外部调用本类 |
|
28 |
public function init() |
|
29 |
{ |
|
30 |
$this->userIdentity = CommonUser::getUserIdentity(); |
|
31 |
if ($this->userIdentity->is_admin == 1) { |
|
32 |
$arr = (CommonUser::getAdminInfo())->permissions; |
|
33 |
// 有些菜单上也有key标识、但无需进行删除 |
|
34 |
$this->adminPermissions = array_merge(json_decode($arr, true), [ |
|
35 |
'cache_manage', 'rule_user' |
|
36 |
]); |
|
37 |
$this->permissionsStatus = CommonOption::get( |
|
38 |
Option::NAME_PERMISSIONS_STATUS, |
|
39 |
\Yii::$app->user->identity->mall_id, |
|
40 |
Option::GROUP_ADMIN, |
|
41 |
0 |
|
42 |
); |
|
43 |
} |
|
44 |
parent::init(); // TODO: Change the autogenerated stub |
|
45 |
} |
|
46 |
|
|
47 |
/** |
|
48 |
* @param $type string 有效参数 admin|mall |
|
49 |
* @return mixed |
|
50 |
* @throws \Exception |
|
51 |
*/ |
|
52 |
public function getMenus($type) |
|
53 |
{ |
|
54 |
if (!in_array($type, ['admin', 'mall', 'plugin'])) { |
|
55 |
throw new \Exception('type 传入参数无效'); |
|
56 |
} |
|
57 |
|
|
58 |
if ($type === 'admin') { |
|
59 |
$menus = Menus::getAdminMenus(); |
|
60 |
} |
|
61 |
|
|
62 |
if ($type === 'mall') { |
|
63 |
$menus = Menus::getMallMenus(); |
|
64 |
} |
|
65 |
|
|
66 |
if ($type === 'plugin') { |
|
67 |
$plugin = \Yii::$app->plugin->currentPlugin; |
|
68 |
$menus = $plugin->getMenus(); |
|
69 |
} |
|
70 |
|
|
71 |
// TODO 需要加入插件菜单 |
|
72 |
$userPermissions = []; |
|
73 |
// 获取员工账号 权限路由 |
|
74 |
if ($this->userIdentity->is_operator == 1) { |
|
75 |
$userPermissions = CommonUser::getUserPermissions(); |
|
76 |
} |
|
77 |
// 多商户 |
|
78 |
if (\Yii::$app->user->identity->mch_id) { |
|
79 |
$userPermissions = CommonUser::getMchPermissions(); |
|
80 |
} |
|
81 |
|
|
82 |
// 标识菜单是否显示 |
|
83 |
$checkMenus = $this->checkMenus($menus, $userPermissions); |
|
84 |
|
|
85 |
// 去除不需显示的菜单 |
|
86 |
$menus = $this->deleteMenus($checkMenus); |
|
87 |
|
|
88 |
// 菜单列表 |
|
89 |
$newMenus = $this->resetMenus($menus); |
|
90 |
|
|
91 |
return [ |
|
92 |
'menus' => $newMenus, |
|
93 |
'currentRouteInfo' => $this->currentRouteInfo, |
|
94 |
]; |
|
95 |
} |
|
96 |
|
|
97 |
/** |
|
98 |
* 给自定义路由列表 追加ID 及 PID |
|
99 |
* @param array $list 自定义的多维路由数组 |
|
100 |
* @param int $id 权限ID |
|
101 |
* @param int $pid 权限PID |
|
102 |
* @return mixed |
|
103 |
*/ |
|
104 |
private function resetMenus(array $list, &$id = 1, $pid = 0) |
|
105 |
{ |
|
106 |
foreach ($list as $key => $item) { |
|
107 |
$list[$key]['id'] = (string)$id; |
|
108 |
$list[$key]['pid'] = (string)$pid; |
|
109 |
|
|
110 |
// 前端选中的菜单 |
|
111 |
if (isset($list[$key]['route']) && $this->currentRoute === $list[$key]['route']) { |
|
112 |
$this->currentRouteInfo = $list[$key]; |
|
113 |
} |
|
114 |
|
|
115 |
if (isset($item['children'])) { |
|
116 |
$id++; |
|
117 |
$list[$key]['children'] = $this->resetMenus($item['children'], $id, $id - 1); |
|
118 |
} |
|
119 |
|
|
120 |
if (isset($item['action'])) { |
|
121 |
$id++; |
|
122 |
$list[$key]['action'] = $this->resetMenus($item['action'], $id, $id - 1); |
|
123 |
} |
|
124 |
|
|
125 |
isset($item['children']) == false && isset($item['action']) == false ? $id++ : $id; |
|
126 |
} |
|
127 |
|
|
128 |
return $list; |
|
129 |
} |
|
130 |
|
|
131 |
/** |
|
132 |
* 根据权限 标识菜单是否可用 |
|
133 |
* 2020年6月30日 16:18:36 开发给数据统计插件菜单使用 |
|
134 |
* @param $menus |
|
135 |
* @param $permissions |
|
136 |
* @return mixed |
|
137 |
*/ |
|
138 |
public function checkMenus($menus, $permissions) |
|
139 |
{ |
|
140 |
foreach ($menus as $k => $item) { |
|
141 |
if ($this->userIdentity->is_super_admin === 1) { |
|
142 |
// 超级管理员 |
|
143 |
$menus[$k]['is_show'] = true; |
|
144 |
} elseif ($this->userIdentity->is_admin === 1) { |
|
145 |
// 子账号管理员 |
|
146 |
$menus[$k]['is_show'] = true; |
|
147 |
} else { |
|
148 |
// 员工账号 |
|
149 |
if ($item['route'] && in_array($item['route'], $permissions)) { |
|
150 |
$menus[$k]['is_show'] = true; |
|
151 |
} else { |
|
152 |
$menus[$k]['is_show'] = false; |
|
153 |
} |
|
154 |
} |
|
155 |
|
|
156 |
if (isset($item['children'])) { |
|
157 |
$menus[$k]['children'] = $this->checkMenus($item['children'], $permissions); |
|
158 |
foreach ($menus[$k]['children'] as $i) { |
|
159 |
if ($i['is_show'] == true) { |
|
160 |
$menus[$k]['route'] = $i['route']; |
|
161 |
$menus[$k]['is_show'] = true; |
|
162 |
break; |
|
163 |
} |
|
164 |
} |
|
165 |
} |
|
166 |
} |
|
167 |
|
|
168 |
return $menus; |
|
169 |
} |
|
170 |
|
|
171 |
/** |
|
172 |
* 根据是否显示标识、去除不显示的菜单. |
|
173 |
* 2020年6月30日 16:18:36 开发给数据统计插件菜单使用 |
|
174 |
* @param $menus |
|
175 |
* @param $permissions |
|
176 |
* @return mixed |
|
177 |
*/ |
|
178 |
public function deleteMenus($menus) |
|
179 |
{ |
|
180 |
foreach ($menus as $k => $item) { |
|
181 |
if (isset($item['is_show']) && $item['is_show'] == false) { |
|
182 |
unset($menus[$k]); |
|
183 |
continue; |
|
184 |
} |
|
185 |
//子账号 |
|
186 |
if ($this->userIdentity->is_admin === 1) { |
|
187 |
// 去除总管理才有的权限 |
|
188 |
if (isset($item['key']) && in_array($item['key'], Menus::MALL_SUPER_ADMIN_KEY)) { |
|
189 |
unset($menus[$k]); |
|
190 |
continue; |
|
191 |
} |
|
192 |
//去除总账号未分配给子账号的菜单 |
|
193 |
if (isset($item['key']) && !in_array($item['key'], $this->adminPermissions)) { |
|
194 |
unset($menus[$k]); |
|
195 |
continue; |
|
196 |
} |
|
197 |
} |
|
198 |
|
|
199 |
// 去除只允许多商户访问的路由 |
|
200 |
if (\Yii::$app->user->identity->mch_id <= 0) { |
|
201 |
if (isset($item['key']) && in_array($item['key'], Menus::MALL_MCH_KEY)) { |
|
202 |
unset($menus[$k]); |
|
203 |
continue; |
|
204 |
} |
|
205 |
} |
|
206 |
|
|
207 |
//员工账号 |
|
208 |
if ($this->userIdentity->is_operator === 1) { |
|
209 |
// 去除总管理才有的权限 |
|
210 |
if (isset($item['key']) && in_array($item['key'], Menus::MALL_SUPER_ADMIN_KEY)) { |
|
211 |
unset($menus[$k]); |
|
212 |
continue; |
|
213 |
} |
|
214 |
} |
|
215 |
|
|
216 |
if (isset($item['children'])) { |
|
217 |
$menus[$k]['children'] = $this->deleteMenus($item['children']); |
|
218 |
} |
|
219 |
} |
|
220 |
$menus = array_values($menus); |
|
221 |
return $menus; |
|
222 |
} |
|
223 |
} |