| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- <?php
- /**
- * 渠道工具类
- * 提供渠道树形数据获取等公共方法
- */
- namespace app\common\library;
- use think\Db;
- class ChannelUtil
- {
- /**
- * 获取渠道树(一级节点,用于初始化树形选择器)
- *
- * @return array 渠道树形数据
- */
- public static function getChannelTree()
- {
- $channelList = Db::name('nw_channel')
- ->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;
- }
- }
|