wangzhibo
2026-07-16 b57020623cf0c946706573bf102e548c3a544423
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
<template>
    <div class="cl-menu-perms">
        <el-cascader
            v-model="value"
            separator=":"
            clearable
            filterable
            collapse-tags
            collapse-tags-tooltip
            :disabled="disabled"
            :options="data"
            :props="cascaderProps"
            @change="onChange"
        />
    </div>
</template>
 
<script lang="ts" setup>
defineOptions({
    name: 'cl-menu-perms'
});
 
import { onMounted, ref, watch, reactive } from 'vue';
import { useCool } from '/@/cool';
import { deepPaths } from '/@/cool/utils';
 
const props = defineProps({
    modelValue: {
        type: String,
        default: ''
    },
    disabled: Boolean
});
 
const emit = defineEmits(['update:modelValue']);
 
const { service } = useCool();
 
// 绑定值
const value = ref<string[][]>([]);
 
// 权限列表
const data = ref<any[]>([]);
 
// elm BUG
const cascaderProps = reactive({ multiple: true });
 
// 监听改变
function onChange(arr: any) {
    emit('update:modelValue', arr.map((e: string[]) => e.join(':')).join(','));
}
 
// 监听值
watch(
    () => props.modelValue,
    val => {
        value.value = val ? val.split(',').map(e => e.split(':')) : [];
    },
    {
        immediate: true
    }
);
 
onMounted(() => {
    const list: any[] = [];
 
    function deep(s: any) {
        if (typeof s == 'object') {
            for (const i in s) {
                const { permission } = s[i];
 
                if (permission) {
                    list.push(...Object.values(permission));
                } else {
                    deep(s[i]);
                }
            }
        }
    }
 
    deep(service);
 
    data.value = deepPaths(list, ':');
});
</script>
 
<style lang="scss" scoped>
.cl-menu-perms {
    line-height: 0;
 
    :deep(.el-cascader) {
        width: 100%;
    }
}
</style>