zzw
18 小时以前 5643f9caa25e78810e05073468c846411a373d04
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
<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-radio-group v-model="permissionScope" size="small" @change="handleScopeChange">
        <el-radio-button label="all">全部</el-radio-button>
        <el-radio-button label="assigned">已设置</el-radio-button>
      </el-radio-group>
      <el-checkbox v-if="permissionScope === 'all'" v-model="checkAll" @change="toggleAll">全选</el-checkbox>
      <span class="permission-tip">{{ permissionTip }}</span>
    </div>
    <div v-loading="loading" class="permission-tree">
      <el-tree
        ref="permissionTree"
        :data="displayMenus"
        node-key="id"
        show-checkbox
        default-expand-all
        empty-text="该角色尚未设置权限"
        :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,
      permissionScope: 'all',
      selectedRoleIds: []
    }
  },
  computed: {
    dialogVisible: {
      get() { return this.visible },
      set(value) { this.$emit('update:visible', value) }
    },
    allMenuIds() {
      return this.flatMenus.map(item => String(item.id))
    },
    displayMenus() {
      if (this.permissionScope === 'all') return this.menus
      const assigned = new Set(this.selectedRoleIds.map(String))
      const filterAssigned = nodes => (nodes || []).reduce((result, node) => {
        const children = filterAssigned(node.children)
        if (assigned.has(String(node.id)) || children.length) {
          result.push({ ...node, children })
        }
        return result
      }, [])
      return filterAssigned(this.menus)
    },
    permissionTip() {
      return this.permissionScope === 'all'
        ? '菜单和按钮权限均可勾选,保存后切换角色会自动反显。'
        : '仅显示当前角色已设置的权限及其父级节点。'
    }
  },
  watch: {
    visible(value) {
      if (value) this.open()
    },
  },
  methods: {
    open() {
      if (!this.role || !this.role.id) return
      this.loading = true
      this.checkAll = false
      this.permissionScope = 'all'
      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 })
 
    },
    handleScopeChange() {
      this.selectedRoleIds = this.checkedMenuIds()
      this.$nextTick(() => {
        if (!this.$refs.permissionTree) return
        this.$refs.permissionTree.setCheckedKeys(this.displayCheckedKeys(this.selectedRoleIds))
        this.syncCheckAllState()
      })
    },
    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() {
      const checked = this.checkedMenuIds()
      this.selectedRoleIds = checked
      this.checkAll = checked.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>