where(['parent_id' => 0, 'status' => 1]) ->where('id', '>', 0) ->field('id as value, name, level') ->order('id', 'asc') ->select(); $tree = []; foreach ($channelList as $v) { $hasChildren = self::hasChildChannels($v['value']); $tree[] = [ 'name' => $v['name'], 'value' => (int)$v['value'], 'children' => $hasChildren ? [] : null, ]; } return $tree; } /** * 获取完整渠道树(包含所有层级,用于搜索功能) * * @return array 完整渠道树形数据 */ public static function getFullChannelTree() { $channelList = Db::name('nw_channel') ->where('status', 1) ->where('id', '>', 0) ->field('id as value, name, parent_id, level') ->order('id', 'asc') ->select(); // 构建树形结构 $tree = []; $map = []; foreach ($channelList as $v) { $item = [ 'name' => $v['name'], 'value' => (int)$v['value'], 'children' => [], ]; $map[$v['value']] = $item; } foreach ($channelList as $v) { if ($v['parent_id'] == 0) { $tree[] = &$map[$v['value']]; } else if (isset($map[$v['parent_id']])) { $map[$v['parent_id']]['children'][] = &$map[$v['value']]; } } // 清理空的 children 数组(叶子节点设为 null) array_walk_recursive($tree, function(&$item) { // 不处理 }); // 递归清理空 children self::cleanEmptyChildren($tree); return $tree; } /** * 递归清理空的 children 数组 */ private static function cleanEmptyChildren(&$tree) { foreach ($tree as &$item) { if (isset($item['children'])) { if (empty($item['children'])) { $item['children'] = null; } else { self::cleanEmptyChildren($item['children']); } } } } /** * 获取下级渠道(用于懒加载) * * @param int $parentId 父级渠道ID * @return array 下级渠道数据 */ public static function getChildChannels($parentId) { $parentId = intval($parentId); $channelList = Db::name('nw_channel') ->where(['parent_id' => $parentId, 'status' => 1]) ->where('id', '>', 0) ->field('id as value, name, level') ->order('id', 'asc') ->select(); $data = []; foreach ($channelList as $v) { $hasChildren = self::hasChildChannels($v['value']); $data[] = [ 'name' => $v['name'], 'value' => (int)$v['value'], 'children' => $hasChildren ? [] : null, ]; } return $data; } /** * 检查是否有下级渠道 * * @param int $channelId 渠道ID * @return bool */ public static function hasChildChannels($channelId) { return Db::name('nw_channel') ->where(['parent_id' => $channelId, 'status' => 1]) ->count() > 0; } /** * 获取已选中渠道的详细信息(用于前端渲染 initValue) * * @param string $channelIds 渠道ID字符串(逗号分隔或'all') * @return array 已选中渠道信息 */ public static function getSelectedChannels($channelIds) { if (empty($channelIds) || $channelIds === 'NULL') { return []; } // 全选模式 if ($channelIds === 'all') { $level1List = Db::name('nw_channel') ->where(['parent_id' => 0, 'status' => 1]) ->field('id as value, name') ->order('id', 'asc') ->select(); $result = []; foreach ($level1List as $v) { $result[] = [ 'name' => $v['name'], 'value' => (int)$v['value'], 'selected' => true ]; } return $result; } $ids = explode(',', $channelIds); $ids = array_filter(array_map('intval', $ids), function($v) { return $v > 0; }); if (empty($ids)) { return []; } // 获取一级渠道ID列表 $level1Ids = Db::name('nw_channel') ->where(['parent_id' => 0, 'status' => 1]) ->column('id'); // 判断是否是一级全选模式(所有一级渠道ID都在选中列表中) $isLevel1All = empty(array_diff($level1Ids, $ids)); if ($isLevel1All) { // 一级全选模式,只返回一级渠道信息 $level1List = Db::name('nw_channel') ->where(['parent_id' => 0, 'status' => 1]) ->field('id as value, name') ->order('id', 'asc') ->select(); $result = []; foreach ($level1List as $v) { $result[] = [ 'name' => $v['name'], 'value' => (int)$v['value'], 'selected' => true ]; } return $result; } // 部分选中模式,返回具体选中的渠道信息 $selectedChannelList = Db::name('nw_channel') ->where(['id' => ['in', $ids]]) ->field('id as value, name, parent_id') ->select(); $result = []; foreach ($selectedChannelList as $sc) { $item = [ 'name' => $sc['name'], 'value' => (int)$sc['value'], 'selected' => true ]; // 检查是否有子节点,如果有则添加 children 属性(用于树形组件识别父节点) if (self::hasChildChannels($sc['value'])) { $item['children'] = []; } $result[] = $item; } return $result; } }