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
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
<template>
    <el-button v-if="isDev" style="margin-left: 10px" @click="autoCreate">{{
        $t('自动添加')
    }}</el-button>
 
    <cl-form ref="Form">
        <template #slot-list="{ scope }">
            <el-scrollbar class="scrollbar">
                <div v-for="(item, index) in scope.list" :key="index" class="list">
                    <el-divider content-position="left">{{ item.prefix }}</el-divider>
 
                    <div v-for="(a, ai) in item.api" :key="ai" class="item">
                        <!-- 是否开启 -->
                        <el-switch v-model="a.checked"></el-switch>
 
                        <!-- 名称 -->
                        <el-input
                            v-model="a.summary"
                            clearable
                            :placeholder="$t('权限名称')"
                            :disabled="!a.checked"
                        />
 
                        <!-- 权限 -->
                        <cl-menu-perms v-model="a.perms" :disabled="!a.checked" />
                    </div>
                </div>
            </el-scrollbar>
        </template>
    </cl-form>
</template>
 
<script setup lang="ts">
defineOptions({
    name: 'auto-perms'
});
 
import { useForm } from '@cool-vue/crud';
import { useCool } from '/@/cool';
import { deepPaths } from '/@/cool/utils';
import { ElMessage } from 'element-plus';
import { isDev } from '/@/config';
import { useI18n } from 'vue-i18n';
 
const props = defineProps({
    menuId: [String, Number]
});
 
const emit = defineEmits(['open', 'close']);
 
const { service } = useCool();
const { t } = useI18n();
const Form = useForm();
 
// 获取实体数据
async function getEntity() {
    return service.base.open.eps().then((eps: EpsData) => {
        const modules: EpsModule[] = [];
        const paths: string[] = [];
 
        // 遍历实体
        for (const i in eps) {
            eps[i].forEach(e => {
                e.prefix = e.prefix?.replace('/admin/', '');
 
                if (e.prefix) {
                    paths.push(e.prefix);
                }
 
                modules.push(e);
            });
        }
 
        return {
            prop: 'entity',
            label: t('实体数据'),
            component: {
                name: 'el-cascader',
                props: {
                    options: deepPaths(paths),
                    separator: '.',
                    props: {
                        multiple: true
                    },
                    onChange(arr: string[][]) {
                        const list: any[] = [];
 
                        arr.forEach(v => {
                            const d = modules.find(e => e.prefix == v.join('/'));
 
                            if (d) {
                                d.api.forEach(e => {
                                    e.perms = (d.prefix + e.path).replace(/\//g, ':');
                                    e.checked = true;
                                });
 
                                list.push(d);
                            }
                        });
 
                        // 渲染列表
                        Form.value?.setForm('list', list);
                    }
                }
            }
        };
    });
}
 
// 自动创建
async function autoCreate() {
    emit('open');
 
    Form.value?.open({
        title: t('自动添加权限'),
        width: '800px',
        dialog: {
            draggable: true,
            controls: ['close']
        },
        props: {
            labelPosition: 'top'
        },
        op: {
            saveButtonText: t('一键添加')
        },
        items: [
            await getEntity(),
            {
                prop: 'list',
                label: t('权限列表'),
                value: [],
                hidden({ scope }) {
                    return !scope.entity;
                },
                component: {
                    name: 'slot-list'
                }
            }
        ],
        on: {
            submit(data: { list: any[]; entity: any }, { done, close }) {
                if (!data.entity) {
                    ElMessage.error(t('请选择实体数据'));
                    done();
                    return;
                }
 
                // 选中权限
                const checked: EpsApi[] = data.list
                    .map(e => e.api)
                    .flat()
                    .filter(e => e.checked);
 
                if (checked.find(e => !e.summary)) {
                    ElMessage.error(t('请填写权限名称'));
                    done();
                    return;
                }
 
                if (checked.length == 0) {
                    ElMessage.error(t('请至少选择一个权限'));
                    done();
                    return;
                }
 
                Promise.all(
                    checked.map(e => {
                        return service.base.sys.menu.add({
                            type: 2,
                            parentId: props.menuId,
                            name: e.summary,
                            perms: e.perms
                        });
                    })
                )
                    .then(() => {
                        ElMessage.success(t('添加权限成功'));
                        close();
                        emit('close');
                    })
                    .catch(err => {
                        done();
                        ElMessage.error(err.message);
                    });
            }
        }
    });
}
</script>
 
<style lang="scss" scoped>
.scrollbar {
    max-height: 500px;
 
    .list {
        .item {
            display: flex;
            align-items: center;
            margin-bottom: 10px;
 
            .el-input {
                margin: 0 10px;
                width: 200px;
            }
 
            .cl-menu-perms {
                flex: 1;
            }
        }
    }
}
</style>