ChooseOrgTree.vue
3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<template>
<el-dialog
:title="$t('scheduleClassesPage.selectOrg')"
:visible.sync="dialogVisible"
width="40%"
>
<el-tree
ref="orgTree"
:data="chooseOrgInfo.orgs"
:props="defaultProps"
node-key="id"
highlight-current
@node-click="handleNodeClick"
/>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">{{ $t('common.cancel') }}</el-button>
<el-button type="primary" @click="_doChooseOrg">{{ $t('common.confirm') }}</el-button>
</span>
</el-dialog>
</template>
<script>
import { getCommunityId } from '@/api/community/communityApi'
import { listOrgTree } from '@/api/org/orgApi'
export default {
name: 'ChooseOrgTree',
data() {
return {
dialogVisible: false,
chooseOrgInfo: {
orgs: [],
orgId: '',
curOrg: {}
},
defaultProps: {
children: 'children',
label: 'text'
}
}
},
methods: {
open() {
this.dialogVisible = true
this._loadChooseOrgs()
},
async _loadChooseOrgs() {
try {
const params = {
communityId: getCommunityId()
}
const { data } = await listOrgTree(params)
// 确保数据是数组格式
let treeData = data
// 如果返回的是对象而不是数组,尝试提取数组
if (Array.isArray(treeData)) {
this.chooseOrgInfo.orgs = treeData
} else if (treeData && Array.isArray(treeData.children)) {
this.chooseOrgInfo.orgs = treeData.children
} else if (treeData && Array.isArray(treeData.data)) {
this.chooseOrgInfo.orgs = treeData.data
} else if (treeData && Array.isArray(treeData.list)) {
this.chooseOrgInfo.orgs = treeData.list
} else {
// 如果都不是数组,转换为数组格式
this.chooseOrgInfo.orgs = Array.isArray(treeData) ? treeData : [treeData]
}
// 确保每个节点都有 children 属性且为数组
this.processTreeData(this.chooseOrgInfo.orgs)
} catch (error) {
console.error('获取组织树失败:', error)
this.chooseOrgInfo.orgs = []
}
},
processTreeData(nodes) {
if (!Array.isArray(nodes)) return
nodes.forEach(node => {
// 确保每个节点都有 children 属性且为数组
if (!node.children) {
node.children = []
} else if (!Array.isArray(node.children)) {
node.children = []
}
// 递归处理子节点
if (node.children && node.children.length > 0) {
this.processTreeData(node.children)
}
})
},
handleNodeClick(data) {
this.chooseOrgInfo.curOrg = data
this.chooseOrgInfo.curOrg.orgId = data.id
},
_doChooseOrg() {
this.$emit('switchOrg', this.chooseOrgInfo.curOrg)
this.dialogVisible = false
}
}
}
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body {
padding: 20px;
max-height: 60vh;
overflow-y: auto;
}
</style>