column('menu_id'); if (QywxPromotionOperatorAccess::hasSharedPagePermission((int) $adminId)) { $roleMenu = array_values(array_unique(array_merge( array_map('intval', $roleMenu), self::sharedPromotionMenuIds() ))); } $where[] = ['id', 'in', $roleMenu]; } $menu = SystemMenu::where($where) ->order(['sort' => 'desc', 'id' => 'asc']) ->select(); return linear_to_tree($menu, 'children'); } /** @return list */ private static function sharedPromotionMenuIds(): array { $menus = SystemMenu::where('is_disable', 0) ->field('id,pid,perms') ->select() ->toArray(); $byId = []; $pageIds = []; foreach ($menus as $menu) { $id = (int) ($menu['id'] ?? 0); if ($id <= 0) { continue; } $byId[$id] = $menu; if ((string) ($menu['perms'] ?? '') === QywxPromotionOperatorAccess::PAGE_PERMISSION) { $pageIds[] = $id; } } $result = []; foreach ($pageIds as $pageId) { $chain = []; $currentId = $pageId; while ($currentId > 0) { if (!isset($byId[$currentId])) { $chain = []; break; } $chain[] = $currentId; $currentId = (int) ($byId[$currentId]['pid'] ?? 0); } $result = array_merge($result, $chain); } return array_values(array_unique($result)); } /** * @notes 添加菜单 * @param array $params * @return SystemMenu|\think\Model * @author 段誉 * @date 2022/6/30 10:06 */ public static function add(array $params) { return SystemMenu::create([ 'pid' => $params['pid'], 'type' => $params['type'], 'name' => $params['name'], 'icon' => $params['icon'] ?? '', 'sort' => $params['sort'], 'perms' => $params['perms'] ?? '', 'paths' => $params['paths'] ?? '', 'component' => $params['component'] ?? '', 'selected' => $params['selected'] ?? '', 'params' => $params['params'] ?? '', 'is_cache' => $params['is_cache'], 'is_show' => $params['is_show'], 'is_disable' => $params['is_disable'], ]); } /** * @notes 编辑菜单 * @param array $params * @return SystemMenu * @author 段誉 * @date 2022/6/30 10:07 */ public static function edit(array $params) { return SystemMenu::update([ 'id' => $params['id'], 'pid' => $params['pid'], 'type' => $params['type'], 'name' => $params['name'], 'icon' => $params['icon'] ?? '', 'sort' => $params['sort'], 'perms' => $params['perms'] ?? '', 'paths' => $params['paths'] ?? '', 'component' => $params['component'] ?? '', 'selected' => $params['selected'] ?? '', 'params' => $params['params'] ?? '', 'is_cache' => $params['is_cache'], 'is_show' => $params['is_show'], 'is_disable' => $params['is_disable'], ]); } /** * @notes 详情 * @param $params * @return array * @author 段誉 * @date 2022/6/30 9:54 */ public static function detail($params) { return SystemMenu::findOrEmpty($params['id'])->toArray(); } /** * @notes 删除菜单 * @param $params * @author 段誉 * @date 2022/6/30 9:47 */ public static function delete($params) { // 删除菜单 SystemMenu::destroy($params['id']); // 删除角色-菜单表中 与该菜单关联的记录 SystemRoleMenu::where(['menu_id' => $params['id']])->delete(); } /** * @notes 更新状态 * @param array $params * @return SystemMenu * @author 段誉 * @date 2022/7/6 17:02 */ public static function updateStatus(array $params) { return SystemMenu::update([ 'id' => $params['id'], 'is_disable' => $params['is_disable'] ]); } /** * @notes 全部数据 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @author 段誉 * @date 2022/10/13 11:03 */ public static function getAllData() { $data = SystemMenu::where(['is_disable' => YesNoEnum::NO]) ->field('id,pid,name') ->order(['sort' => 'desc', 'id' => 'desc']) ->select() ->toArray(); return linear_to_tree($data, 'children'); } }