FolderTreeItem.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <template>
  2. <view class="folder-tree-item">
  3. <!-- 文件夹项 -->
  4. <view
  5. class="folder-item"
  6. :style="{ paddingLeft: (level * 40 + 24) + 'rpx' }"
  7. @click="handleToggle"
  8. >
  9. <!-- 展开/收起图标 -->
  10. <view class="expand-icon" v-if="hasChildren">
  11. <text class="icon-text">{{ isExpanded ? '▼' : '▶' }}</text>
  12. </view>
  13. <view class="expand-icon placeholder" v-else></view>
  14. <!-- 文件夹图标 -->
  15. <view class="folder-icon-wrapper">
  16. <image
  17. :src="getFolderIcon(folder.type)"
  18. mode="aspectFit"
  19. class="folder-icon"
  20. />
  21. </view>
  22. <!-- 文件夹名称 -->
  23. <text class="folder-name">{{ folder.name }}</text>
  24. <!-- 选择按钮 -->
  25. <view class="select-btn" @click.stop="handleSelect">
  26. <text class="select-text">选择</text>
  27. </view>
  28. </view>
  29. <!-- 子文件夹 -->
  30. <view v-if="isExpanded && hasChildren" class="children">
  31. <view
  32. v-for="child in folder.children"
  33. :key="child.id"
  34. >
  35. <folder-tree-item
  36. :folder="child"
  37. :level="level + 1"
  38. @select="handleChildSelect"
  39. />
  40. </view>
  41. </view>
  42. <!-- 调试:显示展开状态 -->
  43. <view v-if="hasChildren" style="padding: 10rpx 24rpx; background: #e3f2fd; font-size: 20rpx; color: #1976d2;">
  44. 展开状态: {{ isExpanded ? '已展开' : '已收起' }} | 子节点数: {{ folder.children.length }}
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. export default {
  50. name: 'FolderTreeItem',
  51. // 递归组件需要在这里注册自己
  52. components: {
  53. FolderTreeItem: () => import('./FolderTreeItem.vue')
  54. },
  55. props: {
  56. folder: {
  57. type: Object,
  58. required: true
  59. },
  60. level: {
  61. type: Number,
  62. default: 0
  63. }
  64. },
  65. data() {
  66. return {
  67. isExpanded: false
  68. }
  69. },
  70. computed: {
  71. hasChildren() {
  72. const result = this.folder.children && this.folder.children.length > 0
  73. console.log(`文件夹 ${this.folder.name} hasChildren:`, result, '子节点数:', this.folder.children?.length || 0)
  74. return result
  75. }
  76. },
  77. mounted() {
  78. console.log('========== FolderTreeItem mounted ==========')
  79. console.log('文件夹名称:', this.folder.name)
  80. console.log('层级:', this.level)
  81. console.log('类型:', this.folder.type)
  82. console.log('完整 folder 对象:', this.folder)
  83. console.log('children:', this.folder.children)
  84. console.log('children 是否为数组:', Array.isArray(this.folder.children))
  85. console.log('children 长度:', this.folder.children?.length || 0)
  86. console.log('hasChildren 计算结果:', this.hasChildren)
  87. },
  88. methods: {
  89. // 获取文件夹图标
  90. getFolderIcon(type) {
  91. switch (type) {
  92. case 0:
  93. // 普通文件夹
  94. return '/static/pages/scan/folderSelect/folder.svg'
  95. case 1:
  96. // 国家
  97. return '/static/pages/scan/folderSelect/country.svg'
  98. case 2:
  99. // 中心
  100. return '/static/pages/scan/folderSelect/center.svg'
  101. default:
  102. return '/static/pages/scan/folderSelect/folder.svg'
  103. }
  104. },
  105. // 切换展开/收起
  106. handleToggle() {
  107. console.log('========== 点击切换 ==========')
  108. console.log('文件夹:', this.folder.name)
  109. console.log('当前展开状态:', this.isExpanded)
  110. console.log('有子节点:', this.hasChildren)
  111. console.log('子节点数量:', this.folder.children?.length || 0)
  112. if (this.hasChildren) {
  113. this.isExpanded = !this.isExpanded
  114. console.log('切换后展开状态:', this.isExpanded)
  115. // 强制更新
  116. this.$forceUpdate()
  117. // 延迟检查子节点是否渲染
  118. setTimeout(() => {
  119. console.log('延迟检查 - 当前展开状态:', this.isExpanded)
  120. console.log('延迟检查 - 子节点:', this.folder.children)
  121. }, 100)
  122. }
  123. },
  124. // 选择当前文件夹
  125. handleSelect() {
  126. this.$emit('select', this.folder)
  127. },
  128. // 子节点选择事件
  129. handleChildSelect(folder) {
  130. this.$emit('select', folder)
  131. }
  132. }
  133. }
  134. </script>
  135. <style lang="scss" scoped>
  136. .folder-tree-item {
  137. .folder-item {
  138. display: flex;
  139. align-items: center;
  140. padding: 20rpx 24rpx;
  141. border-bottom: 1rpx solid #f5f5f5;
  142. &:active {
  143. background: #f8f8f8;
  144. }
  145. .expand-icon {
  146. width: 40rpx;
  147. height: 40rpx;
  148. display: flex;
  149. align-items: center;
  150. justify-content: center;
  151. margin-right: 8rpx;
  152. &.placeholder {
  153. visibility: hidden;
  154. }
  155. .icon-text {
  156. font-size: 20rpx;
  157. color: #999999;
  158. }
  159. }
  160. .folder-icon-wrapper {
  161. width: 48rpx;
  162. height: 48rpx;
  163. display: flex;
  164. align-items: center;
  165. justify-content: center;
  166. margin-right: 16rpx;
  167. .folder-icon {
  168. width: 48rpx;
  169. height: 48rpx;
  170. }
  171. }
  172. .folder-name {
  173. flex: 1;
  174. font-size: 28rpx;
  175. color: #333333;
  176. }
  177. .select-btn {
  178. padding: 8rpx 24rpx;
  179. background: #1ec9c9;
  180. border-radius: 24rpx;
  181. &:active {
  182. background: #1ab8b8;
  183. }
  184. .select-text {
  185. font-size: 24rpx;
  186. color: #ffffff;
  187. font-weight: 500;
  188. }
  189. }
  190. }
  191. .children {
  192. background: #fafafa;
  193. }
  194. }
  195. </style>