RuoYi
2026-04-14 7a4f61cf5e8d42d24907ccc3d7345d25012d3a55
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
<template>
  <div class="tree-sidebar" :class="{ collapsed: collapsed, resizing: isResizing, 'no-initial-transition': isLoadingFromStorage}" :style="{ width: sidebarWidth + 'px' }">
    <!-- 右侧拖动条 -->
    <div v-if="!collapsed" class="resize-handle" @mousedown="startResize" @touchstart="startResize" :class="{ active: isResizing }" />
    <div class="tree-header">
      <span class="tree-title" v-show="!collapsed">
        <i :class="titleIconClass"></i> {{ title }}
      </span>
      <div class="tree-actions" v-show="!collapsed">
        <el-tooltip :content="isExpandedAll ? '收起全部' : '展开全部'" placement="right">
          <i class="tree-action-icon" :class="isExpandedAll ? 'el-icon-arrow-down' : 'el-icon-arrow-up'" @click="toggleExpandAll" />
        </el-tooltip>
        <el-tooltip content="刷新" placement="right">
          <i class="tree-action-icon el-icon-refresh" @click="handleRefresh" />
        </el-tooltip>
        <slot name="actions"></slot>
      </div>
    </div>
    
    <!-- 侧边栏展开/收起按钮 -->
    <div class="collapse-button-container">
      <el-tooltip :content="collapsed ? '展开' : '收起'" placement="right">
        <i class="collapse-button" :class="collapsed ? 'el-icon-d-arrow-right' : 'el-icon-d-arrow-left'" @click="toggleCollapsed" />
      </el-tooltip>
    </div>
 
    <div class="tree-search" v-show="!collapsed" v-if="showSearch">
      <el-input v-model="searchKeyword" :placeholder="searchPlaceholder" clearable size="small" prefix-icon="el-icon-search" @input="onSearch" />
    </div>
 
    <div class="tree-wrap" v-show="!collapsed">
      <el-tree 
        ref="treeRef" 
        :data="treeData" 
        :props="treeProps" 
        :expand-on-click-node="expandOnClickNode"
        :filter-node-method="filterNodeMethod"
        :default-expand-all="defaultExpandAll"
        :default-expanded-keys="defaultExpandedKeys"
        :node-key="nodeKey"
        :check-strictly="checkStrictly"
        :show-checkbox="showCheckbox"
        @node-click="onNodeClick"
        @check="onCheck"
        @node-expand="onNodeExpand"
        @node-collapse="onNodeCollapse"
      >
        <span class="tree-node" slot-scope="{ node, data }">
          <slot name="node" :node="node" :data="data">
            <i :class="data.children && data.children.length ? 'el-icon-folder' : 'el-icon-document'" class="node-icon" />
            <span class="node-label" :title="node.label">{{ node.label }}</span>
          </slot>
        </span>
      </el-tree>
    </div>
  </div>
</template>
 
<script>
export default {
  name: "TreeSidebar",
  props: {
    // 树形数据
    treeData: {
      type: Array,
      default: () => []
    },
    // 标题
    title: {
      type: String,
      default: '树形结构'
    },
    // 标题图标类名
    titleIconClass: {
      type: String,
      default: 'el-icon-office-building'
    },
    // 是否显示搜索框
    showSearch: {
      type: Boolean,
      default: true
    },
    // 搜索框占位符
    searchPlaceholder: {
      type: String,
      default: '请输入名称'
    },
    // 是否默认收起侧边栏
    defaultCollapsed: {
      type: Boolean,
      default: false
    },
    // 树配置项
    treeProps: {
      type: Object,
      default: () => ({
        children: "children",
        label: "label"
      })
    },
    // 节点唯一标识字段
    nodeKey: {
      type: String,
      default: 'id'
    },
    // 是否在点击节点时展开或收起
    expandOnClickNode: {
      type: Boolean,
      default: false
    },
    // 是否显示复选框
    showCheckbox: {
      type: Boolean,
      default: false
    },
    // 是否严格的遵循父子不互相关联
    checkStrictly: {
      type: Boolean,
      default: false
    },
    // 是否默认展开所有节点
    defaultExpandAll: {
      type: Boolean,
      default: false
    },
    // 默认展开的节点的key数组
    defaultExpandedKeys: {
      type: Array,
      default: () => []
    },
    // 默认宽度
    defaultWidth: {
      type: Number,
      default: 220
    },
    // 收起时的宽度
    collapsedWidth: {
      type: Number,
      default: 20
    },
    // 最小宽度
    minWidth: {
      type: Number,
      default: 180
    },
    // 最大宽度
    maxWidth: {
      type: Number,
      default: 400
    },
    // 本地存储的宽度key
    storageKey: {
      type: String,
      default: 'tree-sidebar-width'
    },
    // 是否启用本地存储宽度
    enableStorage: {
      type: Boolean,
      default: true
    },
    // 自定义过滤方法
    filterMethod: {
      type: Function,
      default: null
    }
  },
  data() {
    return {
      searchKeyword: "",
      collapsed: this.defaultCollapsed,
      sidebarWidth: this.defaultCollapsed ? this.collapsedWidth : this.defaultWidth,
      isResizing: false,
      startX: 0,
      startWidth: 0,
      saveWidthTimer: null,
      rafId: null,
      isLoadingFromStorage: false,
      expandedAll: this.defaultExpandAll
    };
  },
  computed: {
    // 计算当前是否全部展开
    isExpandedAll: {
      get() {
        return this.expandedAll;
      },
      set(val) {
        this.expandedAll = val;
      }
    }
  },
  watch: {
    collapsed(newVal, oldVal) {
      if (newVal !== oldVal) {
        this.handleCollapseChange(newVal);
        this.$emit("collapsed-change", newVal);
      }
    },
    // 监听内部展开状态变化,触发实际树的展开/收起
    expandedAll(newVal) {
      this.$nextTick(() => {
        if (newVal) {
          this.expandAllNodes();
        } else {
          this.collapseAllNodes();
        }
      });
      this.$emit("expanded-all-change", newVal);
    },
    // 监听搜索关键词
    searchKeyword(val) {
      if (this.$refs.treeRef) {
        this.$refs.treeRef.filter(val);
        this.$emit("search", val);
      }
    }
  },
  mounted() {
    this.isLoadingFromStorage = true
    if (!this.collapsed && this.enableStorage) {
      const savedWidth = this.getSavedWidth();
      if (savedWidth !== null) {
        this.sidebarWidth = savedWidth;
      }
    }
    this.$nextTick(() => {
      this.isLoadingFromStorage = false
    })
    if (this.expandedAll) {
      this.$nextTick(() => {
        this.expandAllNodes();
      });
    }
  },
  beforeDestroy() {
    this.cleanup();
  },
  methods: {
    // 节点过滤方法
    filterNodeMethod(value, data) {
      if (this.filterMethod) {
        return this.filterMethod(value, data);
      }
      if (!value) return true;
      return data.label && data.label.indexOf(value) !== -1;
    },
    // 清理定时器和动画帧
    cleanup() {
      if (this.rafId) {
        cancelAnimationFrame(this.rafId);
        this.rafId = null;
      }
      if (this.saveWidthTimer) {
        clearTimeout(this.saveWidthTimer);
        this.saveWidthTimer = null;
      }
    },
    // 处理收起/展开状态变化
    handleCollapseChange(isCollapsed) {
      if (isCollapsed) {
        this.saveWidthToStorage();
        this.sidebarWidth = this.collapsedWidth;
      } else {
        const savedWidth = this.getSavedWidth();
        this.sidebarWidth = savedWidth !== null ? savedWidth : this.defaultWidth;
      }
    },
    // 获取保存的宽度
    getSavedWidth() {
      if (!this.enableStorage) {
        return null;
      }
      try {
        const savedWidth = localStorage.getItem(this.storageKey);
        if (savedWidth) {
          const width = parseInt(savedWidth, 10);
          if (!isNaN(width) && width >= this.minWidth && width <= this.maxWidth) {
            return width;
          }
        }
      } catch (error) {
        console.warn(`Failed to load sidebar width from storage with key ${this.storageKey}:`, error);
      }
      return null;
    },
    // 保存宽度到本地存储
    saveWidthToStorage() {
      if (this.collapsed || !this.enableStorage) return;
      try {
        localStorage.setItem(this.storageKey, this.sidebarWidth.toString());
      } catch (error) {
        console.warn(`Failed to save sidebar width to storage with key ${this.storageKey}:`, error);
      }
    },
    // 切换侧边栏收起/展开状态
    toggleCollapsed() {
      this.collapsed = !this.collapsed;
    },
    // 切换展开/折叠所有节点
    toggleExpandAll() {
      this.isExpandedAll = !this.isExpandedAll;
    },
    // 展开所有节点
    expandAllNodes() {
      if (!this.$refs.treeRef) return;
      const allNodes = this.getAllNodes(this.$refs.treeRef.root);
      allNodes.forEach(node => {
        if (node.expanded !== undefined && !node.expanded) {
          node.expanded = true;
        }
      });
    },
    // 获取所有节点
    getAllNodes(rootNode) {
      const nodes = [];
      const traverse = (node) => {
        if (!node) return;
        nodes.push(node);
        if (node.childNodes && node.childNodes.length) {
          node.childNodes.forEach(child => traverse(child));
        }
      };
      traverse(rootNode);
      return nodes;
    },
    // 收起所有节点
    collapseAllNodes() {
      if (!this.$refs.treeRef) return;
      const allNodes = this.getAllNodes(this.$refs.treeRef.root);
      allNodes.forEach(node => {
        if (node.expanded !== undefined && node.expanded) {
          node.expanded = false;
        }
      });
    },
    // 处理刷新操作
    handleRefresh() {
      this.$emit("refresh");
    },
    // 节点点击事件
    onNodeClick(data, node, e) {
      this.$emit("node-click", data, node, e);
    },
    // 复选框选中事件
    onCheck(data, checkedInfo) {
      this.$emit("check", data, checkedInfo);
    },
    // 节点展开事件
    onNodeExpand(data, node, e) {
      this.$emit("node-expand", data, node, e);
    },
    // 节点折叠事件
    onNodeCollapse(data, node, e) {
      this.$emit("node-collapse", data, node, e);
    },
    // 搜索处理
    onSearch() {
      // 搜索逻辑已在 watch 中处理
    },
    // 设置当前选中的节点
    setCurrentKey(key) {
      if (this.$refs.treeRef) {
        this.$refs.treeRef.setCurrentKey(key);
      }
    },
    // 获取当前选中的节点
    getCurrentNode() {
      if (this.$refs.treeRef) {
        return this.$refs.treeRef.getCurrentNode();
      }
      return null;
    },
    // 获取当前选中的节点的key
    getCurrentKey() {
      if (this.$refs.treeRef) {
        return this.$refs.treeRef.getCurrentKey();
      }
      return null;
    },
    // 设置选中的节点keys(复选框)
    setCheckedKeys(keys) {
      if (this.$refs.treeRef && this.showCheckbox) {
        this.$refs.treeRef.setCheckedKeys(keys);
      }
    },
    // 获取选中的节点keys(复选框)
    getCheckedKeys() {
      if (this.$refs.treeRef && this.showCheckbox) {
        return this.$refs.treeRef.getCheckedKeys();
      }
      return [];
    },
    // 获取选中的节点(复选框)
    getCheckedNodes() {
      if (this.$refs.treeRef && this.showCheckbox) {
        return this.$refs.treeRef.getCheckedNodes();
      }
      return [];
    },
    // 清空搜索
    clearSearch() {
      this.searchKeyword = "";
      if (this.$refs.treeRef) {
        this.$refs.treeRef.filter("");
      }
    },
    // 过滤树
    filter(value) {
      this.searchKeyword = value;
    },
    // 开始调整大小
    startResize(e) {
      e.preventDefault();
      e.stopPropagation();
      this.isResizing = true;
      this.startX = e.type === 'mousedown' ? e.clientX : e.touches[0].clientX;
      this.startWidth = this.sidebarWidth;
      
      if (e.type === 'mousedown') {
        document.addEventListener('mousemove', this.handleResizeMove);
        document.addEventListener('mouseup', this.stopResize);
      } else {
        document.addEventListener('touchmove', this.handleResizeMove, { passive: false });
        document.addEventListener('touchend', this.stopResize);
      }
      this.disableUserSelect();
    },
    // 处理调整大小移动
    handleResizeMove(e) {
      if (!this.isResizing) return;
      if (this.rafId) {
        cancelAnimationFrame(this.rafId);
      }
      this.rafId = requestAnimationFrame(() => {
        e.preventDefault();
        e.stopPropagation();
        const clientX = e.type === 'mousemove' ? e.clientX : e.touches[0].clientX;
        const deltaX = clientX - this.startX;
        const newWidth = this.startWidth + deltaX;
        const clampedWidth = Math.max(this.minWidth, Math.min(this.maxWidth, newWidth));
        if (Math.abs(clampedWidth - this.sidebarWidth) >= 1) {
          this.sidebarWidth = clampedWidth;
        }
      });
    },
    // 停止调整大小
    stopResize() {
      if (!this.isResizing) return;
      this.isResizing = false;
      if (this.rafId) {
        cancelAnimationFrame(this.rafId);
        this.rafId = null;
      }
      this.startX = 0;
      this.startWidth = 0;
      document.removeEventListener('mousemove', this.handleResizeMove);
      document.removeEventListener('mouseup', this.stopResize);
      document.removeEventListener('touchmove', this.handleResizeMove);
      document.removeEventListener('touchend', this.stopResize);
      this.enableUserSelect();
      this.saveWidthToStorage();
    },
    // 禁用用户选择
    disableUserSelect() {
      document.body.style.userSelect = 'none';
      document.body.style.webkitUserSelect = 'none';
      document.body.style.mozUserSelect = 'none';
      document.body.style.msUserSelect = 'none';
    },
    // 启用用户选择
    enableUserSelect() {
      document.body.style.userSelect = '';
      document.body.style.webkitUserSelect = '';
      document.body.style.mozUserSelect = '';
      document.body.style.msUserSelect = '';
    },
    // 重置宽度到默认值
    resetWidth() {
      this.sidebarWidth = this.defaultWidth;
      this.saveWidthToStorage();
    },
    // 获取当前宽度
    getCurrentWidth() {
      return this.sidebarWidth;
    },
    // 设置宽度
    setWidth(width) {
      if (typeof width === 'number' && width >= this.minWidth && width <= this.maxWidth) {
        this.sidebarWidth = width;
        if (!this.collapsed) {
          this.saveWidthToStorage();
        }
      }
    }
  }
};
</script>
 
<style lang="scss" scoped>
.tree-sidebar {
  flex-shrink: 0;
  width: 220px;
  background: #fff;
  border-right: 1px solid #e8eaed;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
  transition: width 0.25s ease;
  
  &.collapsed {
    width: 42px;
  }
  
  &.resizing {
    transition: none;
    will-change: width;
    
    * {
      pointer-events: none !important;
    }
  }
  
  &.no-initial-transition {
    transition: none;
  }
}
 
.resize-handle {
  position: absolute;
  top: 0;
  right: 0;
  width: 6px;
  height: 100%;
  cursor: col-resize;
  z-index: 20;
  background: transparent;
  transition: background 0.2s;
  
  &:hover {
    background: rgba(64, 158, 255, 0.3);
  }
  
  &.active {
    background: rgba(64, 158, 255, 0.5);
  }
}
 
.collapse-button-container {
  position: absolute;
  top: 50%;
  right: 0;
  transform: translateY(-50%);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 15px;
  height: 20px;
  background: #fff;
  border-radius: 0 4px 4px 0;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  transition: all 0.2s ease;
  
  .tree-sidebar.collapsed & {
    right: 0;
    background: #f7f8fa;
    border-radius: 0 4px 4px 0;
  }
  
  .tree-sidebar.resizing & {
    pointer-events: none;
  }
}
 
.collapse-button {
  font-size: 14px;
  color: #909399;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: all 0.2s;
  
  &:hover {
    color: #409eff;
    background: #ecf5ff;
  }
}
 
.tree-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 10px;
  height: 40px;
  border-bottom: 1px solid #e8eaed;
  background: #f7f8fa;
  flex-shrink: 0;
 
  .tree-title {
    font-size: 13px;
    font-weight: 600;
    color: #303133;
    white-space: nowrap;
    overflow: hidden;
    display: flex;
    align-items: center;
    gap: 5px;
 
    i {
      color: #409eff;
      font-size: 14px;
    }
  }
 
  .tree-actions {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
  }
}
 
.tree-action-icon {
  font-size: 14px;
  color: #909399;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: all 0.2s;
 
  &:hover {
    color: #409eff;
    background: #ecf5ff;
  }
}
 
.tree-search {
  padding: 10px 10px 4px;
  flex-shrink: 0;
}
 
.tree-wrap {
  flex: 1;
  overflow-y: auto;
  padding: 6px 6px 12px;
  
  .tree-sidebar.resizing & {
    overflow: hidden;
  }
 
  &::-webkit-scrollbar {
    width: 4px;
  }
 
  &::-webkit-scrollbar-thumb {
    background: #dcdfe6;
    border-radius: 4px;
    
    &:hover {
      background: #c0c4cc;
    }
  }
 
  ::v-deep .el-tree-node__content {
    height: 32px;
    border-radius: 4px;
    margin-bottom: 1px;
 
    &:hover {
      background: #f0f7ff;
    }
  }
 
  ::v-deep .el-tree-node.is-current > .el-tree-node__content {
    background: #e6f0fd;
    color: #409eff;
    font-weight: 600;
 
    .node-icon {
      color: #409eff !important;
    }
  }
}
 
.tree-node {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 13px;
  overflow: hidden;
 
  .node-icon {
    font-size: 14px;
    color: #f5a623;
    flex-shrink: 0;
  }
 
  .node-label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
 
::v-deep .el-icon-document.node-icon {
  color: #909399 !important;
}
</style>