zhuguifei
2025-04-28 442928123f63ee497d766f9a7a14f0a6ee067e25
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
710
711
712
713
<template>
  <div >
    <div class='app' ref='departOrg' v-if='data.id'>
      <!--    <div style="display: flex; padding: 10px 0;">
            <div style="margin-right: 10px"><i-switch v-model="horizontal"></i-switch> 横向</div>
            <div style="margin-right: 10px"><i-switch v-model="collapsable"></i-switch> 可收起</div>
            <div style="margin-right: 10px"><i-switch v-model="disaled"></i-switch> 禁止编辑</div>
            <div style="margin-right: 10px"><i-switch v-model="onlyOneNode"></i-switch> 仅拖动当前节点</div>
            <div style="margin-right: 10px"><i-switch v-model="cloneNodeDrag"></i-switch> 拖动节点副本</div>
          </div>
          <div style="padding-bottom:10px">
            背景色:
            <color-picker v-model="style.background" size="small"></color-picker>&nbsp;
            文字颜色:
            <color-picker v-model="style.color" size="small"></color-picker>&nbsp;
            搜索:
            <input type="text" v-model="keyword" placeholder="请输入搜索内容" @keydown.enter="filter" />
          </div>-->
      <div style='height: 100%; border:1px solid #eee'>
        <zm-tree-org
          ref='tree'
          :data='data'
          center
          :scalable='scalable'
          :wheel='zoomWheel'
          :disabled='disabled'
          :horizontal='horizontal'
          :collapsable='collapsable'
          :label-style='style'
          :node-draggable='draggable'
          :default-expand-level='10'
          :only-one-node='onlyOneNode'
          :clone-node-drag='cloneNodeDrag'
          :before-drag-end='beforeDragEnd'
          :toolBar='toolBar'
          :filterNodeMethod='filterNodeMethod'
          @on-node-drag='nodeDragMove'
          @on-node-drag-end='nodeDragEnd'
          @on-contextmenu='onMenus'
          @on-expand='onExpand'
          @on-node-click='onNodeClick'
          @on-node-dblclick='onNodeDblclick'
          @on-node-copy='onNodeCopy'
        >
          <!-- <template v-slot="{node}">
                     <div class="tree-org-node__text node-label">{{node.label}}</div>
                 </template>
                 <template v-slot:expand="{node}">
                   <div>{{node.children.length}}</div>
                 </template> -->
 
 
          <template v-slot='{node}'>
            <!--           <div class="tree-org-node__text node-label">{{node.label}}</div>-->
            <div v-if='node.leaf' style='width:120px'>
              <div class='tree-org-node__text text-blod shadow'>{{ node.label }}
                <div v-if='node.userList.length'>共{{ node.userList.length }}人</div>
              </div>
 
              <div class='tree-org-node__text  shadow post-item' v-for='(item,index) in node.postList'>
                <div class='text-blod'>
                  <span>{{ item.postName }}</span>
                </div>
                <div>
                <span class='user-item' :class="isCustom(user)  ? 'gray' : null" v-for='user in item.userList'
                      @click.stop='userClick($event,user)'>
                  <!--应届生-->
                      <a-popover title='提示'>
                        <template slot='content'>
                          <p> <a-icon type='star' style='color: #1890FF;' theme='filled' /> 应届生</p>
                        </template>
                              <a-icon v-if='user.grad == 1' type='star' style='color: #1890FF;' theme='filled' />
                      </a-popover>
 
                  <!--借调-->
                      <a-popover title='提示'>
                        <template slot='content'>
                          <p> <a-icon type='golden' style='color: #1890FF;' theme='filled' /> 借调人员</p>
                        </template>
                              <a-icon v-if='user.employStatu == 2' type='golden' style='color: #1890FF;' theme='filled' />
                      </a-popover>
                  <!--试用期-->
 
                  <a-popover title='提示'>
                        <template slot='content'>
                          <p> <a-icon type='thunderbolt' style='color: #1890FF;' theme='filled' /> 试用期</p>
                        </template>
                              <a-icon v-if='user.employ == 2' type='thunderbolt' style='color: #1890FF;' theme='filled' />
                      </a-popover>
                  <!--实习生-->
                   <a-popover title='提示'>
                        <template slot='content'>
                          <p> <a-icon type='flag' style='color: #1890FF;' theme='filled' /> 实习生</p>
                        </template>
                              <a-icon v-if='user.employ == 3' type='flag' style='color: #1890FF;' theme='filled' />
                      </a-popover>
                  <span :class="checkPostList.includes(item.postName)  ? 'text-blue' : null">{{ user.realname }}</span>
 
                </span>
                </div>
 
              </div>
 
            </div>
            <div v-else>
              <div class='tree-org-node__text text-blod shadow'>{{ node.label }}</div>
            </div>
          </template>
          <!--        <template v-slot:expand='{node}'>-->
          <!--          <div>{{ node.children.length }}</div>-->
          <!--        </template>-->
        </zm-tree-org>
      </div>
 
 
      <template>
        <div class='menu-handle'>
          <a-popover placement='topRight'>
            <template slot='content'>
              <depart-info-list :data='data'></depart-info-list>
            </template>
            <div @click="showCmd('tips')" title='提示' class='menu-handle-item'>
          <span class='menu-svg'>
            <a-icon type='bulb' />
          </span>
            </div>
          </a-popover>
 
          <a-popover placement='topRight'>
 
            <template slot='title'>
              <div style='display: flex;justify-content: space-between;align-items: center'>
                <span>选择职务</span>
                <a-button size='small' type='link' @click='cleanCheckPostList'>
                  清空
                </a-button>
              </div>
            </template>
 
            <template slot='content'>
              <depart-post-check-list ref='checkPostModal' :data='postList'
                                      @ok='checkPostListOk'></depart-post-check-list>
            </template>
            <div @click="showCmd('filter')" title='筛选' class='menu-handle-item'>
          <span class='menu-svg'>
            <a-icon type='filter' />
          </span>
            </div>
          </a-popover>
 
          <!--        <div @click="showCmd('camera')" title='截屏' class='menu-handle-item'>
                    <span class='menu-svg'>
                      <a-icon type='camera' />
                    </span>
                  </div>-->
 
 
          <div @click="showCmd('root')" title='设置根目录' class='menu-handle-item'>
          <span class='menu-svg'>
            <a-icon type='cluster' />
          </span>
          </div>
          <div @click="showCmd('setting')" title='设置' class='menu-handle-item'>
          <span class='menu-svg'>
            <a-icon type='setting' />
          </span>
          </div>
 
 
        </div>
      </template>
 
 
      <company-left-drawer ref='leftDrawer' @ok='onSelectDepartSave'
                           @selectDepart='onSelectDepart'></company-left-drawer>
      <company-right-drawer ref='rightDrawer' @settingCmd='settingCmd'></company-right-drawer>
      <!--用户信息-->
      <user-modal ref='userModal' @ok='userModalOk'></user-modal>
    </div>
    <div v-else style='background-color: white;height: calc(100vh - 140px);display: flex;justify-content: center;align-items: center'>
      <a-spin size="large" />
    </div>
  </div>
</template>
 
 
<script>
 
import { queryDepartTreeList } from '@/api/api'
import CompanyLeftDrawer from '@views/hrm/modules/CompanyLeftDrawer'
import CompanyRightDrawer from '@views/hrm/modules/CompanyRightDrawer'
import { getAction } from '@api/manage'
import UserModal from '@views/system/modules/UserModal'
import Vue from 'vue'
import DepartInfoList from '@views/hrm/modules/DepartInfoList'
import DepartPostCheckList from '@views/hrm/modules/DepartPostCheckList'
import html2canvas from 'html2canvas'
 
export default {
  name: 'DepartOrgTree',
  components: { DepartPostCheckList, DepartInfoList, UserModal, CompanyRightDrawer, CompanyLeftDrawer },
  data() {
    return {
      toolBar: {
        scale: true
      },
      keyword: '',
      dataSource: [],
      postList: [],
      checkPostList: [],
      rootNodeName: '研发中心',
      data: {
 
        /* id: 1,
         label: 'xxx科技有限公司',
         children: [
           {
             id: 2,
             pid: 1,
             label: '产品研发部',
             style: { color: '#fff', background: '#108ffe' },
             expand: false,
 
             children: [
               {
                 id: 6,
                 pid: 2,
                 label: '禁止编辑节点',
                 disabled: true
               },
               {
                 id: 7,
                 pid: 2,
                 label: '研发-后端'
               },
               {
                 id: 8,
                 pid: 2,
                 label: '禁止拖拽节点',
                 noDragging: true
               },
               {
                 id: 9,
                 pid: 2,
                 label: '产品经理'
               },
               {
                 id: 10,
                 pid: 2,
                 label: '测试'
               }
             ]
           },
           {
             id: 3,
             pid: 1,
             label: '客服部',
             children: [
               {
                 id: 11,
                 pid: 3,
                 label: '客服一部'
               },
               {
                 id: 12,
                 pid: 3,
                 label: '客服二部'
               }
             ]
           },
           {
             id: 4,
             pid: 1,
             label: '业务部'
           },
           {
             id: 5,
             pid: 1,
             label: '人力资源中心'
           }
         ]*/
      },
      horizontal: false,
      collapsable: true,
      draggable: false,
      disabled: true,
      scalable: true,
      zoomWheel: false,
      onlyOneNode: false,
      cloneNodeDrag: false,
      expandAll: true,
 
      style: {
        background: '#fff',
        color: '#5e6d82'
      }
    }
  },
  created() {
    this.loadSetting()
    this.loadDepartTree()
    this.loadPostList()
    this.toggleExpandAll()
 
 
  },
  methods: {
    loadSetting() {
      //上次设置的根节点
      const save = Vue.ls.get('select_depart_save')
      if (save) {
        this.rootNodeName = save
 
      }
 
      const collapsable = Vue.ls.get('org_setting_collapsable')
      const disabled = Vue.ls.get('org_setting_disabled')
      const scalable = Vue.ls.get('org_setting_scalable')
      const zoomWheel = Vue.ls.get('org_setting_zoomWheel')
      if (collapsable != null) {
        this.collapsable = collapsable
      }
      if (disabled != null) {
        this.disabled = disabled
      }
      if (scalable != null) {
        this.scalable = scalable
      }
      if (zoomWheel != null) {
        this.zoomWheel = zoomWheel
      }
 
 
    },
    checkPostListOk(postList) {
      this.checkPostList = postList
    },
    cleanCheckPostList() {
      this.$refs.checkPostModal.clean()
      this.checkPostList = []
 
    },
    isCustom(user) {
      return user.grad || user.borrow || user.period || user.intern
    },
    onSelectDepart(depart) {
      if (!depart) return
      let res = this.findDataById(this.dataSource, depart)
      if (this.data.id != res.id) {
        this.data = res
        console.info(this.data)
      }
    },
    onSelectDepartSave(depart) {
      if (!depart) {
        this.$message.info('请选择一个部门后保存~')
        return
      }
      let select = this.findDataById(this.dataSource, depart)
      Vue.ls.set('select_depart_save', select.label)
      this.$message.success('设置根节点成功~')
    },
 
    //通过点击的部门查找部门数据
    findDataById(data, id) {
      // 遍历每个节点
      for (var i = 0; i < data.length; i++) {
        var node = data[i]
 
        // 如果当前节点的id等于目标id,返回该节点
        if (node.id === id) {
          return node
        }
 
        // 如果当前节点有子节点,则递归查找子节点
        if (node.children && node.children.length > 0) {
          var result = this.findDataById(node.children, id)
          if (result) {
            return result
          }
        }
      }
 
      // 如果未找到匹配的节点,返回null
      return null
    },
    settingCmd(cmd) {
      const name = cmd.name
      const value = cmd.value
      if (name === 'collapsable') {
        this.collapsable = value
        Vue.ls.set('org_setting_collapsable', value)
      } else if (name === 'disabled') {
        this.disabled = value
        Vue.ls.set('org_setting_disabled', value)
      } else if (name === 'scalable') {
        this.scalable = value
        Vue.ls.set('org_setting_scalable', value)
      } else if (name === 'zoomWheel') {
        this.zoomWheel = value
        Vue.ls.set('org_setting_zoomWheel', value)
      } else if (name === 'bgColor') {
        this.style.background = value
      } else if (name === 'textColor') {
        this.style.color = value
      }
    },
 
 
    showCmd(cmd) {
      if (cmd === 'root') {
        this.$refs.leftDrawer.onOpen()
      } else if (cmd === 'setting') {
        this.$refs.rightDrawer.onOpen()
      } else if (cmd === 'camera') {
        this.toImage()
      }
    },
 
 
    userModalOk() {
      this.loadDepartTree()
    },
    userClick(e, user) {
      this.$refs.userModal.edit(user)
      this.$refs.userModal.departDisabled = false
      this.$message.info(user.realname)
    },
    loadDepartTree() {
 
      getAction('/sys/org/depart', {}).then(res => {
        if (res.success) {
          // let array = this.filterData(res.result)
          // this.dataSource = array
          // console.info(this.dataSource)
          // this.data = this.dataSource[0]
 
 
          let array = res.result
          this.dataSource = array
          this.findDataByName(this.dataSource)
        }
 
      })
    },
    loadPostList() {
      getAction('/sys/position/list', { pageNo: 1, pageSize: 1000,column:'sort',order:'asc' }).then((res) => {
        if (res.success) {
          this.postList = res.result.records
        }
      })
    },
 
    findDataByName(array) {
      for (let i = 0; i < array.length; i++) {
        let item = array[i]
        if (item.label === this.rootNodeName) {
          this.data = item
          return
        }
        if (item.children) {
          this.findDataByName(item.children)
        }
      }
 
    },
 
    filterData(result, pid) {
      for (let i = 0; i < result.length; i++) {
        let item = result[i]
        item.label = item.departName
        item.pid = pid
        if (item.children) {
          this.filterData(item.children, item.id)
        }
      }
      return result
 
    },
 
 
    onMenus({ node, command }) {
      console.log(node, command)
    },
    filter() {
      this.$refs.tree.filter(this.keyword)
    },
    filterNodeMethod(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    onExpand(e, data) {
      console.log(e, data)
    },
    nodeDragMove(data) {
      console.log(data)
    },
    beforeDragEnd(node, targetNode) {
      return new Promise((resolve, reject) => {
        if (!targetNode) reject()
        if (node.id === targetNode.id) {
          reject()
        } else {
          resolve()
        }
      })
    },
    nodeDragEnd(node, parentNode) {
      node.id === parentNode.id && this.$Message.info('移动到自身')
    },
    onNodeClick(e, data) {
      this.$message.info(data.label)
    },
    onNodeDblclick() {
      this.$message.info('双击节点')
    },
    onNodeCopy() {
      this.$message.success('复制成功')
    },
    handleNodeAdd(node) {
      console.log(node)
      this.$message.info('新增节点')
    },
    toggleExpandAll() {
      this.toggleExpand(this.data, this.expandAll)
    },
    toggleExpand(data, val) {
      if (Array.isArray(data)) {
        data.forEach(item => {
          this.$set(item, 'expand', val)
          if (item.children) {
            this.toggleExpand(item.children, val)
          }
        })
      } else {
        this.$set(data, 'expand', val)
        if (data.children) {
          this.toggleExpand(data.children, val)
        }
      }
    },
    // 页面元素转图片
    toImage() {
      // 手动创建一个 canvas 标签
      const canvas = document.createElement('canvas')
      // 获取父标签,意思是这个标签内的 DOM 元素生成图片
      // imageTofile是给截图范围内的父级元素自定义的ref名称
      let canvasBox = this.$refs.departOrg
      // 获取父级的宽高
      const width = parseInt(window.getComputedStyle(canvasBox).width)
      const height = parseInt(window.getComputedStyle(canvasBox).height)
      // 宽高 * 2 并放大 2 倍 是为了防止图片模糊
      canvas.width = width * 2
      canvas.height = height * 2
      canvas.style.width = width + 'px'
      canvas.style.height = height + 'px'
      const context = canvas.getContext('2d')
      context.scale(2, 2)
      const options = {
        backgroundColor: null,
        canvas: canvas,
        useCORS: true
      }
      html2canvas(canvasBox, options).then((canvas) => {
        // toDataURL 图片格式转成 base64
        let dataURL = canvas.toDataURL('image/png')
        console.log(dataURL)
        this.downloadImage(dataURL)
      })
    },
    //下载图片
    downloadImage(url) {
      // 如果是在网页中可以直接创建一个 a 标签直接下载
      let a = document.createElement('a')
      a.href = url
      a.download = '部门结构图'
      a.click()
    }
  }
}
</script>
 
<style lang='less' scoped>
 
.app {
  width: 100%;
  //height: calc(100vh - 140px);
  height: 2000px;
  position: relative;
  overflow: hidden;
}
 
 
.text-blod {
  font-weight: bold;
}
 
.shadow {
  box-shadow: 0 1px 5px rgba(0, 0, 0, .15);
}
 
.box-title {
  display: inline-block;
  white-space: nowrap;
}
 
.post-item {
  margin-top: 10px;
 
  .user-item {
    padding: 2px 4px;
    cursor: pointer;
    word-break: break-word;
  }
 
  .gray {
    color: darkgray;
  }
}
 
 
.menu-handle {
  position: fixed;
  display: flex;
  bottom: 30px;
  right: 80px;
 
  .menu-percent,
  .menu-handle-item {
    width: 32px;
    height: 32px;
    user-select: none;
    line-height: 32px;
    font-size: 12px;
    text-align: center;
  }
 
  .menu-handle-item {
    color: #999;
    border: 1px solid #ddd;
    cursor: pointer;
    position: relative;
    background: #fff;
 
    &:not(:last-child) {
      margin-right: 6px;
    }
 
    &.zoom-out {
      margin-right: -1px;
      z-index: 2;
    }
 
    &.zoom-in:hover {
      z-index: 3;
    }
 
    &:hover {
      color: #2d8cf0;
      background: #f0faff;
      border-color: #2d8cf0;
 
      .menu-restore {
        border-color: #2d8cf0;
 
        &:after {
          border-color: #2d8cf0;
        }
      }
    }
 
    .menu-icon {
      font-size: 18px;
    }
 
    .menu-restore {
      display: inline-block;
      width: 10px;
      height: 10px;
      border: 1px solid #aaa;
      margin-left: -2px;
      margin-right: -2px;
 
      &:after {
        content: "";
        display: block;
        height: 100%;
        border-top: 1px solid #aaa;
        border-right: 1px solid #aaa;
        transform: translate(3px, -3px);
      }
    }
 
    .menu-svg {
      display: flex;
      height: 100%;
      justify-content: center;
      align-items: center;
 
      img {
        width: 50%;
        height: 50%;
        vertical-align: middle;
        opacity: 0.5;
      }
    }
  }
}
 
.text-blue {
  color: #1890FF;
}
</style>