Blame view

src/components/role/PrivilegeTree.vue 4.33 KB
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
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
  <template>
    <div class="privilege-tree">
      <el-tree
        ref="privilegeTree"
        :data="treeData"
        :props="defaultProps"
        show-checkbox
        node-key="id"
        :default-expanded-keys="expandedKeys"
        :default-checked-keys="checkedKeys"
        @check="handleCheckChange"
      ></el-tree>
    </div>
  </template>
  
  <script>
  import { queryPrivilegeGroupNoAddPrivilege, addPrivilegeToPrivilegeGroup, deletePrivilegeFromPrivilegeGroup } from '@/api/role/roleApi'
  
  export default {
    name: 'PrivilegeTree',
    props: {
      pgId: {
        type: String,
        required: true
      }
    },
    data() {
      return {
        privileges: [],
        treeData: [],
        defaultProps: {
          children: 'children',
          label: 'text'
        },
        expandedKeys: [],
        checkedKeys: [],
        // Add a variable to store previous state
        previousCheckedKeys: []
      }
    },
    watch: {
      pgId: {
        immediate: true,
        handler(newVal) {
          if (newVal) {
            this.loadPrivileges(newVal)
          }
        }
      }
    },
    methods: {
      async loadPrivileges(pgId) {
        try {
          const params = {
            pgId,
          }
          const res = await queryPrivilegeGroupNoAddPrivilege(params)
          this.privileges = res
          this.checkedKeys= []
          this.formatTreeData()
          // Store initial checked state
          this.previousCheckedKeys = [...this.checkedKeys]
        } catch (error) {
          this.$message.error(error.message)
        }
      },
      formatTreeData() {
        const groups = []
        const expandedKeys = []
        
        this.privileges.forEach(item => {
          if (!groups.some(g => g.gId === item.gId)) {
            const group = {
              id: `g_${item.gId}`,
              gId: item.gId,
              text: item.gName,
              children: this.formatMenuData(item.gId)
            }
            groups.push(group)
            expandedKeys.push(`g_${item.gId}`)
          }
        })
        
        this.treeData = groups
        this.expandedKeys = expandedKeys
      },
      formatMenuData(gId) {
        const menus = []
        this.privileges
          .filter(item => item.gId === gId)
          .forEach(item => {
            if (!menus.some(m => m.mId === item.mId)) {
              const menu = {
                id: `m_${item.mId}`,
                mId: item.mId,
                text: item.mName,
                children: this.formatPrivilegeData(item.mId)
              }
              menus.push(menu)
            }
          })
        return menus
      },
      formatPrivilegeData(mId) {
        return this.privileges
          .filter(item => item.mId === mId)
          .map(item => {
            const node = {
              id: `p_${item.pId}`,
              pId: item.pId,
              text: item.pName
            }
            if (item.pgId) {
              this.checkedKeys.push(`p_${item.pId}`)
            }
            return node
          })
      },
      async handleCheckChange(data, { checkedKeys }) {
        // Get current checked privilege IDs
        const currentPrivilegeIds = checkedKeys
          .filter(key => key.startsWith('p_'))
          .map(key => key.replace('p_', ''))
        
        // Find added privileges
        const addedPrivileges = currentPrivilegeIds.filter(id => 
          !this.previousCheckedKeys.includes(`p_${id}`)
        )
        
        // Find removed privileges
        const removedPrivileges = this.previousCheckedKeys
          .filter(key => key.startsWith('p_'))
          .map(key => key.replace('p_', ''))
          .filter(id => !currentPrivilegeIds.includes(id))
        
        try {
          if (addedPrivileges.length > 0) {
            await addPrivilegeToPrivilegeGroup({
              pgId: this.pgId,
              pIds: addedPrivileges.map(pId => ({ pId }))
            })
          }
          if (removedPrivileges.length > 0) {
            await deletePrivilegeFromPrivilegeGroup({
              pgId: this.pgId,
              pIds: removedPrivileges.map(pId => ({ pId }))
            })
          }
          
          // Update previous state only after successful API calls
          this.previousCheckedKeys = [...checkedKeys]
        } catch (error) {
          this.$message.error(error.message)
          // Revert changes if API call fails
          this.$nextTick(() => {
            this.$refs.privilegeTree.setCheckedKeys([...this.previousCheckedKeys])
          })
        }
      }
    }
  }
  </script>
  
  <style scoped>
  .privilege-tree {
    background: #fff;
    border-radius: 4px;
    padding: 15px;
    margin-top: 10px;
  }
  </style>