-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathActionRenderer.vue
More file actions
399 lines (352 loc) · 8.98 KB
/
ActionRenderer.vue
File metadata and controls
399 lines (352 loc) · 8.98 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<template>
<div class="action-renderer">
<!-- 动态渲染Action组件 -->
<component
v-if="parsedConfig"
:is="currentComponent"
:config="parsedConfig"
:visible="isVisible"
@submit="handleSubmit"
@cancel="handleCancel"
@close="handleClose"
@action="handleAction"
/>
<!-- 错误提示 -->
<ActionDialog
v-if="error"
:visible="!!error"
title="错误"
width="400"
@close="clearError"
>
<div class="action-error">
<p><strong>{{ error.type }}</strong></p>
<p>{{ error.message }}</p>
<pre v-if="error.details">{{ JSON.stringify(error.details, null, 2) }}</pre>
</div>
<template #footer>
<button class="action-button action-button-primary" @click="clearError">
确定
</button>
</template>
</ActionDialog>
<!-- 加载状态 -->
<ActionDialog
v-if="isLoading"
:visible="isLoading"
title="处理中"
width="300"
:show-close="false"
>
<div class="action-loading">
正在处理,请稍候...
</div>
</ActionDialog>
<!-- Toast提示 -->
<Transition name="action-toast">
<div v-if="toast" class="action-toast" :class="toastType">
{{ toast }}
</div>
</Transition>
</div>
</template>
<script>
import { ref, computed, watch, defineAsyncComponent } from 'vue'
import ActionDialog from './ActionDialog.vue'
import {
parseActionConfig,
validateActionConfig,
ActionType,
ActionErrorType,
createActionError
} from './types.js'
// 懒加载Action组件
const InputAction = defineAsyncComponent(() => import('./InputAction.vue'))
const MultiInputAction = defineAsyncComponent(() => import('./MultiInputAction.vue'))
const MenuAction = defineAsyncComponent(() => import('./MenuAction.vue'))
const SelectAction = defineAsyncComponent(() => import('./SelectAction.vue'))
const MsgBoxAction = defineAsyncComponent(() => import('./MsgBoxAction.vue'))
const WebViewAction = defineAsyncComponent(() => import('./WebViewAction.vue'))
const HelpAction = defineAsyncComponent(() => import('./HelpAction.vue'))
export default {
name: 'ActionRenderer',
components: {
ActionDialog,
InputAction,
MultiInputAction,
MenuAction,
SelectAction,
MsgBoxAction,
WebViewAction,
HelpAction
},
props: {
// Action数据,可以是JSON字符串或对象
actionData: {
type: [String, Object],
default: null
},
// 是否显示
visible: {
type: Boolean,
default: true
},
// 自动显示,当actionData变化时自动显示
autoShow: {
type: Boolean,
default: true
}
},
emits: ['action', 'close', 'error', 'success'],
setup(props, { emit }) {
const parsedConfig = ref(null)
const error = ref(null)
const isLoading = ref(false)
const isVisible = ref(props.visible)
const toast = ref('')
const toastType = ref('success')
const toastTimer = ref(null)
// 组件映射
const componentMap = {
[ActionType.INPUT]: 'InputAction',
[ActionType.EDIT]: 'InputAction', // edit类型使用InputAction,通过multiLine区分
[ActionType.MULTI_INPUT]: 'MultiInputAction',
[ActionType.MULTI_INPUT_X]: 'MultiInputAction',
[ActionType.MENU]: 'MenuAction',
[ActionType.SELECT]: 'SelectAction',
[ActionType.MSGBOX]: 'MsgBoxAction',
[ActionType.WEBVIEW]: 'WebViewAction',
[ActionType.HELP]: 'HelpAction'
}
// 当前组件
const currentComponent = computed(() => {
if (!parsedConfig.value) return null
return componentMap[parsedConfig.value.type] || null
})
// 解析Action配置
const parseConfig = (data) => {
try {
if (!data) {
parsedConfig.value = null
return
}
const config = parseActionConfig(data)
validateActionConfig(config)
// 处理特殊动作
handleSpecialActions(config)
parsedConfig.value = config
error.value = null
if (props.autoShow) {
isVisible.value = true
}
} catch (err) {
console.error('解析Action配置失败:', err)
error.value = err
parsedConfig.value = null
}
}
// 处理特殊动作
const handleSpecialActions = (config) => {
const { actionId } = config
// 专项动作处理
switch (actionId) {
case '源内搜索':
// 可以在这里添加特殊处理逻辑
break
case '详情页跳转':
// 可以在这里添加特殊处理逻辑
break
case 'KTV播放':
// 可以在这里添加特殊处理逻辑
break
case '刷新列表':
// 可以在这里添加特殊处理逻辑
break
case '放入剪贴板':
// 可以在这里添加特殊处理逻辑
break
case '保持窗口':
// 可以在这里添加特殊处理逻辑
break
}
}
// 处理提交
const handleSubmit = async (value) => {
if (!parsedConfig.value) return
try {
isLoading.value = true
// 触发action事件
const result = await emit('action', parsedConfig.value.actionId, value)
// 处理返回结果
if (result && typeof result === 'object') {
if (result.success === false) {
throw createActionError(
ActionErrorType.NETWORK_ERROR,
result.error || '操作失败'
)
}
// 显示toast消息
if (result.toast) {
showToast(result.toast, 'success')
}
// 处理新的action
if (result.action) {
parseConfig(result.action)
return
}
}
// 默认成功处理
emit('success', value)
handleClose()
} catch (err) {
console.error('执行Action失败:', err)
error.value = err
} finally {
isLoading.value = false
}
}
// 处理取消
const handleCancel = () => {
handleClose()
}
// 处理关闭
const handleClose = () => {
isVisible.value = false
parsedConfig.value = null
emit('close')
}
// 处理动作
const handleAction = async (action, value) => {
await handleSubmit({ action, value })
}
// 清除错误
const clearError = () => {
error.value = null
}
// 显示Toast
const showToast = (message, type = 'success') => {
if (toastTimer.value) {
clearTimeout(toastTimer.value)
}
toast.value = message
toastType.value = type
toastTimer.value = setTimeout(() => {
toast.value = ''
toastTimer.value = null
}, 3000)
}
// 监听actionData变化
watch(() => props.actionData, (newData) => {
parseConfig(newData)
}, { immediate: true })
// 监听visible变化
watch(() => props.visible, (newVal) => {
isVisible.value = newVal
})
// 公开方法
const show = (actionData) => {
if (actionData) {
parseConfig(actionData)
}
isVisible.value = true
}
const hide = () => {
isVisible.value = false
}
const executeAction = async (actionId, value) => {
try {
isLoading.value = true
const result = await emit('action', actionId, value)
return result
} catch (err) {
error.value = err
throw err
} finally {
isLoading.value = false
}
}
return {
parsedConfig,
currentComponent,
error,
isLoading,
isVisible,
toast,
toastType,
handleSubmit,
handleCancel,
handleClose,
handleAction,
clearError,
show,
hide,
executeAction,
showToast
}
}
}
</script>
<style scoped>
.action-renderer {
position: relative;
}
/* Toast样式 */
.action-toast {
position: fixed;
top: 20px;
left: 50%;
transform: translateX(-50%);
padding: 12px 24px;
border-radius: 6px;
color: white;
font-size: 14px;
z-index: 2000;
max-width: 400px;
text-align: center;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}
.action-toast.success {
background: #52c41a;
}
.action-toast.error {
background: #f5222d;
}
.action-toast.warning {
background: #faad14;
}
.action-toast.info {
background: #1890ff;
}
/* Toast动画 */
.action-toast-enter-active,
.action-toast-leave-active {
transition: all 0.3s ease;
}
.action-toast-enter-from {
opacity: 0;
transform: translateX(-50%) translateY(-20px);
}
.action-toast-leave-to {
opacity: 0;
transform: translateX(-50%) translateY(-20px);
}
/* 错误样式 */
.action-error {
color: #f5222d;
}
.action-error pre {
background: #fff2f0;
border: 1px solid #ffccc7;
border-radius: 4px;
padding: 8px;
margin-top: 8px;
font-size: 12px;
overflow-x: auto;
}
/* 加载样式 */
.action-loading {
text-align: center;
padding: 20px;
color: #8c8c8c;
}
</style>