MrShi
2025-05-21 f5ab83dfad01c87828232af8cd18c676e69fd917
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
<template>
  <TreeSelect
    :placeholder="placeholder"
    :value="value"
    :data="data"
    :append-to-body="appendToBody"
    :clearable="clearable"
    :inline="inline"
    @input="$emit('input', $event)"
  />
</template>
 
<script>
import TreeSelect from './TreeSelect'
// import { fetchTree, fetchTree1 } from '@/api/system/menu'
import { fetchTree } from '@/api/system/menu'
export default {
  name: 'MenuSelect',
  components: { TreeSelect },
  props: {
    value: {},
    inline: {
      default: true
    },
    type: {
      default: '0'
    },
    placeholder: {
      default: '请选择菜单'
    },
    // 是否可清空
    clearable: {
      default: false
    },
    appendToBody: {
      default: false
    },
    // 需被排除的部门ID
    excludeId: {}
  },
  data () {
    return {
      data: []
    }
  },
  watch: {
    excludeId () {
      this.fetchData()
    }
  },
  methods: {
    // 获取所有菜单
    fetchData () {
      if (this.type === '1') {
        // fetchTree1()
        //   .then(records => {
        //     this.data = []
        //     this.__fillData(this.data, records)
        //   })
        //   .catch(e => {
        //     this.$tip.apiFailed(e)
        //   })
      } else {
        fetchTree()
          .then(records => {
            this.data = []
            this.__fillData(this.data, records)
          })
          .catch(e => {
            this.$tip.apiFailed(e)
          })
      }
    },
    // 填充菜单树
    __fillData (list, pool) {
      for (const menu of pool) {
        if (menu.id === this.excludeId) {
          continue
        }
        const menuNode = {
          id: menu.id,
          label: menu.name
        }
        list.push(menuNode)
        if (menu.children != null && menu.children.length > 0) {
          menuNode.children = []
          this.__fillData(menuNode.children, menu.children)
          if (menuNode.children.length === 0) {
            menuNode.children = undefined
          }
        }
      }
    }
  },
  created () {
    this.fetchData()
  }
}
</script>