puzhibing
2026-08-28 e92cd56bb56b111c1afe9922191f9897215ff6b0
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
<template>
  <div class="perm-matrix">
    <el-table :data="tableRows" border class="matrix-table" :span-method="spanMethod">
      <el-table-column label="板块" width="200">
        <template #default="{ row }">
          <div v-if="row.showModuleCell" class="cell-inner">
            <el-checkbox
              size="small"
              :model-value="moduleFullyChecked(row.module)"
              :indeterminate="moduleIndeterminate(row.module)"
              :disabled="disabled"
              @change="(v: CheckboxValue) => toggleModule(row.module, Boolean(v))"
            />
            <span class="cell-label">{{ row.module.label }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="页面" width="200">
        <template #default="{ row }">
          <div v-if="row.showPageCell" class="cell-inner">
            <el-checkbox
              size="small"
              :model-value="pageFullyChecked(row.page)"
              :indeterminate="pageIndeterminate(row.page)"
              :disabled="disabled"
              @change="(v: CheckboxValue) => togglePage(row.page, row.module, Boolean(v))"
            />
            <span class="cell-label">{{ row.page.label }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="权限" min-width="240">
        <template #default="{ row }">
          <div v-if="row.perm" class="perm-cell">
            <el-checkbox
              size="small"
              :model-value="hasId(row.perm.id)"
              :disabled="disabled"
              @change="(v: CheckboxValue) => togglePerm(row.perm!.id, row.page, row.module, Boolean(v))"
            />
            <span class="perm-label">{{ row.perm.label }}</span>
          </div>
          <span v-else class="muted">—</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
 
<script setup lang="ts">
import { computed, shallowRef, watch } from "vue";
import type { MenuNode } from "../types/sysMenu";
 
type CheckboxValue = string | number | boolean;
 
const props = withDefaults(
  defineProps<{
    menuTree: MenuNode[];
    modelValue: number[];
    disabled?: boolean;
  }>(),
  { disabled: false }
);
 
const emit = defineEmits<{
  "update:modelValue": [value: number[]];
}>();
 
/** 每个权限一行;板块、页面列 rowspan */
type MatrixRow = {
  module: MenuNode;
  page: MenuNode;
  perm: MenuNode | null;
  moduleRowspan: number;
  pageRowspan: number;
  showModuleCell: boolean;
  showPageCell: boolean;
};
 
function permChildren(page: MenuNode): MenuNode[] {
  return (page.children || []).filter((c) => c.menuType === 3);
}
 
function pageNodesUnderModule(mod: MenuNode): MenuNode[] {
  const ch = mod.children || [];
  const pages = ch.filter((c) => c.menuType === 2);
  if (pages.length > 0) {
    return pages;
  }
  const perms = ch.filter((c) => c.menuType === 3);
  if (perms.length > 0) {
    return [{ ...mod, children: perms }];
  }
  return [];
}
 
function collectDescendantIds(node: MenuNode): number[] {
  const ids: number[] = [node.id];
  for (const c of node.children || []) {
    ids.push(...collectDescendantIds(c));
  }
  return ids;
}
 
function permIdsOnly(page: MenuNode): number[] {
  return permChildren(page).map((p) => p.id);
}
 
function pageSubtreeComplete(page: MenuNode, s: Set<number>): boolean {
  const pids = permIdsOnly(page);
  if (pids.length === 0) {
    return s.has(page.id);
  }
  return pids.every((id) => s.has(id));
}
 
function pageHasAny(page: MenuNode, s: Set<number>): boolean {
  const pids = permIdsOnly(page);
  if (pids.some((id) => s.has(id))) {
    return true;
  }
  return s.has(page.id);
}
 
function moduleHasAny(mod: MenuNode, s: Set<number>): boolean {
  return collectDescendantIds(mod).some((id) => id !== mod.id && s.has(id));
}
 
function syncPageAndModule(page: MenuNode, mod: MenuNode, s: Set<number>) {
  const pids = permIdsOnly(page);
  if (pids.length > 0) {
    if (pids.every((id) => s.has(id))) {
      s.add(page.id);
    } else {
      s.delete(page.id);
    }
  }
  const pages = pageNodesUnderModule(mod);
  if (pages.length > 0) {
    if (pages.every((p) => pageSubtreeComplete(p, s))) {
      s.add(mod.id);
    } else {
      s.delete(mod.id);
    }
  }
}
 
function syncAllAncestors(roots: MenuNode[], s: Set<number>) {
  for (const mod of roots) {
    const pages = pageNodesUnderModule(mod);
    for (const page of pages) {
      syncPageAndModule(page, mod, s);
    }
  }
}
 
const tableRows = computed<MatrixRow[]>(() => {
  const roots = props.menuTree || [];
  const rows: MatrixRow[] = [];
  for (const mod of roots) {
    const pages = pageNodesUnderModule(mod);
    if (pages.length === 0) {
      continue;
    }
    let moduleRowCount = 0;
    for (const page of pages) {
      const perms = permChildren(page);
      moduleRowCount += Math.max(1, perms.length);
    }
 
    let isFirstInModule = true;
    for (const page of pages) {
      const perms = permChildren(page);
      const pageRowspan = Math.max(1, perms.length);
      if (perms.length === 0) {
        rows.push({
          module: mod,
          page,
          perm: null,
          moduleRowspan: moduleRowCount,
          pageRowspan: 1,
          showModuleCell: isFirstInModule,
          showPageCell: true,
        });
        isFirstInModule = false;
      } else {
        perms.forEach((perm, idx) => {
          rows.push({
            module: mod,
            page,
            perm,
            moduleRowspan: moduleRowCount,
            pageRowspan: perms.length,
            showModuleCell: isFirstInModule && idx === 0,
            showPageCell: idx === 0,
          });
        });
        isFirstInModule = false;
      }
    }
  }
  return rows;
});
 
const innerRef = shallowRef(new Set<number>());
 
function sortedJson(ids: Iterable<number>) {
  return JSON.stringify([...ids].sort((a, b) => a - b));
}
 
watch(
  [() => props.modelValue, () => props.menuTree],
  () => {
    const inner = new Set(props.modelValue ?? []);
    const before = sortedJson(inner);
    syncAllAncestors(props.menuTree || [], inner);
    innerRef.value = inner;
    if (sortedJson(inner) !== before) {
      emit("update:modelValue", [...inner]);
    }
  },
  { immediate: true, deep: true }
);
 
function commitSelection(next: Set<number>) {
  syncAllAncestors(props.menuTree || [], next);
  innerRef.value = next;
  emit("update:modelValue", [...next]);
}
 
function hasId(id: number): boolean {
  return innerRef.value.has(id);
}
 
function moduleFullyChecked(mod: MenuNode): boolean {
  const s = innerRef.value;
  const pages = pageNodesUnderModule(mod);
  return pages.length > 0 && pages.every((p) => pageSubtreeComplete(p, s));
}
 
function moduleIndeterminate(mod: MenuNode): boolean {
  const s = innerRef.value;
  return moduleHasAny(mod, s) && !moduleFullyChecked(mod);
}
 
function pageFullyChecked(page: MenuNode): boolean {
  return pageSubtreeComplete(page, innerRef.value);
}
 
function pageIndeterminate(page: MenuNode): boolean {
  const s = innerRef.value;
  const pids = permIdsOnly(page);
  if (pids.length === 0) {
    return false;
  }
  return pageHasAny(page, s) && !pageSubtreeComplete(page, s);
}
 
function toggleModule(mod: MenuNode, on: boolean) {
  const inner = new Set(innerRef.value);
  const ids = collectDescendantIds(mod);
  if (on) {
    ids.forEach((id) => inner.add(id));
  } else {
    ids.forEach((id) => inner.delete(id));
  }
  commitSelection(inner);
}
 
function togglePage(page: MenuNode, mod: MenuNode, on: boolean) {
  const inner = new Set(innerRef.value);
  const ids = collectDescendantIds(page);
  if (on) {
    ids.forEach((id) => inner.add(id));
  } else {
    ids.forEach((id) => inner.delete(id));
  }
  syncPageAndModule(page, mod, inner);
  commitSelection(inner);
}
 
function togglePerm(permId: number, page: MenuNode, mod: MenuNode, on: boolean) {
  const inner = new Set(innerRef.value);
  if (on) {
    inner.add(permId);
  } else {
    inner.delete(permId);
  }
  syncPageAndModule(page, mod, inner);
  commitSelection(inner);
}
 
function spanMethod({
  row,
  columnIndex,
}: {
  row: MatrixRow;
  columnIndex: number;
}) {
  if (columnIndex === 0) {
    if (row.showModuleCell) {
      return { rowspan: row.moduleRowspan, colspan: 1 };
    }
    return { rowspan: 0, colspan: 0 };
  }
  if (columnIndex === 1) {
    if (row.showPageCell) {
      return { rowspan: row.pageRowspan, colspan: 1 };
    }
    return { rowspan: 0, colspan: 0 };
  }
  return { rowspan: 1, colspan: 1 };
}
</script>
 
<style scoped>
.perm-matrix {
  width: 100%;
}
.matrix-table {
  width: 100%;
}
/* 行高约压到默认的一半:收紧单元格与勾选 */
.matrix-table :deep(.el-table__cell) {
  padding: 3px 8px;
}
.matrix-table :deep(.cell) {
  line-height: 1.2;
}
.matrix-table :deep(.el-checkbox) {
  margin-right: 0;
  height: auto;
}
.matrix-table :deep(.el-checkbox__label) {
  padding-left: 6px;
}
.cell-inner {
  display: flex;
  align-items: center;
  gap: 4px;
}
.cell-label {
  font-size: 12px;
}
.perm-cell {
  display: flex;
  align-items: center;
  gap: 4px;
}
.perm-label {
  font-size: 12px;
}
.muted {
  color: var(--el-text-color-secondary);
  font-size: 12px;
  line-height: 1.2;
}
</style>