fuchengshen
2022-06-22 9fa82135f46592776883a9913dc2f3d126ccf1c2
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
<template>
    <el-button type="success" size="mini" @click="create">快速创建</el-button>
 
    <cl-form :ref="setRefs('form')" />
</template>
 
<script lang="ts">
import { defineComponent } from "vue";
import { last, isEmpty } from "/@/cool/utils";
import { ElMessage } from "element-plus";
import { useCool } from "/@/cool";
 
export default defineComponent({
    name: "cl-menu-quick",
 
    emits: ["success"],
 
    setup(_, { emit }) {
        const { service, refs, setRefs } = useCool();
 
        // 快速创建
        async function create() {
            // 模块列表
            const modules = await service.request({
                url: "/__cool_modules"
            });
 
            // 数据结构列表
            const eps: any[] = await service.base.common.eps();
            const entities: any[] = [];
 
            for (const i in eps) {
                eps[i].forEach((e: any) => {
                    if (!isEmpty(e.columns)) {
                        entities.push({
                            label: `${e.name}(${e.prefix})`,
                            value: entities.length,
                            filename: last(e.prefix.split("/")),
                            ...e
                        });
                    }
                });
            }
 
            // 打开
            refs.value.form.open({
                title: "快速创建",
                width: "900px",
                items: [
                    {
                        prop: "module",
                        label: {
                            text: "模块名称",
                            tip: "菜单文件存放在所选模块的 views 目录下",
                            icon: "el-icon-question"
                        },
                        span: 9,
                        component: {
                            name: "el-select",
                            props: {
                                filterable: true,
                                clearable: true
                            },
                            options: modules.map((e: string) => {
                                return {
                                    label: e,
                                    value: e
                                };
                            })
                        },
                        required: true
                    },
                    {
                        prop: "entity",
                        label: {
                            text: "数据结构",
                            tip: "所选实体会通过规则配置自动转换",
                            icon: "el-icon-question"
                        },
                        span: 15,
                        component: {
                            name: "el-select",
                            props: {
                                filterable: true,
                                clearable: true,
                                onChange(i: number) {
                                    refs.value.form.setForm(
                                        "router",
                                        "/" +
                                            (refs.value.form.getForm("module") || "test") +
                                            "/" +
                                            entities[i].filename
                                    );
                                }
                            },
                            options: entities
                        },
                        required: true
                    },
                    {
                        prop: "name",
                        label: "菜单名称",
                        span: 9,
                        component: {
                            name: "el-input",
                            props: {
                                placeholder: "请输入菜单名称"
                            }
                        },
                        required: true
                    },
                    {
                        prop: "router",
                        label: "菜单路由",
                        span: 15,
                        component: {
                            name: "el-input",
                            props: {
                                placeholder: "请输入菜单路由,如:/test"
                            }
                        }
                    },
                    {
                        prop: "parentId",
                        label: "上级节点",
                        component: {
                            name: "cl-menu-tree"
                        }
                    },
                    {
                        prop: "keepAlive",
                        value: true,
                        label: "路由缓存",
                        component: {
                            name: "el-radio-group",
                            options: [
                                {
                                    label: "开启",
                                    value: true
                                },
                                {
                                    label: "关闭",
                                    value: false
                                }
                            ]
                        }
                    },
                    {
                        prop: "icon",
                        label: "菜单图标",
                        component: {
                            name: "cl-menu-icons"
                        }
                    },
                    {
                        prop: "orderNum",
                        label: "排序号",
                        component: {
                            name: "el-input-number",
                            props: {
                                placeholder: "请填写排序号",
                                min: 0,
                                max: 99,
                                "controls-position": "right"
                            }
                        }
                    }
                ],
                on: {
                    submit(data: any, { done, close }: any) {
                        // 选择的数据结构
                        const item = entities[data.entity];
 
                        // 插入菜单
                        service.base.sys.menu
                            .add({
                                type: 1,
                                isShow: true,
                                viewPath: `cool/modules/${data.module}/views/${item.filename}.vue`,
                                ...data
                            })
                            .then((res: any) => {
                                // 权限列表
                                const perms: any[] = [];
 
                                item.api.forEach((e: any) => {
                                    const d: any = {
                                        type: 2,
                                        parentId: res.id,
                                        name: e.summary || e.path,
                                        perms: [e.path]
                                    };
 
                                    if (e.path == "/update") {
                                        if (item.api.find((a: any) => a.path == "/info")) {
                                            d.perms.push("/info");
                                        }
                                    }
 
                                    d.perms = d.perms
                                        .map((e: string) =>
                                            (item.prefix.replace("/admin/", "") + e).replace(
                                                /\//g,
                                                ":"
                                            )
                                        )
                                        .join(";");
 
                                    perms.push(d);
                                });
 
                                // 批量插入权限
                                service.base.sys.menu.add(perms).then(() => {
                                    emit("success");
                                    close();
 
                                    service.request({
                                        url: "/__cool_createMenu",
                                        method: "POST",
                                        data: {
                                            ...item,
                                            ...data
                                        }
                                    });
                                });
                            })
                            .catch((err: string) => {
                                ElMessage.error(err);
                                done();
                            });
                    }
                }
            });
        }
 
        return {
            refs,
            setRefs,
            create
        };
    }
});
</script>