index.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. this.parsePermission()
  69. this.buildFlattenedTree()
  70. },
  71. immediate: true,
  72. deep: true
  73. },
  74. permission: {
  75. handler() {
  76. this.parsePermission()
  77. this.buildFlattenedTree()
  78. },
  79. immediate: true
  80. }
  81. },
  82. methods: {
  83. // 解析权限配置
  84. parsePermission() {
  85. this.permissionIds.clear()
  86. this.allowedNodeIds.clear()
  87. // 如果是 '*',表示全部可选
  88. if (this.permission === '*') {
  89. return
  90. }
  91. // 解析逗号分隔的ID列表
  92. if (this.permission && this.permission.trim()) {
  93. const ids = this.permission.split(',').map(id => parseInt(id.trim())).filter(id => !isNaN(id))
  94. ids.forEach(id => this.permissionIds.add(id))
  95. // 收集所有允许选择的节点(包括子节点)
  96. this.collectAllowedNodes(this.treeData)
  97. }
  98. },
  99. // 递归收集允许选择的节点
  100. collectAllowedNodes(nodes) {
  101. if (!nodes || !Array.isArray(nodes)) return
  102. nodes.forEach(node => {
  103. // 如果当前节点有权限,则它及其所有子节点都可选
  104. if (this.permissionIds.has(node.id)) {
  105. this.addNodeAndChildren(node)
  106. } else if (node.children && node.children.length > 0) {
  107. // 继续检查子节点
  108. this.collectAllowedNodes(node.children)
  109. }
  110. })
  111. },
  112. // 添加节点及其所有子节点到允许列表
  113. addNodeAndChildren(node) {
  114. this.allowedNodeIds.add(node.id)
  115. if (node.children && node.children.length > 0) {
  116. node.children.forEach(child => {
  117. this.addNodeAndChildren(child)
  118. })
  119. }
  120. },
  121. // 检查节点是否可选
  122. isNodeSelectable(nodeId) {
  123. // 如果权限为 '*',全部可选
  124. if (this.permission === '*') {
  125. return true
  126. }
  127. // 否则检查是否在允许列表中
  128. return this.allowedNodeIds.has(nodeId)
  129. },
  130. // 将树形数据扁平化
  131. buildFlattenedTree() {
  132. this.flattenedTree = []
  133. this.flattenNode(this.treeData, 0)
  134. },
  135. // 递归扁平化节点
  136. flattenNode(nodes, level, parentPath = []) {
  137. if (!nodes || !Array.isArray(nodes)) {
  138. return
  139. }
  140. nodes.forEach(node => {
  141. const hasChildren = node.children && node.children.length > 0
  142. const isExpanded = this.expandedIds.has(node.id)
  143. const isSelectable = this.isNodeSelectable(node.id)
  144. // 构建当前节点的完整路径
  145. const currentPath = [...parentPath, node.name]
  146. const fullPath = currentPath.join('/')
  147. // 添加当前节点
  148. this.flattenedTree.push({
  149. id: node.id,
  150. name: node.name,
  151. type: node.type,
  152. level: level,
  153. hasChildren: hasChildren,
  154. expanded: isExpanded,
  155. selectable: isSelectable,
  156. fullPath: fullPath, // 完整路径
  157. originalNode: node
  158. })
  159. // 如果展开且有子节点,递归处理子节点
  160. if (isExpanded && hasChildren) {
  161. this.flattenNode(node.children, level + 1, currentPath)
  162. }
  163. })
  164. },
  165. // 切换展开/收起
  166. toggleExpand(item) {
  167. if (item.expanded) {
  168. this.expandedIds.delete(item.id)
  169. } else {
  170. this.expandedIds.add(item.id)
  171. }
  172. // 重新构建扁平化树
  173. this.buildFlattenedTree()
  174. },
  175. // 获取图标
  176. getIcon(type) {
  177. return this.iconMap[type] || this.iconMap[0]
  178. },
  179. // 选择节点
  180. handleSelect(item) {
  181. if (!item.selectable) {
  182. uni.showToast({
  183. title: '无权限选择此文件夹',
  184. icon: 'none',
  185. duration: 2000
  186. })
  187. return
  188. }
  189. // 返回节点信息,包含完整路径
  190. this.$emit('select', {
  191. ...item.originalNode,
  192. fullPath: item.fullPath
  193. })
  194. }
  195. }
  196. }
  197. </script>
  198. <style lang="scss" scoped>
  199. .tree-view {
  200. .tree-item {
  201. display: flex;
  202. align-items: center;
  203. padding: 20rpx 24rpx;
  204. border-bottom: 1rpx solid #f5f5f5;
  205. background: #ffffff;
  206. &:active {
  207. background: #f8f8f8;
  208. }
  209. .expand-icon {
  210. width: 40rpx;
  211. height: 40rpx;
  212. display: flex;
  213. align-items: center;
  214. justify-content: center;
  215. margin-right: 8rpx;
  216. &.placeholder {
  217. visibility: hidden;
  218. }
  219. .icon-text {
  220. font-size: 20rpx;
  221. color: #999999;
  222. }
  223. }
  224. .folder-icon-wrapper {
  225. width: 48rpx;
  226. height: 48rpx;
  227. display: flex;
  228. align-items: center;
  229. justify-content: center;
  230. margin-right: 16rpx;
  231. .folder-icon {
  232. width: 48rpx;
  233. height: 48rpx;
  234. }
  235. }
  236. .folder-name {
  237. flex: 1;
  238. font-size: 28rpx;
  239. color: #333333;
  240. }
  241. .select-btn {
  242. padding: 8rpx 24rpx;
  243. background: #1ec9c9;
  244. border-radius: 24rpx;
  245. &:active {
  246. background: #1ab8b8;
  247. }
  248. &.disabled {
  249. background: #cccccc;
  250. &:active {
  251. background: #cccccc;
  252. }
  253. }
  254. .select-text {
  255. font-size: 24rpx;
  256. color: #ffffff;
  257. font-weight: 500;
  258. }
  259. }
  260. }
  261. }
  262. </style>