-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathActionRenderer.vue
More file actions
348 lines (309 loc) · 8.44 KB
/
Copy pathActionRenderer.vue
File metadata and controls
348 lines (309 loc) · 8.44 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<template>
<div class="action-renderer">
<!-- 动态渲染Action组件 -->
<component
v-if="parsedConfig"
:is="currentComponent"
:config="parsedConfig"
:visible="isVisible"
:module="module"
:extend="extend"
:api-url="apiUrl"
@submit="handleSubmit"
@cancel="handleCancel"
@close="handleClose"
@action="handleAction"
@toast="handleToast"
@reset="handleReset"
@special-action="handleSpecialActionFromChild"
/>
<!-- 错误提示 -->
<ActionShell
v-if="error"
:visible="!!error"
title="错误"
width="400"
@close="clearError"
>
<a-alert type="error" show-icon>
<template #title>{{ error.type || 'Action 解析失败' }}</template>
{{ error.message }}
</a-alert>
<a-card v-if="error.details" size="small" class="action-error-details">
<pre>{{ JSON.stringify(error.details, null, 2) }}</pre>
</a-card>
<template #footer>
<ActionFooter :show-cancel="false" ok-text="确定" @ok="clearError" />
</template>
</ActionShell>
<!-- 加载状态 -->
<ActionShell
v-if="isLoading"
:visible="isLoading"
title="处理中"
width="300"
:show-close="false"
>
<a-spin tip="正在处理,请稍候..." class="action-loading" />
</ActionShell>
</div>
</template>
<script>
import { ref, computed, watch, defineAsyncComponent } from 'vue'
import ActionShell from './shared/ActionShell.vue'
import ActionFooter from './shared/ActionFooter.vue'
import { ActionType } from './types.js'
import {
isSpecialActionConfig,
normalizeActionConfig,
validateActionConfig
} from './utils/actionConfig.js'
import { callActionEndpoint } from './utils/actionTransport.js'
import { handleActionResponse } from './utils/actionResponse.js'
import { useActionSpecialActions } from './composables/useActionSpecialActions.js'
import { showToast } from '@/stores/toast.js'
const InputAction = defineAsyncComponent(() => import('./InputAction.vue'))
const MultiInputAction = defineAsyncComponent(() => import('./MultiInputAction.vue'))
const MenuAction = defineAsyncComponent(() => import('./MenuAction.vue'))
const MsgBoxAction = defineAsyncComponent(() => import('./MsgBoxAction.vue'))
const WebViewAction = defineAsyncComponent(() => import('./WebViewAction.vue'))
const BrowserAction = defineAsyncComponent(() => import('./BrowserAction.vue'))
const HelpAction = defineAsyncComponent(() => import('./HelpAction.vue'))
export default {
name: 'ActionRenderer',
components: {
ActionShell,
ActionFooter,
InputAction,
MultiInputAction,
MenuAction,
MsgBoxAction,
WebViewAction,
BrowserAction,
HelpAction
},
props: {
actionData: {
type: [String, Object],
default: null
},
visible: {
type: Boolean,
default: true
},
autoShow: {
type: Boolean,
default: true
},
module: {
type: String,
default: ''
},
extend: {
type: [Object, String],
default: () => ({})
},
apiUrl: {
type: String,
default: ''
}
},
emits: ['action', 'close', 'error', 'success', 'special-action'],
setup(props, { emit }) {
const parsedConfig = ref(null)
const error = ref(null)
const isLoading = ref(false)
const isVisible = ref(props.visible)
const componentMap = {
[ActionType.INPUT]: 'InputAction',
[ActionType.EDIT]: 'InputAction',
[ActionType.MULTI_INPUT]: 'MultiInputAction',
[ActionType.MULTI_INPUT_X]: 'MultiInputAction',
[ActionType.MENU]: 'MenuAction',
[ActionType.SELECT]: 'MenuAction',
[ActionType.MSGBOX]: 'MsgBoxAction',
[ActionType.WEBVIEW]: 'WebViewAction',
[ActionType.BROWSER]: 'BrowserAction',
[ActionType.HELP]: 'HelpAction'
}
const currentComponent = computed(() => {
if (!parsedConfig.value) {
return null
}
return componentMap[parsedConfig.value.type] || null
})
const handleToast = (message, type = 'success') => {
showToast(message, type)
}
const handleClose = () => {
isVisible.value = false
parsedConfig.value = null
emit('close')
}
const { handleSpecialAction } = useActionSpecialActions({
emit,
close: handleClose,
toast: handleToast
})
const handleKeepAction = async (actionData) => {
await handleSpecialAction(actionData)
if (actionData?.reset) {
parsedConfig.value = null
}
}
const parseConfig = async (data) => {
try {
if (!data) {
parsedConfig.value = null
return
}
const config = normalizeActionConfig(data, {
ensureActionId: true,
actionIdPrefix: 'renderer'
})
validateActionConfig(config)
if (isSpecialActionConfig(config)) {
await handleSpecialAction(config)
return
}
parsedConfig.value = config
error.value = null
if (props.autoShow) {
isVisible.value = true
}
} catch (err) {
console.error('解析Action配置失败:', err)
error.value = err
parsedConfig.value = null
emit('error', err)
}
}
const handleResponse = async (result, submittedValue) => {
await handleActionResponse(result, {
onToast: handleToast,
onNextAction: parseConfig,
onSpecialAction: handleSpecialAction,
onKeep: handleKeepAction,
onSuccess: () => emit('success', submittedValue),
onClose: handleClose,
onError: (err) => {
error.value = err
emit('error', err)
}
})
}
const handleSubmit = async (value) => {
if (!parsedConfig.value) return
try {
isLoading.value = true
if (!props.module && !props.apiUrl) {
emit('action', parsedConfig.value.actionId, value)
emit('success', value)
handleClose()
return
}
const result = await callActionEndpoint({
module: props.module,
apiUrl: props.apiUrl,
extend: props.extend,
action: parsedConfig.value.actionId,
value
})
await handleResponse(result, value)
} catch (err) {
console.error('执行Action失败:', err)
error.value = err
emit('error', err)
showToast(err.message || '操作失败', 'error')
} finally {
isLoading.value = false
}
}
const handleCancel = () => {
handleClose()
}
const handleAction = async (action, value) => {
if (action && typeof action === 'object') {
isVisible.value = false
await new Promise(resolve => setTimeout(resolve, 100))
await parseConfig(action)
return
}
await handleSubmit({ action, value })
}
const clearError = () => {
error.value = null
}
const handleSpecialActionFromChild = (actionType, actionData) => {
emit('special-action', actionType, actionData)
handleClose()
}
watch(() => props.actionData, async (newData) => {
await parseConfig(newData)
}, { immediate: true })
watch(() => props.visible, (newVal) => {
isVisible.value = newVal
})
const show = async (actionData) => {
if (actionData) {
await parseConfig(actionData)
}
isVisible.value = true
}
const hide = () => {
isVisible.value = false
}
const executeParentAction = async (actionId, value) => {
try {
isLoading.value = true
emit('action', actionId, value)
} catch (err) {
error.value = err
emit('error', err)
throw err
} finally {
isLoading.value = false
}
}
const handleReset = () => {}
return {
parsedConfig,
currentComponent,
error,
isLoading,
isVisible,
handleSubmit,
handleCancel,
handleClose,
handleAction,
handleToast,
handleReset,
handleSpecialActionFromChild,
clearError,
show,
hide,
executeParentAction,
executeAction: executeParentAction
}
}
}
</script>
<style scoped>
.action-renderer {
position: relative;
}
.action-error-details {
margin-top: 12px;
}
.action-error-details pre {
margin: 0;
white-space: pre-wrap;
word-break: break-word;
font-size: 12px;
}
.action-loading {
display: flex;
justify-content: center;
padding: 24px 0;
}
</style>