@ebiz/designer-components 0.0.36 → 0.0.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/designer-components.css +1 -1
- package/dist/index.mjs +8270 -7767
- package/package.json +1 -1
- package/src/components/senior/EbizSDialog/index.vue +900 -0
- package/src/index.js +3 -1
- package/src/router/index.js +6 -0
- package/src/views/EbizSDataDemo.vue +2 -1
- package/src/views/EbizSDialogDemo.vue +302 -0
- package/src/views/Home.vue +2 -1
@@ -0,0 +1,900 @@
|
|
1
|
+
<template>
|
2
|
+
<div class="ebiz-tdesign-button-dialog">
|
3
|
+
<t-dialog v-model:visible="dialogVisible" :header="title || computedTitle" :width="dialogWidth" :top="dialogTop"
|
4
|
+
:attach="dialogAttach" :destroy-on-close="dialogDestroyOnClose" :mode="dialogMode" :placement="dialogPlacement"
|
5
|
+
:show-overlay="dialogShowOverlay" :close-on-esc-keydown="dialogCloseOnEscKeydown"
|
6
|
+
:close-on-overlay-click="dialogCloseOnOverlayClick" :show-footer="dialogShowFooter" @close="handleDialogClose"
|
7
|
+
@confirm="handleDialogConfirm" @cancel="handleDialogCancel">
|
8
|
+
<template #header>
|
9
|
+
<slot name="header">
|
10
|
+
<div :class="['dialog-header', dialogType === 'delete' ? 'dialog-header-delete' : '']">
|
11
|
+
{{ title || computedTitle }}
|
12
|
+
</div>
|
13
|
+
</slot>
|
14
|
+
</template>
|
15
|
+
|
16
|
+
<template #body>
|
17
|
+
<div v-if="dialogType === 'normal'">
|
18
|
+
<slot name="body"></slot>
|
19
|
+
</div>
|
20
|
+
<div v-else-if="dialogType === 'add'">
|
21
|
+
<t-form ref="formRef" :colon="colon"
|
22
|
+
:data="localFormData" :disabled="disabled" :label-align="labelAlign" :label-width="labelWidth" :layout="layout"
|
23
|
+
:reset-type="resetType" :reset-on-semi-controlled="resetOnSemiControlled" :rules="rules"
|
24
|
+
:show-error-message="showErrorMessage" :status-icon="statusIcon"
|
25
|
+
:style="customStyle" @reset="handleReset" @submit="handleSubmit" @validate="handleValidate">
|
26
|
+
<slot name="body"></slot>
|
27
|
+
</t-form>
|
28
|
+
</div>
|
29
|
+
<div v-else-if="dialogType === 'edit'">
|
30
|
+
<t-form ref="formRef" :colon="colon"
|
31
|
+
:data="localFormData" :disabled="disabled" :label-align="labelAlign" :label-width="labelWidth" :layout="layout"
|
32
|
+
:reset-type="resetType" :reset-on-semi-controlled="resetOnSemiControlled" :rules="rules"
|
33
|
+
:show-error-message="showErrorMessage" :status-icon="statusIcon"
|
34
|
+
:style="customStyle" @reset="handleReset" @submit="handleSubmit" @validate="handleValidate">
|
35
|
+
<slot name="body"></slot>
|
36
|
+
</t-form>
|
37
|
+
</div>
|
38
|
+
<slot v-else-if="dialogType === 'delete'" name="body">
|
39
|
+
<div class="delete-confirm-content">
|
40
|
+
<t-icon name="error-circle" size="large" class="delete-icon" />
|
41
|
+
<p class="delete-message">{{ deleteConfirmMessage }}</p>
|
42
|
+
</div>
|
43
|
+
</slot>
|
44
|
+
</template>
|
45
|
+
</t-dialog>
|
46
|
+
</div>
|
47
|
+
</template>
|
48
|
+
|
49
|
+
<script setup>
|
50
|
+
import { ref, computed, watch } from 'vue'
|
51
|
+
import { Dialog as TDialog, MessagePlugin, Icon as TIcon } from 'tdesign-vue-next'
|
52
|
+
import dataService from '../../../apiService/simpleDataService'
|
53
|
+
|
54
|
+
const props = defineProps({
|
55
|
+
// 弹窗类型
|
56
|
+
dialogType: {
|
57
|
+
type: String,
|
58
|
+
default: 'normal', // 'normal', 'add', 'delete'
|
59
|
+
validator: (value) => ['normal', 'add', 'edit', 'delete'].includes(value)
|
60
|
+
},
|
61
|
+
|
62
|
+
// 删除确认消息
|
63
|
+
deleteConfirmMessage: {
|
64
|
+
type: String,
|
65
|
+
default: '确定要删除该数据吗?此操作不可逆,请谨慎操作。'
|
66
|
+
},
|
67
|
+
|
68
|
+
// 按钮基础属性
|
69
|
+
buttonText: {
|
70
|
+
type: String,
|
71
|
+
default: ''
|
72
|
+
},
|
73
|
+
// 按钮样式属性
|
74
|
+
buttonTheme: {
|
75
|
+
type: String,
|
76
|
+
default: ''
|
77
|
+
},
|
78
|
+
buttonVariant: {
|
79
|
+
type: String,
|
80
|
+
default: 'base'
|
81
|
+
},
|
82
|
+
buttonSize: {
|
83
|
+
type: String,
|
84
|
+
default: 'medium'
|
85
|
+
},
|
86
|
+
buttonBlock: {
|
87
|
+
type: Boolean,
|
88
|
+
default: false
|
89
|
+
},
|
90
|
+
buttonShape: {
|
91
|
+
type: String,
|
92
|
+
default: 'rectangle'
|
93
|
+
},
|
94
|
+
buttonDisabled: {
|
95
|
+
type: Boolean,
|
96
|
+
default: false
|
97
|
+
},
|
98
|
+
buttonLoading: {
|
99
|
+
type: Boolean,
|
100
|
+
default: false
|
101
|
+
},
|
102
|
+
buttonIcon: {
|
103
|
+
type: String,
|
104
|
+
default: ''
|
105
|
+
},
|
106
|
+
|
107
|
+
// 弹窗基础属性
|
108
|
+
title: {
|
109
|
+
type: String,
|
110
|
+
default: ''
|
111
|
+
},
|
112
|
+
visible: {
|
113
|
+
type: Boolean,
|
114
|
+
default: false
|
115
|
+
},
|
116
|
+
confirmText: {
|
117
|
+
type: String,
|
118
|
+
default: undefined
|
119
|
+
},
|
120
|
+
cancelText: {
|
121
|
+
type: String,
|
122
|
+
default: undefined
|
123
|
+
},
|
124
|
+
|
125
|
+
// 弹窗样式和行为属性
|
126
|
+
dialogWidth: {
|
127
|
+
type: [String, Number],
|
128
|
+
default: '500px'
|
129
|
+
},
|
130
|
+
dialogTop: {
|
131
|
+
type: [String, Number],
|
132
|
+
default: '15vh'
|
133
|
+
},
|
134
|
+
dialogAttach: {
|
135
|
+
type: [String, Function],
|
136
|
+
default: 'body'
|
137
|
+
},
|
138
|
+
dialogDestroyOnClose: {
|
139
|
+
type: Boolean,
|
140
|
+
default: false
|
141
|
+
},
|
142
|
+
dialogMode: {
|
143
|
+
type: String,
|
144
|
+
default: 'modal'
|
145
|
+
},
|
146
|
+
dialogPlacement: {
|
147
|
+
type: String,
|
148
|
+
default: 'top'
|
149
|
+
},
|
150
|
+
dialogShowOverlay: {
|
151
|
+
type: Boolean,
|
152
|
+
default: true
|
153
|
+
},
|
154
|
+
dialogCloseOnEscKeydown: {
|
155
|
+
type: Boolean,
|
156
|
+
default: true
|
157
|
+
},
|
158
|
+
dialogCloseOnOverlayClick: {
|
159
|
+
type: Boolean,
|
160
|
+
default: true
|
161
|
+
},
|
162
|
+
dialogShowFooter: {
|
163
|
+
type: Boolean,
|
164
|
+
default: true
|
165
|
+
},
|
166
|
+
|
167
|
+
// API配置
|
168
|
+
apiConfig: {
|
169
|
+
type: Object,
|
170
|
+
default: () => null
|
171
|
+
},
|
172
|
+
|
173
|
+
// 请求参数
|
174
|
+
params: {
|
175
|
+
type: Object,
|
176
|
+
default: () => null
|
177
|
+
},
|
178
|
+
|
179
|
+
// 表单数据
|
180
|
+
formData:{
|
181
|
+
type: Object,
|
182
|
+
default: () => null
|
183
|
+
},
|
184
|
+
|
185
|
+
// 表单验证规则
|
186
|
+
rules:{
|
187
|
+
type: Object,
|
188
|
+
default: () => null
|
189
|
+
},
|
190
|
+
|
191
|
+
// 是否在API调用成功后自动关闭弹窗
|
192
|
+
closeOnSuccess: {
|
193
|
+
type: Boolean,
|
194
|
+
default: true
|
195
|
+
},
|
196
|
+
// 是否显示API调用的结果消息
|
197
|
+
showResultMessage: {
|
198
|
+
type: Boolean,
|
199
|
+
default: true
|
200
|
+
},
|
201
|
+
|
202
|
+
// 表单组件属性
|
203
|
+
colon: {
|
204
|
+
type: Boolean,
|
205
|
+
default: false
|
206
|
+
},
|
207
|
+
disabled: {
|
208
|
+
type: Boolean,
|
209
|
+
default: false
|
210
|
+
},
|
211
|
+
labelAlign: {
|
212
|
+
type: String,
|
213
|
+
default: 'right'
|
214
|
+
},
|
215
|
+
labelWidth: {
|
216
|
+
type: [String, Number],
|
217
|
+
default: '100px'
|
218
|
+
},
|
219
|
+
layout: {
|
220
|
+
type: String,
|
221
|
+
default: 'vertical'
|
222
|
+
},
|
223
|
+
resetType: {
|
224
|
+
type: String,
|
225
|
+
default: 'empty'
|
226
|
+
},
|
227
|
+
resetOnSemiControlled: {
|
228
|
+
type: Boolean,
|
229
|
+
default: false
|
230
|
+
},
|
231
|
+
showErrorMessage: {
|
232
|
+
type: Boolean,
|
233
|
+
default: true
|
234
|
+
},
|
235
|
+
statusIcon: {
|
236
|
+
type: Boolean,
|
237
|
+
default: false
|
238
|
+
},
|
239
|
+
customStyle: {
|
240
|
+
type: Object,
|
241
|
+
default: () => ({})
|
242
|
+
},
|
243
|
+
|
244
|
+
// 详情API配置,用于编辑模式加载数据
|
245
|
+
detailApiConfig: {
|
246
|
+
type: Object,
|
247
|
+
default: () => null
|
248
|
+
},
|
249
|
+
})
|
250
|
+
|
251
|
+
const emit = defineEmits([
|
252
|
+
'update:modelValue',
|
253
|
+
'button-click',
|
254
|
+
'dialog-close',
|
255
|
+
'dialog-confirm',
|
256
|
+
'dialog-cancel',
|
257
|
+
'api-success',
|
258
|
+
'api-error',
|
259
|
+
'reset',
|
260
|
+
'submit',
|
261
|
+
'submit-error',
|
262
|
+
'validate',
|
263
|
+
'detail-loaded',
|
264
|
+
'detail-error'
|
265
|
+
])
|
266
|
+
|
267
|
+
const computedTitle = computed(() => {
|
268
|
+
switch (props.dialogType) {
|
269
|
+
case 'add': return '添加数据';
|
270
|
+
case 'delete': return '删除确认';
|
271
|
+
case 'edit': return '编辑数据';
|
272
|
+
default: return '标题';
|
273
|
+
}
|
274
|
+
})
|
275
|
+
|
276
|
+
// 初始化dialogVisible
|
277
|
+
const dialogVisible = ref(props.visible)
|
278
|
+
const apiLoading = ref(false)
|
279
|
+
const formRef = ref(null)
|
280
|
+
const currentId = ref(null)
|
281
|
+
const currentParams = ref({})
|
282
|
+
// 本地维护一份表单数据的副本
|
283
|
+
const localFormData = ref(props.formData ? { ...props.formData } : {})
|
284
|
+
|
285
|
+
// 监听props的visible变化
|
286
|
+
watch(() => props.visible, (newVal) => {
|
287
|
+
dialogVisible.value = newVal
|
288
|
+
})
|
289
|
+
|
290
|
+
// 监听dialogVisible变化,同步更新modelValue
|
291
|
+
watch(dialogVisible, (newVal) => {
|
292
|
+
emit('update:modelValue', newVal)
|
293
|
+
})
|
294
|
+
|
295
|
+
// 弹窗关闭事件
|
296
|
+
const handleDialogClose = (e) => {
|
297
|
+
dialogVisible.value = false
|
298
|
+
emit('dialog-close', e)
|
299
|
+
}
|
300
|
+
|
301
|
+
// API调用方法
|
302
|
+
// 获取错误消息
|
303
|
+
const getErrorMessage = (error, dialogType) => {
|
304
|
+
const errorPrefix = dialogType === 'delete'
|
305
|
+
? '删除失败'
|
306
|
+
: dialogType === 'add'
|
307
|
+
? '添加失败'
|
308
|
+
: dialogType === 'edit'
|
309
|
+
? '修改失败'
|
310
|
+
: '操作失败'
|
311
|
+
|
312
|
+
if (!error) return `${errorPrefix}: 未知错误`
|
313
|
+
|
314
|
+
// 处理不同类型的错误
|
315
|
+
if (error.response && error.response.data) {
|
316
|
+
// API返回的详细错误信息
|
317
|
+
const { message, msg, error: errMsg } = error.response.data
|
318
|
+
return `${errorPrefix}: ${message || msg || errMsg || error.message || '服务器返回错误'}`
|
319
|
+
} else if (error.message) {
|
320
|
+
// 标准错误对象
|
321
|
+
return `${errorPrefix}: ${error.message}`
|
322
|
+
} else if (typeof error === 'string') {
|
323
|
+
// 字符串错误
|
324
|
+
return `${errorPrefix}: ${error}`
|
325
|
+
} else {
|
326
|
+
// 未知错误
|
327
|
+
return `${errorPrefix}: 未知错误`
|
328
|
+
}
|
329
|
+
}
|
330
|
+
|
331
|
+
const callApi = async (config = props.apiConfig) => {
|
332
|
+
if (!config) return null
|
333
|
+
|
334
|
+
try {
|
335
|
+
apiLoading.value = true
|
336
|
+
|
337
|
+
// 克隆配置以避免修改原始对象
|
338
|
+
const apiConfig = { ...config }
|
339
|
+
|
340
|
+
// 对于添加和编辑表单,自动附加表单数据
|
341
|
+
if ((props.dialogType === 'add' || props.dialogType === 'edit') && localFormData.value) {
|
342
|
+
// 如果没有data字段,创建一个
|
343
|
+
if (!apiConfig.data) {
|
344
|
+
apiConfig.data = { ...localFormData.value }
|
345
|
+
}
|
346
|
+
// 如果有data字段且是对象,合并表单数据
|
347
|
+
else if (typeof apiConfig.data === 'object' && apiConfig.data !== null) {
|
348
|
+
// 深拷贝表单数据,避免引用问题
|
349
|
+
const formDataCopy = {}
|
350
|
+
Object.keys(localFormData.value).forEach(key => {
|
351
|
+
// 处理表单中的引用类型数据
|
352
|
+
if (typeof localFormData.value[key] === 'object' && localFormData.value[key] !== null) {
|
353
|
+
formDataCopy[key] = Array.isArray(localFormData.value[key])
|
354
|
+
? [...localFormData.value[key]]
|
355
|
+
: { ...localFormData.value[key] }
|
356
|
+
} else {
|
357
|
+
formDataCopy[key] = localFormData.value[key]
|
358
|
+
}
|
359
|
+
})
|
360
|
+
|
361
|
+
apiConfig.data = { ...apiConfig.data, ...formDataCopy }
|
362
|
+
}
|
363
|
+
}
|
364
|
+
|
365
|
+
// 添加默认的content-type头
|
366
|
+
if (!apiConfig.headers) {
|
367
|
+
apiConfig.headers = {}
|
368
|
+
}
|
369
|
+
|
370
|
+
if (!apiConfig.headers['Content-Type']) {
|
371
|
+
apiConfig.headers['Content-Type'] = 'application/json'
|
372
|
+
}
|
373
|
+
|
374
|
+
// 设置默认的请求方法
|
375
|
+
if (!apiConfig.method) {
|
376
|
+
// 根据弹窗类型设置默认方法
|
377
|
+
switch (props.dialogType) {
|
378
|
+
case 'add':
|
379
|
+
apiConfig.method = 'post'
|
380
|
+
break
|
381
|
+
case 'edit':
|
382
|
+
apiConfig.method = 'put'
|
383
|
+
break
|
384
|
+
case 'delete':
|
385
|
+
apiConfig.method = 'delete'
|
386
|
+
break
|
387
|
+
default:
|
388
|
+
apiConfig.method = 'post'
|
389
|
+
}
|
390
|
+
}
|
391
|
+
|
392
|
+
// 如果是URL形式的API配置
|
393
|
+
if (typeof apiConfig === 'string') {
|
394
|
+
apiConfig = { url: apiConfig, method: 'post' }
|
395
|
+
}
|
396
|
+
|
397
|
+
// 添加自定义请求拦截器
|
398
|
+
let requestTransformer = null
|
399
|
+
if (apiConfig.transformRequest) {
|
400
|
+
requestTransformer = apiConfig.transformRequest
|
401
|
+
delete apiConfig.transformRequest
|
402
|
+
}
|
403
|
+
|
404
|
+
// 添加自定义响应拦截器
|
405
|
+
let responseTransformer = null
|
406
|
+
if (apiConfig.transformResponse) {
|
407
|
+
responseTransformer = apiConfig.transformResponse
|
408
|
+
delete apiConfig.transformResponse
|
409
|
+
}
|
410
|
+
|
411
|
+
// 处理请求数据
|
412
|
+
if (requestTransformer && typeof requestTransformer === 'function') {
|
413
|
+
apiConfig.data = requestTransformer(apiConfig.data)
|
414
|
+
}
|
415
|
+
|
416
|
+
// 调用dataService进行API请求
|
417
|
+
const response = await dataService.fetch({}, apiConfig)
|
418
|
+
|
419
|
+
// 处理响应数据
|
420
|
+
let transformedResponse = response
|
421
|
+
if (responseTransformer && typeof responseTransformer === 'function') {
|
422
|
+
transformedResponse = responseTransformer(response)
|
423
|
+
}
|
424
|
+
|
425
|
+
// 显示成功消息
|
426
|
+
if (props.showResultMessage) {
|
427
|
+
const successMessage = props.dialogType === 'delete'
|
428
|
+
? '删除成功'
|
429
|
+
: props.dialogType === 'add'
|
430
|
+
? '添加成功'
|
431
|
+
: props.dialogType === 'edit'
|
432
|
+
? '修改成功'
|
433
|
+
: '操作成功'
|
434
|
+
MessagePlugin.success(successMessage)
|
435
|
+
}
|
436
|
+
|
437
|
+
// 触发成功事件
|
438
|
+
emit('api-success', transformedResponse)
|
439
|
+
|
440
|
+
// 如果设置了成功后关闭,则关闭弹窗
|
441
|
+
if (props.closeOnSuccess) {
|
442
|
+
dialogVisible.value = false
|
443
|
+
}
|
444
|
+
|
445
|
+
return transformedResponse
|
446
|
+
} catch (error) {
|
447
|
+
// 错误处理和错误消息展示
|
448
|
+
const errorMessage = getErrorMessage(error, props.dialogType)
|
449
|
+
|
450
|
+
// 显示错误消息
|
451
|
+
if (props.showResultMessage) {
|
452
|
+
MessagePlugin.error(errorMessage)
|
453
|
+
}
|
454
|
+
|
455
|
+
// 触发错误事件
|
456
|
+
emit('api-error', error)
|
457
|
+
|
458
|
+
throw error // 继续抛出错误以便上层捕获
|
459
|
+
} finally {
|
460
|
+
apiLoading.value = false
|
461
|
+
}
|
462
|
+
}
|
463
|
+
|
464
|
+
// 弹窗确认事件
|
465
|
+
const handleDialogConfirm = async (e) => {
|
466
|
+
emit('dialog-confirm', e)
|
467
|
+
|
468
|
+
// 对于非普通类型的弹窗,总是尝试调用API
|
469
|
+
if (props.dialogType !== 'normal') {
|
470
|
+
if (props.apiConfig) {
|
471
|
+
|
472
|
+
// 如果是表单类型(添加或编辑),先进行表单校验
|
473
|
+
if ((props.dialogType === 'add' || props.dialogType === 'edit') && formRef.value) {
|
474
|
+
|
475
|
+
try {
|
476
|
+
// 显示加载状态
|
477
|
+
apiLoading.value = true
|
478
|
+
|
479
|
+
// 使用表单的validate方法进行校验
|
480
|
+
const validateResult = await formRef.value.validate()
|
481
|
+
|
482
|
+
// 校验成功,执行提交操作
|
483
|
+
if (validateResult === true) {
|
484
|
+
// 克隆API配置,避免修改原始配置
|
485
|
+
const apiConfig = { ...props.apiConfig }
|
486
|
+
|
487
|
+
// 对于编辑模式,确保请求中包含ID
|
488
|
+
if (props.dialogType === 'edit' && currentId.value) {
|
489
|
+
// 替换URL中的ID参数,支持{id}和:id两种格式
|
490
|
+
if (apiConfig.url) {
|
491
|
+
apiConfig.url = apiConfig.url.replace(/\{id\}/g, currentId.value).replace(/:id/g, currentId.value)
|
492
|
+
}
|
493
|
+
|
494
|
+
// 准备请求参数
|
495
|
+
if (!apiConfig.params) {
|
496
|
+
apiConfig.params = {}
|
497
|
+
}
|
498
|
+
|
499
|
+
// 如果有params参数且没有id,添加id
|
500
|
+
if (!apiConfig.params.id && currentId.value) {
|
501
|
+
apiConfig.params.id = currentId.value
|
502
|
+
}
|
503
|
+
|
504
|
+
// 准备请求数据
|
505
|
+
if (!apiConfig.data) {
|
506
|
+
apiConfig.data = {}
|
507
|
+
}
|
508
|
+
|
509
|
+
// 确保请求体中有ID
|
510
|
+
if (!apiConfig.data.id && currentId.value) {
|
511
|
+
apiConfig.data.id = currentId.value
|
512
|
+
}
|
513
|
+
|
514
|
+
// 合并当前参数中的非表单数据
|
515
|
+
if (currentParams.value && Object.keys(currentParams.value).length > 0) {
|
516
|
+
// 过滤掉id和formData,避免重复
|
517
|
+
const { id: _id, formData: _formData, ...otherParams } = currentParams.value
|
518
|
+
apiConfig.params = { ...apiConfig.params, ...otherParams }
|
519
|
+
}
|
520
|
+
|
521
|
+
await callApi(apiConfig)
|
522
|
+
} else {
|
523
|
+
// 克隆API配置,确保添加操作有正确的data字段
|
524
|
+
const addApiConfig = { ...props.apiConfig }
|
525
|
+
|
526
|
+
// 合并当前参数中的非表单数据
|
527
|
+
if (currentParams.value && Object.keys(currentParams.value).length > 0) {
|
528
|
+
// 过滤掉id和formData,避免重复
|
529
|
+
const { id: _id, formData: _formData, ...otherParams } = currentParams.value
|
530
|
+
|
531
|
+
if (!addApiConfig.params) {
|
532
|
+
addApiConfig.params = {}
|
533
|
+
}
|
534
|
+
|
535
|
+
addApiConfig.params = { ...addApiConfig.params, ...otherParams }
|
536
|
+
}
|
537
|
+
|
538
|
+
await callApi(addApiConfig)
|
539
|
+
}
|
540
|
+
} else {
|
541
|
+
// 校验失败,显示第一个错误信息
|
542
|
+
const firstError = validateResult[Object.keys(validateResult)[0]]?.[0]?.message || '表单校验失败'
|
543
|
+
MessagePlugin.error(firstError)
|
544
|
+
emit('submit-error', { validateResult, firstError })
|
545
|
+
return // 阻止关闭弹窗
|
546
|
+
}
|
547
|
+
} catch (error) {
|
548
|
+
// 校验或API调用过程中的错误
|
549
|
+
MessagePlugin.error(`操作失败: ${error.message || '未知错误'}`)
|
550
|
+
emit('submit-error', { error })
|
551
|
+
return // 阻止关闭弹窗
|
552
|
+
} finally {
|
553
|
+
apiLoading.value = false
|
554
|
+
}
|
555
|
+
} else if (props.dialogType === 'delete') {
|
556
|
+
try {
|
557
|
+
// 显示加载状态
|
558
|
+
apiLoading.value = true
|
559
|
+
|
560
|
+
// 克隆API配置,避免修改原始配置
|
561
|
+
const deleteApiConfig = { ...props.apiConfig }
|
562
|
+
|
563
|
+
// 对于删除操作,确保包含ID
|
564
|
+
if (currentId.value) {
|
565
|
+
// 替换URL中的ID参数
|
566
|
+
if (deleteApiConfig.url) {
|
567
|
+
deleteApiConfig.url = deleteApiConfig.url.replace(/\{id\}/g, currentId.value).replace(/:id/g, currentId.value)
|
568
|
+
}
|
569
|
+
|
570
|
+
// 准备请求参数
|
571
|
+
if (!deleteApiConfig.params) {
|
572
|
+
deleteApiConfig.params = {}
|
573
|
+
}
|
574
|
+
|
575
|
+
// 如果有params参数且没有id,添加id
|
576
|
+
if (!deleteApiConfig.params.id && currentId.value) {
|
577
|
+
deleteApiConfig.params.id = currentId.value
|
578
|
+
}
|
579
|
+
|
580
|
+
// 准备请求数据
|
581
|
+
if (!deleteApiConfig.data) {
|
582
|
+
deleteApiConfig.data = {}
|
583
|
+
}
|
584
|
+
|
585
|
+
// 确保请求体中有ID
|
586
|
+
if (!deleteApiConfig.data.id && currentId.value) {
|
587
|
+
deleteApiConfig.data.id = currentId.value
|
588
|
+
}
|
589
|
+
}
|
590
|
+
|
591
|
+
// 合并当前参数
|
592
|
+
if (currentParams.value && Object.keys(currentParams.value).length > 0) {
|
593
|
+
// 过滤掉id和formData,避免重复
|
594
|
+
const { id: _id, formData: _formData, ...otherParams } = currentParams.value
|
595
|
+
|
596
|
+
if (!deleteApiConfig.params) {
|
597
|
+
deleteApiConfig.params = {}
|
598
|
+
}
|
599
|
+
|
600
|
+
deleteApiConfig.params = { ...deleteApiConfig.params, ...otherParams }
|
601
|
+
}
|
602
|
+
|
603
|
+
// 删除操作直接调用API
|
604
|
+
await callApi(deleteApiConfig)
|
605
|
+
} catch (error) {
|
606
|
+
// API调用过程中的错误
|
607
|
+
MessagePlugin.error(`删除失败: ${error.message || '未知错误'}`)
|
608
|
+
emit('api-error', error)
|
609
|
+
return // 阻止关闭弹窗
|
610
|
+
} finally {
|
611
|
+
apiLoading.value = false
|
612
|
+
}
|
613
|
+
}
|
614
|
+
} else {
|
615
|
+
dialogVisible.value = false
|
616
|
+
}
|
617
|
+
} else {
|
618
|
+
// 普通弹窗的行为
|
619
|
+
if (props.apiConfig) {
|
620
|
+
try {
|
621
|
+
// 显示加载状态
|
622
|
+
apiLoading.value = true
|
623
|
+
|
624
|
+
// 克隆API配置,避免修改原始配置
|
625
|
+
const normalApiConfig = { ...props.apiConfig }
|
626
|
+
|
627
|
+
// 合并当前参数
|
628
|
+
if (currentParams.value && Object.keys(currentParams.value).length > 0) {
|
629
|
+
if (!normalApiConfig.params) {
|
630
|
+
normalApiConfig.params = {}
|
631
|
+
}
|
632
|
+
|
633
|
+
normalApiConfig.params = { ...normalApiConfig.params, ...currentParams.value }
|
634
|
+
}
|
635
|
+
|
636
|
+
await callApi(normalApiConfig)
|
637
|
+
} catch (error) {
|
638
|
+
// API调用过程中的错误
|
639
|
+
MessagePlugin.error(`操作失败: ${error.message || '未知错误'}`)
|
640
|
+
emit('api-error', error)
|
641
|
+
return // 阻止关闭弹窗
|
642
|
+
} finally {
|
643
|
+
apiLoading.value = false
|
644
|
+
}
|
645
|
+
} else {
|
646
|
+
dialogVisible.value = false
|
647
|
+
}
|
648
|
+
}
|
649
|
+
}
|
650
|
+
|
651
|
+
// 弹窗取消事件
|
652
|
+
const handleDialogCancel = (e) => {
|
653
|
+
dialogVisible.value = false
|
654
|
+
emit('dialog-cancel', e)
|
655
|
+
}
|
656
|
+
|
657
|
+
// 表单重置事件
|
658
|
+
const handleReset = (e) => {
|
659
|
+
emit('reset', e)
|
660
|
+
}
|
661
|
+
|
662
|
+
// 表单提交事件
|
663
|
+
const handleSubmit = async ({ validateResult, firstError, e }) => {
|
664
|
+
if (validateResult === true) {
|
665
|
+
emit('submit', { e, formData: localFormData.value })
|
666
|
+
} else {
|
667
|
+
MessagePlugin.error(firstError)
|
668
|
+
emit('submit-error', { validateResult, firstError })
|
669
|
+
}
|
670
|
+
}
|
671
|
+
|
672
|
+
// 表单验证事件
|
673
|
+
const handleValidate = (params) => {
|
674
|
+
emit('validate', params)
|
675
|
+
}
|
676
|
+
|
677
|
+
// 加载详情数据
|
678
|
+
const loadDetailData = async (id) => {
|
679
|
+
if (!props.detailApiConfig) return null
|
680
|
+
|
681
|
+
try {
|
682
|
+
// 显示加载状态
|
683
|
+
apiLoading.value = true
|
684
|
+
|
685
|
+
// 构建详情请求配置
|
686
|
+
const requestConfig = {
|
687
|
+
...props.detailApiConfig
|
688
|
+
}
|
689
|
+
|
690
|
+
// 替换URL中的ID参数,支持{id}和:id两种格式
|
691
|
+
if (requestConfig.url) {
|
692
|
+
requestConfig.url = requestConfig.url.replace(/\{id\}/g, id).replace(/:id/g, id)
|
693
|
+
}
|
694
|
+
|
695
|
+
// 处理请求参数
|
696
|
+
if (!requestConfig.params) {
|
697
|
+
requestConfig.params = {}
|
698
|
+
}
|
699
|
+
|
700
|
+
// 如果有params参数且没有id,添加id
|
701
|
+
if (!requestConfig.params.id && id) {
|
702
|
+
requestConfig.params.id = id
|
703
|
+
}
|
704
|
+
|
705
|
+
// 合并当前参数
|
706
|
+
if (currentParams.value && Object.keys(currentParams.value).length > 0) {
|
707
|
+
// 过滤掉id和formData,避免重复
|
708
|
+
const { id: _id, formData: _formData, ...otherParams } = currentParams.value
|
709
|
+
requestConfig.params = { ...requestConfig.params, ...otherParams }
|
710
|
+
}
|
711
|
+
|
712
|
+
// 调用dataService进行API请求
|
713
|
+
const response = await dataService.fetch({}, requestConfig)
|
714
|
+
|
715
|
+
// 获取详情数据
|
716
|
+
const detailData = response.data || response
|
717
|
+
|
718
|
+
// 更新本地表单数据
|
719
|
+
if (detailData) {
|
720
|
+
// 清空现有数据
|
721
|
+
Object.keys(localFormData.value).forEach(key => {
|
722
|
+
delete localFormData.value[key]
|
723
|
+
})
|
724
|
+
|
725
|
+
// 写入新数据
|
726
|
+
Object.keys(detailData).forEach(key => {
|
727
|
+
localFormData.value[key] = detailData[key]
|
728
|
+
})
|
729
|
+
}
|
730
|
+
|
731
|
+
// 触发加载成功事件
|
732
|
+
emit('detail-loaded', detailData)
|
733
|
+
return detailData
|
734
|
+
} catch (error) {
|
735
|
+
// 显示错误消息
|
736
|
+
if (props.showResultMessage) {
|
737
|
+
MessagePlugin.error(`加载详情失败: ${error.message || '未知错误'}`)
|
738
|
+
}
|
739
|
+
|
740
|
+
// 触发错误事件
|
741
|
+
emit('detail-error', error)
|
742
|
+
return null
|
743
|
+
} finally {
|
744
|
+
apiLoading.value = false
|
745
|
+
}
|
746
|
+
}
|
747
|
+
|
748
|
+
// 打开弹窗并传递参数
|
749
|
+
const openDialog = (params = {}) => {
|
750
|
+
// 重置状态
|
751
|
+
if (props.dialogType === 'add') {
|
752
|
+
// 添加模式:清空表单数据
|
753
|
+
Object.keys(localFormData.value).forEach(key => {
|
754
|
+
delete localFormData.value[key]
|
755
|
+
})
|
756
|
+
}
|
757
|
+
|
758
|
+
// 处理传入的表单数据
|
759
|
+
if (params.formData && (props.dialogType === 'add' || props.dialogType === 'edit')) {
|
760
|
+
// 更新本地表单数据副本
|
761
|
+
Object.keys(params.formData).forEach(key => {
|
762
|
+
localFormData.value[key] = params.formData[key]
|
763
|
+
})
|
764
|
+
}
|
765
|
+
|
766
|
+
// 存储其他参数,如删除ID等
|
767
|
+
if (params.id) {
|
768
|
+
currentId.value = params.id
|
769
|
+
|
770
|
+
// 如果是编辑模式且有detailApiConfig,加载详情数据
|
771
|
+
if (props.dialogType === 'edit' && props.detailApiConfig) {
|
772
|
+
loadDetailData(params.id)
|
773
|
+
}
|
774
|
+
} else {
|
775
|
+
currentId.value = null
|
776
|
+
}
|
777
|
+
|
778
|
+
// 存储其他自定义参数
|
779
|
+
currentParams.value = params
|
780
|
+
|
781
|
+
// 打开弹窗
|
782
|
+
dialogVisible.value = true
|
783
|
+
|
784
|
+
return { success: true, message: '弹窗已打开' }
|
785
|
+
}
|
786
|
+
|
787
|
+
// 关闭弹窗
|
788
|
+
const closeDialog = () => {
|
789
|
+
dialogVisible.value = false
|
790
|
+
return { success: true, message: '弹窗已关闭' }
|
791
|
+
}
|
792
|
+
|
793
|
+
// 重置表单数据到初始状态
|
794
|
+
const resetFormData = () => {
|
795
|
+
if (formRef.value) {
|
796
|
+
formRef.value.reset()
|
797
|
+
emit('reset', { type: 'manual' })
|
798
|
+
}
|
799
|
+
|
800
|
+
return { success: true, message: '表单已重置' }
|
801
|
+
}
|
802
|
+
|
803
|
+
// 清空表单数据
|
804
|
+
const clearFormData = () => {
|
805
|
+
// 清空表单数据
|
806
|
+
Object.keys(localFormData.value).forEach(key => {
|
807
|
+
delete localFormData.value[key]
|
808
|
+
})
|
809
|
+
|
810
|
+
emit('reset', { type: 'clear' })
|
811
|
+
return { success: true, message: '表单数据已清空' }
|
812
|
+
}
|
813
|
+
|
814
|
+
// 设置表单数据
|
815
|
+
const setFormData = (data) => {
|
816
|
+
if (!data || typeof data !== 'object') {
|
817
|
+
return { success: false, message: '数据必须是一个对象' }
|
818
|
+
}
|
819
|
+
|
820
|
+
// 清空现有数据
|
821
|
+
Object.keys(localFormData.value).forEach(key => {
|
822
|
+
delete localFormData.value[key]
|
823
|
+
})
|
824
|
+
|
825
|
+
// 写入新数据
|
826
|
+
Object.keys(data).forEach(key => {
|
827
|
+
localFormData.value[key] = data[key]
|
828
|
+
})
|
829
|
+
|
830
|
+
return { success: true, message: '表单数据已设置' }
|
831
|
+
}
|
832
|
+
|
833
|
+
// 获取表单数据
|
834
|
+
const getFormData = () => {
|
835
|
+
return { ...localFormData.value }
|
836
|
+
}
|
837
|
+
|
838
|
+
// 获取表单的当前验证状态
|
839
|
+
const validateForm = async () => {
|
840
|
+
if (!formRef.value) {
|
841
|
+
return { success: false, message: '表单引用不存在' }
|
842
|
+
}
|
843
|
+
|
844
|
+
try {
|
845
|
+
const validateResult = await formRef.value.validate()
|
846
|
+
|
847
|
+
if (validateResult === true) {
|
848
|
+
return { success: true, message: '表单验证通过', data: getFormData() }
|
849
|
+
} else {
|
850
|
+
const firstError = validateResult[Object.keys(validateResult)[0]]?.[0]?.message || '表单校验失败'
|
851
|
+
return { success: false, message: firstError, errors: validateResult }
|
852
|
+
}
|
853
|
+
} catch (error) {
|
854
|
+
return { success: false, message: error.message || '表单验证过程中发生错误' }
|
855
|
+
}
|
856
|
+
}
|
857
|
+
|
858
|
+
// 暴露给父组件的方法
|
859
|
+
defineExpose({
|
860
|
+
openDialog,
|
861
|
+
closeDialog,
|
862
|
+
resetFormData,
|
863
|
+
clearFormData,
|
864
|
+
setFormData,
|
865
|
+
getFormData,
|
866
|
+
validateForm,
|
867
|
+
loadDetailData,
|
868
|
+
currentId,
|
869
|
+
currentParams,
|
870
|
+
formRef
|
871
|
+
})
|
872
|
+
</script>
|
873
|
+
|
874
|
+
<style scoped>
|
875
|
+
.ebiz-tdesign-button-dialog {
|
876
|
+
display: inline-block;
|
877
|
+
}
|
878
|
+
|
879
|
+
.dialog-header-delete {
|
880
|
+
color: var(--td-error-color, #e34d59);
|
881
|
+
}
|
882
|
+
|
883
|
+
.delete-confirm-content {
|
884
|
+
display: flex;
|
885
|
+
align-items: center;
|
886
|
+
padding: 16px;
|
887
|
+
}
|
888
|
+
|
889
|
+
.delete-icon {
|
890
|
+
font-size: 24px;
|
891
|
+
color: var(--td-error-color, #e34d59);
|
892
|
+
margin-right: 12px;
|
893
|
+
}
|
894
|
+
|
895
|
+
.delete-message {
|
896
|
+
margin: 0;
|
897
|
+
font-size: 14px;
|
898
|
+
line-height: 22px;
|
899
|
+
}
|
900
|
+
</style>
|