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
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
import { defineComponent, h, inject, reactive, ref, toRefs } from "vue";
import { ElMessage } from "element-plus";
import { useCore, useProxy } from "../../hooks";
import { useApi } from "../form/helper";
import { mergeConfig } from "../../utils";
 
export default defineComponent({
    name: "cl-upsert",
 
    props: {
        // 表单项
        items: {
            type: Array,
            default: () => []
        },
        // <el-form /> 参数
        props: Object,
        // 编辑时是否同步打开
        sync: Boolean,
        // 操作按钮参数
        op: Object,
        // <cl-dialog /> 参数
        dialog: Object,
        // 打开表单钩子
        onOpen: Function,
        // 打开表单后钩子
        onOpened: Function,
        // 关闭表单钩子
        onClose: Function,
        // 关闭表单后钩子
        onClosed: Function,
        // 获取表单数据钩子
        onInfo: Function,
        // 表单提交钩子
        onSubmit: Function
    },
 
    emits: ["opened", "closed"],
 
    setup(props, { slots, expose }) {
        const { crud } = useCore();
 
        const config = reactive<ClUpsert.Config>(
            mergeConfig(props, inject("useUpsert__options") || {})
        );
 
        // el-form
        const Form = ref<ClForm.Ref>();
 
        // 模式
        const mode = ref<ClUpsert.Ref["mode"]>("info");
 
        // 关闭表单
        function close(action?: ClForm.CloseAction) {
            Form.value?.close(action);
        }
 
        // 关闭后
        function onClosed() {
            Form.value?.hideLoading();
 
            if (config.onClosed) {
                config.onClosed();
            }
        }
 
        // 关闭前
        function beforeClose(action: ClForm.CloseAction, done: fn) {
            function next() {
                done();
                onClosed();
            }
 
            if (config.onClose) {
                config.onClose(action, next);
            } else {
                next();
            }
        }
 
        // 提交
        function submit(data: obj) {
            const { service, dict, refresh } = crud;
 
            function done() {
                Form.value?.done();
            }
 
            function next(data: obj) {
                return new Promise((resolve, reject) => {
                    // 发送请求
                    service[dict.api[mode.value]](data)
                        .then((res) => {
                            ElMessage.success(dict.label.saveSuccess);
                            done();
                            close("save");
                            refresh();
                            resolve(res);
                        })
                        .catch((err) => {
                            ElMessage.error(err.message);
                            done();
                            reject(err);
                        });
                });
            }
 
            // 提交钩子
            if (config.onSubmit) {
                config.onSubmit(data, {
                    done,
                    next,
                    close() {
                        close("save");
                    }
                });
            } else {
                next(data);
            }
        }
 
        // 打开表单
        function open() {
            // 是否禁用
            const isDisabled = mode.value == "info";
 
            return new Promise((resolve) => {
                if (!Form.value) {
                    return console.error("<cl-upsert /> is not found");
                }
 
                Form.value?.open(
                    {
                        title: crud.dict.label[mode.value],
                        props: {
                            ...config.props,
                            disabled: isDisabled
                        },
                        op: {
                            ...config.op,
                            hidden: isDisabled
                        },
                        dialog: config.dialog,
                        items: config.items || [],
                        on: {
                            open() {
                                if (config.onOpen) {
                                    config.onOpen();
                                }
 
                                resolve(true);
                            },
                            submit,
                            close: beforeClose
                        },
                        form: {},
                        _data: {
                            isDisabled
                        }
                    },
                    config.plugins
                );
            });
        }
 
        // 打开后事件
        function onOpened() {
            const data = Form.value?.getForm();
 
            if (config.onOpened) {
                config.onOpened(data);
            }
        }
 
        // 新增
        async function add() {
            mode.value = "add";
 
            // 打开中
            await open();
 
            // 打开后
            onOpened();
        }
 
        // 追加
        async function append(data: any) {
            mode.value = "add";
 
            // 打开中
            await open();
 
            // 绑定值
            if (data) {
                Form.value?.bindForm(data);
            }
 
            // 打开后
            onOpened();
        }
 
        // 编辑
        function edit(data?: any) {
            mode.value = "update";
            getInfo(data);
        }
 
        // 详情
        function info(data?: any) {
            mode.value = "info";
            getInfo(data);
        }
 
        // 信息
        function getInfo(data: any) {
            // 显示加载中
            Form.value?.showLoading();
 
            // 是否同步打开
            if (!config.sync) {
                open();
            }
 
            // 完成
            async function done(data?: any) {
                // 加载完成
                Form.value?.hideLoading();
 
                // 合并数据
                if (data) {
                    Form.value?.bindForm(data);
                }
 
                // 同步打开表单
                if (config.sync) {
                    await open();
                }
 
                onOpened();
            }
 
            // 获取详情
            function next(data: any): Promise<any> {
                return new Promise(async (resolve, reject) => {
                    // 发送请求
                    await crud.service[crud.dict.api.info]({
                        [crud.dict.primaryId]: data[crud.dict.primaryId]
                    })
                        .then((res) => {
                            done(res);
                            resolve(res);
                        })
                        .catch((err) => {
                            ElMessage.error(err.message);
                            reject(err);
                        });
 
                    // 隐藏加载框
                    Form.value?.hideLoading();
                });
            }
 
            // 详情钩子
            if (config.onInfo) {
                config.onInfo(data, {
                    close,
                    next,
                    done
                });
            } else {
                next(data);
            }
        }
 
        // 完成
        function done() {
            Form.value?.hideLoading();
        }
 
        const ctx = {
            config,
            ...toRefs(config),
            ...useApi({ Form }),
            Form,
            get form() {
                return Form.value?.form || {};
            },
            mode,
            add,
            append,
            edit,
            info,
            open,
            close,
            done,
            submit
        };
 
        useProxy(ctx);
        expose(ctx);
 
        return () => {
            return <div class="cl-upsert">{h(<cl-form ref={Form} />, {}, slots)}</div>;
        };
    }
});