zzw
昨天 943b68c17b27f3b0f9973533f832c48dd6afbb6f
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
<template>
  <el-dialog
    title="角色权限设置"
    :visible.sync="dialogVisible"
    width="860px"
    append-to-body
    :close-on-click-modal="false"
  >
    <div class="permission-head">
      <div>
        <span class="role-label">当前角色</span>
        <strong>{{ role.name }}</strong>
        <span class="role-code">{{ role.code }}</span>
      </div>
      <span class="selected-total">已选权限 {{ selectedRoleIds.length }} 个</span>
    </div>
    <div class="permission-toolbar">
      <el-checkbox v-model="checkAll" @change="toggleAll">全选</el-checkbox>
      <span class="permission-tip">菜单和按钮权限均可勾选,保存后切换角色会自动反显。</span>
    </div>
    <div v-loading="loading" class="permission-tree">
      <el-tree
        ref="permissionTree"
        :data="menus"
        node-key="id"
        show-checkbox
        default-expand-all
        :render-content="renderContent"
        :props="{ label: 'name', children: 'children' }"
        @check="syncCheckAllState"
      ></el-tree>
    </div>
    <template slot="footer">
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" :loading="saving" v-permission="'system:roles:permission'" @click="save">保存</el-button>
    </template>
  </el-dialog>
</template>
 
<script>
import api from '@/api'
export default {
  name: 'RolePermissionDialog',
  props: {
    visible: { type: Boolean, default: false },
    role: { type: Object, default: () => ({}) }
  },
  data() {
    return {
      menus: [],
      flatMenus: [],
      loading: false,
      saving: false,
      checkAll: false,
      selectedRoleIds: []
    }
  },
  computed: {
    dialogVisible: {
      get() { return this.visible },
      set(value) { this.$emit('update:visible', value) }
    },
    allMenuIds() {
      return this.flatMenus.map(item => String(item.id))
    }
  },
  watch: {
    visible(value) {
      if (value) this.open()
    },
  },
  methods: {
    open() {
      if (!this.role || !this.role.id) return
      this.loading = true
      this.checkAll = false
      this.selectedRoleIds = []
      Promise.all([
        api.get('/system/menu/tree'),
        api.get('/system/menu/list'),
        api.get('/system/menu-permissions', { params: { roleId: this.role.id } })
      ]).then(([tree, flat, permissions]) => {
        this.menus = tree.data || []
        this.flatMenus = flat.data || []
        const data = permissions.data || {}
        this.selectedRoleIds = (data.roleMenuIds || []).map(String)
        this.$nextTick(() => {
          if (!this.$refs.permissionTree) return
          this.$refs.permissionTree.setCheckedKeys(this.displayCheckedKeys(this.selectedRoleIds))
          this.syncCheckAllState()
        })
      }).finally(() => { this.loading = false })
    },
    renderContent(h, { data }) {
      const label = data.menuType === 2 ? data.name + ' [按钮 ' + (data.permissions || '') + ']' : data.name
      return h('span', { class: { 'perm-button-node': data.menuType === 2 } }, label)
    },
    displayCheckedKeys(ids) {
      const selected = new Set((ids || []).map(String))
      const children = {}
      const menuIds = new Set(this.flatMenus.map(item => String(item.id)))
      this.flatMenus.forEach(item => {
        const id = String(item.id)
        const pid = item.pid == null ? '0' : String(item.pid)
        if (!children[pid]) children[pid] = []
        children[pid].push(id)
      })
      const result = []
      const collectFullySelected = id => {
        if (!selected.has(id)) return false
        const childIds = children[id] || []
        if (!childIds.length) {
          result.push(id)
          return true
        }
        const allChildrenSelected = childIds.map(collectFullySelected).every(Boolean)
        if (allChildrenSelected) result.push(id)
        return allChildrenSelected
      }
      this.flatMenus
        .filter(item => item.pid == null || String(item.pid) === '0' || !menuIds.has(String(item.pid)))
        .forEach(item => collectFullySelected(String(item.id)))
      return result
    },
    checkedMenuIds() {
      const tree = this.$refs.permissionTree
      if (!tree) return []
      return [...new Set([...(tree.getCheckedKeys(false) || []), ...(tree.getHalfCheckedKeys() || [])].map(String))]
    },
    syncCheckAllState() {
      this.checkAll = this.checkedMenuIds().length === this.allMenuIds.length
    },
    toggleAll() {
      if (!this.$refs.permissionTree) return
      this.$refs.permissionTree.setCheckedKeys(this.checkAll ? this.allMenuIds : [])
      this.$nextTick(this.syncCheckAllState)
    },
    save() {
      const roleMenuIds = this.checkedMenuIds()
      this.saving = true
      api.post('/system/menu-permissions', { roleId: this.role.id, roleMenuIds }).then(() => {
        this.selectedRoleIds = roleMenuIds
        this.$message.success('角色权限已保存')
        this.$emit('saved')
        this.dialogVisible = false
      }).finally(() => { this.saving = false })
    }
  }
}
</script>
 
<style scoped>
.permission-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
  color: #31465d;
}
.role-label {
  margin-right: 10px;
  color: #7b8b9c;
  font-size: 13px;
}
.role-code {
  margin-left: 8px;
  color: #93a2b1;
  font-size: 12px;
}
.selected-total {
  color: #2f658f;
  font-size: 13px;
}
.permission-toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 10px;
}
.permission-tip {
  color: #7b8a9a;
  font-size: 12px;
}
.permission-tree {
  height: 460px;
  padding: 8px 10px;
  border: 1px solid #dce4ec;
  border-radius: 4px;
  overflow: auto;
}
::v-deep .perm-button-node {
  color: #b8791f;
}
</style>