import { ref } from 'vue'; import { onLoad } from '@dcloudio/uni-app'; import { listIndustry, listIndustrySkill } from '../../api/systemIndustry'; export default { setup() { const industryData = ref([]); const currentIndustry = ref(''); const loadData = async (selectedVal) => { try { uni.showLoading({ title: '加载中...' }); const [indRes] = await Promise.all([ listIndustry() ]); if (indRes.code === 200) { const industries = indRes.rows || indRes.data || []; // 获取第一级行业大类 const level1 = industries.filter(i => i.parentId == 0 || !i.parentId); // 获取子行业 const result = level1.map(l1 => { // 将该父级包含的二级行业提取出来(如果后台还有技能并且想挂在行业上,也可以这里兼容,但按需求行业分类为二级) const level2 = industries .filter(i => i.parentId == l1.industryId) .map(i => i.industryName); // 防止某些分类没有二级节点,这里可以兼容一下容错(如果是三级架构,有些顶级大类直接带技能也是可能的,暂不处理直接输出行业) return { name: l1.industryName, expanded: false, children: level2 }; }); // 根据已选内容自动展开对应的父类 if (selectedVal) { const parts = selectedVal.split('-'); if (parts.length > 0) { result.forEach(item => { item.expanded = item.name === parts[0]; }); } } else if (result.length > 0) { // 如果为空,默认展开第一项 result[0].expanded = true; } industryData.value = result; } } catch (e) { console.error('加载行业数据失败', e); uni.showToast({ title: '加载行业数据失败', icon: 'none' }); } finally { uni.hideLoading(); } }; onLoad((options) => { let selectedParam = ''; if (options && options.selected) { currentIndustry.value = decodeURIComponent(options.selected); selectedParam = currentIndustry.value; } uni.setNavigationBarTitle({ title: '选择行业' }); // 页面加载时拉取后端数据 loadData(selectedParam); }); const toggleCategory = (index) => { industryData.value[index].expanded = !industryData.value[index].expanded; }; const selectIndustry = (primaryName, secondaryName) => { const industryVal = `${primaryName}-${secondaryName}`; currentIndustry.value = industryVal; // 选完后回传给上一个页面并返回 uni.$emit('select_industry', industryVal); uni.navigateBack(); }; return { industryData, currentIndustry, toggleCategory, selectIndustry }; } }