| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace app\traits\cms; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use app\model\Menu; | 
					
						
							| 
									
										
										
										
											2021-11-30 15:26:38 +08:00
										 |  |  | use tauthz\facade\Enforcer; | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | use think\Collection; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | trait MenuTrait | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // 获取菜单列表
 | 
					
						
							|  |  |  |     public function getMenuList(string $type = 'all', string $show = 'all'): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Menu::field("id,pid,title,type,name,icon,href,target,sort") | 
					
						
							|  |  |  |             ->where('status', Menu::STATUS_NORMAL) | 
					
						
							|  |  |  |             ->when($type != 'all', function ($q) use ($show) { | 
					
						
							|  |  |  |                 $q->where('is_show', $show); | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             ->when($type != 'all', function ($q) use ($type) { | 
					
						
							|  |  |  |                 $q->where('type', $type); | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             ->order('sort', 'desc') | 
					
						
							|  |  |  |             ->order('id', 'asc') | 
					
						
							|  |  |  |             ->select(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //递归获取子菜单
 | 
					
						
							|  |  |  |     public function buildMenuChild(int $pid, array $menus, string $childName = 'children'): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $treeList = []; | 
					
						
							|  |  |  |         foreach ($menus as $v) { | 
					
						
							|  |  |  |             if ($pid == $v['pid']) { | 
					
						
							|  |  |  |                 $node                 = $v; | 
					
						
							|  |  |  |                 $node['has_children'] = false; | 
					
						
							|  |  |  |                 $child                = $this->buildMenuChild($v['id'], $menus, $childName); | 
					
						
							|  |  |  |                 if (!empty($child)) { | 
					
						
							|  |  |  |                     $node[$childName]     = $child; | 
					
						
							|  |  |  |                     $node['has_children'] = true; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-11-30 15:26:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |                 // todo 后续此处加上用户的权限判断
 | 
					
						
							| 
									
										
										
										
											2021-11-30 15:26:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |                 $treeList[] = $node; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $treeList; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 检测批量数据下 是否有子栏目 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param  array  $ids | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function hasChildrenMenuByIds(array $ids): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Menu::whereIn('pid', $ids)->count() > 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 批量删除 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param  array  $ids | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function delMenuByIds(array $ids): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Menu::deleteByIds($ids); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |