-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathActionRenderer.vue
More file actions
550 lines (492 loc) · 14.8 KB
/
ActionRenderer.vue
File metadata and controls
550 lines (492 loc) · 14.8 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
<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"
/>
<!-- 错误提示 -->
<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>
</div>
</template>
<script>
import { ref, computed, watch, defineAsyncComponent } from 'vue'
import ActionDialog from './ActionDialog.vue'
import {
parseActionConfig,
validateActionConfig,
ActionType,
ActionErrorType,
createActionError
} from './types.js'
import { executeAction } from '@/api/modules/module.js'
import { showToast } from '@/stores/toast.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
},
// 模块名称,用于T4接口调用
module: {
type: String,
default: ''
},
// 扩展参数,用于T4接口调用
extend: {
type: Object,
default: () => ({})
},
// API URL,用于直接调用站点API
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', // 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) {
console.log('ActionRenderer currentComponent: parsedConfig.value 为空')
return null
}
const component = componentMap[parsedConfig.value.type] || null
console.log('ActionRenderer currentComponent:', {
type: parsedConfig.value.type,
component: component,
parsedConfig: parsedConfig.value
})
return component
})
// 解析Action配置
const parseConfig = (data) => {
try {
console.log('ActionRenderer parseConfig 开始解析:', data)
if (!data) {
parsedConfig.value = null
return
}
const config = parseActionConfig(data)
console.log('ActionRenderer parseConfig 解析后的配置:', config)
validateActionConfig(config)
// 处理特殊动作
handleSpecialActions(config)
parsedConfig.value = config
error.value = null
console.log('ActionRenderer parseConfig 设置 parsedConfig.value:', parsedConfig.value)
if (props.autoShow) {
isVisible.value = true
console.log('ActionRenderer parseConfig 设置 isVisible.value = true, autoShow:', props.autoShow)
} else {
console.log('ActionRenderer parseConfig autoShow 为 false,不自动显示')
}
} 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 handleSpecialAction = async (actionData) => {
const { actionId } = actionData
switch (actionId) {
case '__self_search__':
// 源内搜索
showToast('执行源内搜索', 'info')
emit('special-action', 'self-search', actionData)
handleClose()
break
case '__detail__':
// 详情页跳转
showToast('跳转到详情页', 'info')
emit('special-action', 'detail', actionData)
handleClose()
break
case '__ktvplayer__':
// KTV播放
showToast('启动KTV播放', 'info')
emit('special-action', 'ktv-player', actionData)
handleClose()
break
case '__refresh_list__':
// 刷新列表
showToast('刷新列表', 'info')
emit('special-action', 'refresh-list', actionData)
handleClose()
break
case '__copy__':
// 复制到剪贴板
if (actionData.content) {
try {
await navigator.clipboard.writeText(actionData.content)
showToast('已复制到剪贴板', 'success')
} catch (err) {
showToast('复制失败', 'error')
}
}
handleClose()
break
case '__keep__':
// 保持窗口
if (actionData.msg) {
showToast(actionData.msg, 'info')
}
if (actionData.reset) {
// 重置窗口内容
parsedConfig.value = null
}
// 不关闭窗口
break
default:
showToast(`未知的专项动作: ${actionId}`, 'warning')
handleClose()
break
}
}
// 处理提交
const handleSubmit = async (value) => {
if (!parsedConfig.value) return
try {
isLoading.value = true
// 准备action数据
const actionData = {
action: parsedConfig.value.actionId,
value: typeof value === 'object' ? JSON.stringify(value) : value
}
// 添加扩展参数
if (props.extend) {
actionData.extend = props.extend
}
// 添加API URL
if (props.apiUrl) {
actionData.apiUrl = props.apiUrl
}
// 调用T4接口
console.log('ActionRenderer准备调用T4接口:', {
module: props.module,
actionData,
apiUrl: props.apiUrl
})
let result = null
if (props.module) {
result = await executeAction(props.module, actionData)
console.log('T4接口返回结果:', result)
} else {
// 如果没有module,触发action事件让父组件处理
result = await emit('action', parsedConfig.value.actionId, value)
console.log('父组件处理结果:', result)
}
// 处理返回结果
if (result) {
// 如果返回的是字符串,尝试解析为JSON
if (typeof result === 'string') {
try {
const parsedResult = JSON.parse(result)
result = parsedResult
} catch (e) {
// 如果不是JSON,作为toast消息显示
showToast(result, 'success')
emit('success', value)
handleClose()
return
}
}
// 处理对象结果
if (typeof result === 'object') {
console.log('处理API返回的对象结果:', result)
// 检查错误
if (result.error) {
throw createActionError(
ActionErrorType.NETWORK_ERROR,
result.error
)
}
// 显示toast消息
if (result.toast) {
showToast(result.toast, 'success')
}
// 处理消息字段(兼容不同的消息字段名)
if (result.message || result.msg) {
showToast(result.message || result.msg, 'success')
}
// 处理新的action(动态action)
if (result.action) {
console.log('检测到动态action,重新解析:', result.action)
parseConfig(result.action)
return
}
// 处理专项动作
if (result.actionId) {
console.log('检测到专项动作:', result.actionId)
await handleSpecialAction(result)
return
}
// 处理状态码
if (result.code !== undefined) {
if (result.code === 0 || result.code === 200) {
// 成功状态
if (result.data) {
// 如果有data字段,递归处理data内容
if (typeof result.data === 'object') {
// 递归处理data对象
const dataResult = { ...result.data }
if (dataResult.action || dataResult.actionId) {
console.log('在data字段中检测到action:', dataResult)
if (dataResult.action) {
parseConfig(dataResult.action)
return
}
if (dataResult.actionId) {
await handleSpecialAction(dataResult)
return
}
}
}
}
showToast(result.message || result.msg || '操作成功', 'success')
} else {
// 错误状态
throw createActionError(
ActionErrorType.NETWORK_ERROR,
result.message || result.msg || `操作失败,错误码: ${result.code}`
)
}
}
}
}
// 默认成功处理
emit('success', value)
handleClose()
} catch (err) {
console.error('执行Action失败:', err)
error.value = err
showToast(err.message || '操作失败', 'error')
} finally {
isLoading.value = false
}
}
// 处理取消
const handleCancel = () => {
handleClose()
}
// 处理关闭
const handleClose = () => {
isVisible.value = false
parsedConfig.value = null
emit('close')
}
// 处理动作
const handleAction = async (action, value) => {
// 如果action是一个对象(新的动作配置),则重新解析
if (typeof action === 'object' && action.type) {
console.log('ActionRenderer接收到新的动作配置:', action)
// 先隐藏当前弹窗
isVisible.value = false
// 等待一个短暂的时间让当前弹窗完全关闭
await new Promise(resolve => setTimeout(resolve, 100))
// 然后解析新的配置
parseConfig(action)
return
}
// 否则按原来的逻辑处理
await handleSubmit({ action, value })
}
// 清除错误
const clearError = () => {
error.value = null
}
// 监听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
}
}
// 处理Toast消息
const handleToast = (message, type = 'success') => {
showToast(message, type)
}
// 处理重置事件
const handleReset = () => {
// 可以在这里添加额外的重置逻辑
console.log('InputAction触发重置事件')
}
return {
parsedConfig,
currentComponent,
error,
isLoading,
isVisible,
handleSubmit,
handleCancel,
handleClose,
handleAction,
handleToast,
handleReset,
clearError,
show,
hide,
executeAction
}
}
}
</script>
<style scoped>
.action-renderer {
position: relative;
}
/* 错误样式 */
.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>