industry-select.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { ref } from 'vue';
  2. import { onLoad } from '@dcloudio/uni-app';
  3. import { listIndustry, listIndustrySkill } from '../../api/systemIndustry';
  4. export default {
  5. setup() {
  6. const industryData = ref([]);
  7. const currentIndustry = ref('');
  8. const loadData = async (selectedVal) => {
  9. try {
  10. uni.showLoading({ title: '加载中...' });
  11. const [indRes] = await Promise.all([
  12. listIndustry()
  13. ]);
  14. if (indRes.code === 200) {
  15. const industries = indRes.rows || indRes.data || [];
  16. // 获取第一级行业大类
  17. const level1 = industries.filter(i => i.parentId == 0 || !i.parentId);
  18. // 获取子行业
  19. const result = level1.map(l1 => {
  20. // 将该父级包含的二级行业提取出来(如果后台还有技能并且想挂在行业上,也可以这里兼容,但按需求行业分类为二级)
  21. const level2 = industries
  22. .filter(i => i.parentId == l1.industryId)
  23. .map(i => i.industryName);
  24. // 防止某些分类没有二级节点,这里可以兼容一下容错(如果是三级架构,有些顶级大类直接带技能也是可能的,暂不处理直接输出行业)
  25. return {
  26. name: l1.industryName,
  27. expanded: false,
  28. children: level2
  29. };
  30. });
  31. // 根据已选内容自动展开对应的父类
  32. if (selectedVal) {
  33. const parts = selectedVal.split('-');
  34. if (parts.length > 0) {
  35. result.forEach(item => {
  36. item.expanded = item.name === parts[0];
  37. });
  38. }
  39. } else if (result.length > 0) {
  40. // 如果为空,默认展开第一项
  41. result[0].expanded = true;
  42. }
  43. industryData.value = result;
  44. }
  45. } catch (e) {
  46. console.error('加载行业数据失败', e);
  47. uni.showToast({ title: '加载行业数据失败', icon: 'none' });
  48. } finally {
  49. uni.hideLoading();
  50. }
  51. };
  52. onLoad((options) => {
  53. let selectedParam = '';
  54. if (options && options.selected) {
  55. currentIndustry.value = decodeURIComponent(options.selected);
  56. selectedParam = currentIndustry.value;
  57. }
  58. uni.setNavigationBarTitle({ title: '选择行业' });
  59. // 页面加载时拉取后端数据
  60. loadData(selectedParam);
  61. });
  62. const toggleCategory = (index) => {
  63. industryData.value[index].expanded = !industryData.value[index].expanded;
  64. };
  65. const selectIndustry = (primaryName, secondaryName) => {
  66. const industryVal = `${primaryName}-${secondaryName}`;
  67. currentIndustry.value = industryVal;
  68. // 选完后回传给上一个页面并返回
  69. uni.$emit('select_industry', industryVal);
  70. uni.navigateBack();
  71. };
  72. return {
  73. industryData,
  74. currentIndustry,
  75. toggleCategory,
  76. selectIndustry
  77. };
  78. }
  79. }