index.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <template>
  2. <view class="tree-view">
  3. <view
  4. v-for="(item, index) in flattenedTree"
  5. :key="item.id"
  6. class="tree-item"
  7. :style="{ paddingLeft: (item.level * 40 + 24) + 'rpx' }"
  8. >
  9. <!-- 展开/收起图标 -->
  10. <view class="expand-icon" @click="toggleExpand(item)" v-if="item.hasChildren">
  11. <text class="icon-text">{{ item.expanded ? '▼' : '▶' }}</text>
  12. </view>
  13. <view class="expand-icon placeholder" v-else></view>
  14. <!-- 文件夹图标 -->
  15. <view class="folder-icon-wrapper">
  16. <image
  17. :src="getIcon(item.type)"
  18. mode="aspectFit"
  19. class="folder-icon"
  20. />
  21. </view>
  22. <!-- 文件夹名称 -->
  23. <text class="folder-name">{{ item.name }}</text>
  24. <!-- 选择按钮 -->
  25. <view
  26. class="select-btn"
  27. :class="{ disabled: !item.selectable }"
  28. @click="handleSelect(item)"
  29. >
  30. <text class="select-text">{{ item.selectable ? '选择' : '无权限' }}</text>
  31. </view>
  32. </view>
  33. </view>
  34. </template>
  35. <script>
  36. export default {
  37. name: 'TreeView',
  38. props: {
  39. treeData: {
  40. type: Array,
  41. default: () => []
  42. },
  43. iconMap: {
  44. type: Object,
  45. default: () => ({
  46. 0: '/static/pages/scan/folderSelect/folder.svg',
  47. 1: '/static/pages/scan/folderSelect/country.svg',
  48. 2: '/static/pages/scan/folderSelect/center.svg'
  49. })
  50. },
  51. // 权限配置:'*' 表示全部可选,'1,2,3' 表示只有这些ID及其子节点可选
  52. permission: {
  53. type: String,
  54. default: '*'
  55. }
  56. },
  57. data() {
  58. return {
  59. expandedIds: new Set(),
  60. flattenedTree: [],
  61. permissionIds: new Set(), // 有权限的节点ID集合
  62. allowedNodeIds: new Set() // 允许选择的节点ID集合(包括子节点)
  63. }
  64. },
  65. watch: {
  66. treeData: {
  67. handler() {
  68. console.log('TreeView - treeData 变化:', this.treeData)
  69. this.parsePermission()
  70. this.buildFlattenedTree()
  71. },
  72. immediate: true,
  73. deep: true
  74. },
  75. permission: {
  76. handler() {
  77. console.log('TreeView - permission 变化:', this.permission)
  78. this.parsePermission()
  79. this.buildFlattenedTree()
  80. },
  81. immediate: true
  82. }
  83. },
  84. methods: {
  85. // 解析权限配置
  86. parsePermission() {
  87. console.log('========== 解析权限 ==========')
  88. console.log('权限配置:', this.permission)
  89. this.permissionIds.clear()
  90. this.allowedNodeIds.clear()
  91. // 如果是 '*',表示全部可选
  92. if (this.permission === '*') {
  93. console.log('权限为 *,全部节点可选')
  94. return
  95. }
  96. // 解析逗号分隔的ID列表
  97. if (this.permission && this.permission.trim()) {
  98. const ids = this.permission.split(',').map(id => parseInt(id.trim())).filter(id => !isNaN(id))
  99. ids.forEach(id => this.permissionIds.add(id))
  100. console.log('有权限的节点ID:', Array.from(this.permissionIds))
  101. // 收集所有允许选择的节点(包括子节点)
  102. this.collectAllowedNodes(this.treeData)
  103. console.log('允许选择的节点ID:', Array.from(this.allowedNodeIds))
  104. }
  105. },
  106. // 递归收集允许选择的节点
  107. collectAllowedNodes(nodes) {
  108. if (!nodes || !Array.isArray(nodes)) return
  109. nodes.forEach(node => {
  110. // 如果当前节点有权限,则它及其所有子节点都可选
  111. if (this.permissionIds.has(node.id)) {
  112. this.addNodeAndChildren(node)
  113. } else if (node.children && node.children.length > 0) {
  114. // 继续检查子节点
  115. this.collectAllowedNodes(node.children)
  116. }
  117. })
  118. },
  119. // 添加节点及其所有子节点到允许列表
  120. addNodeAndChildren(node) {
  121. this.allowedNodeIds.add(node.id)
  122. if (node.children && node.children.length > 0) {
  123. node.children.forEach(child => {
  124. this.addNodeAndChildren(child)
  125. })
  126. }
  127. },
  128. // 检查节点是否可选
  129. isNodeSelectable(nodeId) {
  130. // 如果权限为 '*',全部可选
  131. if (this.permission === '*') {
  132. return true
  133. }
  134. // 否则检查是否在允许列表中
  135. return this.allowedNodeIds.has(nodeId)
  136. },
  137. // 将树形数据扁平化
  138. buildFlattenedTree() {
  139. console.log('========== 开始构建扁平化树 ==========')
  140. this.flattenedTree = []
  141. this.flattenNode(this.treeData, 0)
  142. console.log('扁平化树构建完成,节点数:', this.flattenedTree.length)
  143. console.log('扁平化树:', this.flattenedTree)
  144. },
  145. // 递归扁平化节点
  146. flattenNode(nodes, level, parentPath = []) {
  147. if (!nodes || !Array.isArray(nodes)) {
  148. console.log('节点不是数组:', nodes)
  149. return
  150. }
  151. nodes.forEach(node => {
  152. const hasChildren = node.children && node.children.length > 0
  153. const isExpanded = this.expandedIds.has(node.id)
  154. const isSelectable = this.isNodeSelectable(node.id)
  155. // 构建当前节点的完整路径
  156. const currentPath = [...parentPath, node.name]
  157. const fullPath = currentPath.join('/')
  158. // 添加当前节点
  159. this.flattenedTree.push({
  160. id: node.id,
  161. name: node.name,
  162. type: node.type,
  163. level: level,
  164. hasChildren: hasChildren,
  165. expanded: isExpanded,
  166. selectable: isSelectable,
  167. fullPath: fullPath, // 完整路径
  168. originalNode: node
  169. })
  170. console.log(`添加节点: ${node.name}, level: ${level}, hasChildren: ${hasChildren}, expanded: ${isExpanded}, selectable: ${isSelectable}, path: ${fullPath}`)
  171. // 如果展开且有子节点,递归处理子节点
  172. if (isExpanded && hasChildren) {
  173. console.log(`展开子节点: ${node.name}, 子节点数: ${node.children.length}`)
  174. this.flattenNode(node.children, level + 1, currentPath)
  175. }
  176. })
  177. },
  178. // 切换展开/收起
  179. toggleExpand(item) {
  180. console.log('========== 切换展开状态 ==========')
  181. console.log('节点:', item.name)
  182. console.log('当前展开状态:', item.expanded)
  183. if (item.expanded) {
  184. this.expandedIds.delete(item.id)
  185. console.log('收起节点')
  186. } else {
  187. this.expandedIds.add(item.id)
  188. console.log('展开节点')
  189. }
  190. // 重新构建扁平化树
  191. this.buildFlattenedTree()
  192. },
  193. // 获取图标
  194. getIcon(type) {
  195. return this.iconMap[type] || this.iconMap[0]
  196. },
  197. // 选择节点
  198. handleSelect(item) {
  199. console.log('选择节点:', item.name, '是否可选:', item.selectable)
  200. console.log('完整路径:', item.fullPath)
  201. if (!item.selectable) {
  202. uni.showToast({
  203. title: '无权限选择此文件夹',
  204. icon: 'none',
  205. duration: 2000
  206. })
  207. return
  208. }
  209. // 返回节点信息,包含完整路径
  210. this.$emit('select', {
  211. ...item.originalNode,
  212. fullPath: item.fullPath
  213. })
  214. }
  215. }
  216. }
  217. </script>
  218. <style lang="scss" scoped>
  219. .tree-view {
  220. .tree-item {
  221. display: flex;
  222. align-items: center;
  223. padding: 20rpx 24rpx;
  224. border-bottom: 1rpx solid #f5f5f5;
  225. background: #ffffff;
  226. &:active {
  227. background: #f8f8f8;
  228. }
  229. .expand-icon {
  230. width: 40rpx;
  231. height: 40rpx;
  232. display: flex;
  233. align-items: center;
  234. justify-content: center;
  235. margin-right: 8rpx;
  236. &.placeholder {
  237. visibility: hidden;
  238. }
  239. .icon-text {
  240. font-size: 20rpx;
  241. color: #999999;
  242. }
  243. }
  244. .folder-icon-wrapper {
  245. width: 48rpx;
  246. height: 48rpx;
  247. display: flex;
  248. align-items: center;
  249. justify-content: center;
  250. margin-right: 16rpx;
  251. .folder-icon {
  252. width: 48rpx;
  253. height: 48rpx;
  254. }
  255. }
  256. .folder-name {
  257. flex: 1;
  258. font-size: 28rpx;
  259. color: #333333;
  260. }
  261. .select-btn {
  262. padding: 8rpx 24rpx;
  263. background: #1ec9c9;
  264. border-radius: 24rpx;
  265. &:active {
  266. background: #1ab8b8;
  267. }
  268. &.disabled {
  269. background: #cccccc;
  270. &:active {
  271. background: #cccccc;
  272. }
  273. }
  274. .select-text {
  275. font-size: 24rpx;
  276. color: #ffffff;
  277. font-weight: 500;
  278. }
  279. }
  280. }
  281. }
  282. </style>