115115<script setup>
116116import { ref , computed , watch } from ' vue'
117117import { Message } from ' @arco-design/web-vue'
118- import { Actions } from ' @/components/actions'
119118import ActionRenderer from ' @/components/actions/ActionRenderer.vue'
120- import { executeAction } from ' @/api/modules/module.js'
121- import { getActionTimeout } from ' @/api/config'
119+ import { callActionEndpoint } from ' @/components/actions/utils/actionTransport.js'
120+ import {
121+ createMessageAction ,
122+ normalizeActionConfig
123+ } from ' @/components/actions/utils/actionConfig.js'
122124
123125const props = defineProps ({
124126 visible: {
@@ -247,52 +249,48 @@ const getEmptyStateHint = () => {
247249 return ' 请确保站源配置中包含动作信息'
248250}
249251
250- // T4接口调用方法
251- const callT4Action = async ( action , siteKey , apiUrl , extend ) => {
252- if ( ! siteKey && ! apiUrl) {
253- console . warn ( ' 未提供siteKey或apiUrl,无法调用T4接口 ' )
254- return null
252+ const parseActionString = ( value ) => {
253+ try {
254+ return JSON . parse (value)
255+ } catch (error) {
256+ return JSON . parse ( value . replace ( / ' / g , ' " ' ))
255257 }
258+ }
256259
257- const actionData = {
258- action: action
260+ const extractActionFromResult = (result , action ) => {
261+ if (typeof result === ' string' ) {
262+ try {
263+ const parsedResult = parseActionString (result)
264+ return parsedResult .action || parsedResult
265+ } catch (error) {
266+ return createMessageAction ({
267+ title: action .name || ' 动作结果' ,
268+ msg: result,
269+ actionIdPrefix: ' global_msgbox'
270+ })
271+ }
259272 }
260273
261- // 添加扩展参数
262- if (extend && extend .ext ) {
263- actionData .extend = extend .ext
274+ if (result && typeof result === ' object' ) {
275+ return result .action || result
264276 }
265277
266- // 添加API URL到actionData中
267- if (apiUrl) {
268- actionData .apiUrl = apiUrl
269- }
278+ throw new Error (' T4接口返回了无效的数据格式' )
279+ }
270280
271- console .log (' GlobalActionDialog调用T4接口:' , {
272- siteKey,
273- actionData,
274- apiUrl
281+ // T4接口调用方法
282+ const callT4Action = async (action , siteKey , apiUrl , extend ) => {
283+ const result = await callActionEndpoint ({
284+ module: siteKey,
285+ apiUrl,
286+ extend,
287+ action
275288 })
276289
277- let result = null
278- if (siteKey) {
279- console .log (' 调用模块:' , siteKey)
280- result = await executeAction (siteKey, actionData)
281- } else if (apiUrl) {
282- // 直接调用API
283- console .log (' 直接调用API:' , apiUrl)
284- const axios = (await import (' axios' )).default
285- const response = await axios .post (apiUrl, actionData, {
286- timeout: getActionTimeout (),
287- headers: {
288- ' Accept' : ' application/json' ,
289- ' Content-Type' : ' application/json;charset=UTF-8' ,
290- }
291- })
292- result = response .data
290+ if (! result) {
291+ throw new Error (' T4接口调用失败或返回空结果' )
293292 }
294293
295- console .log (' T4接口返回结果:' , result)
296294 return result
297295}
298296
@@ -310,70 +308,18 @@ const handleActionClick = async (action) => {
310308 let actionConfig
311309 if (typeof action .action === ' string' ) {
312310 try {
313- // 尝试解析JSON字符串
314- actionConfig = JSON .parse (action .action .replace (/ '/ g , ' "' ))
311+ actionConfig = parseActionString (action .action )
315312 } catch (parseError) {
316- // 如果不是JSON,则调用T4接口获取动作配置
317- console .log (' 纯字符串动作,调用T4接口:' , action .action )
318-
319313 try {
320314 const t4Result = await callT4Action (action .action , action .siteKey , action .siteApi , action .siteExt )
321-
322- if (t4Result) {
323- // 检查T4接口返回的结果
324- if (typeof t4Result === ' string' ) {
325- try {
326- // 尝试解析T4返回的JSON字符串
327- const parsedResult = JSON .parse (t4Result)
328- // 如果解析结果包含action字段,则使用action字段作为动作配置
329- let extractedAction = parsedResult .action || parsedResult
330-
331- // 检查action字段是否也是JSON字符串,如果是则进行二次解析
332- if (typeof extractedAction === ' string' ) {
333- try {
334- extractedAction = JSON .parse (extractedAction)
335- } catch (secondParseError) {
336- console .warn (' action字段不是有效的JSON字符串:' , extractedAction)
337- }
338- }
339-
340- actionConfig = extractedAction
341- } catch (jsonParseError) {
342- // 如果T4返回的不是JSON,则显示为消息框
343- actionConfig = {
344- type: ' msgbox' ,
345- title: action .name || ' 动作结果' ,
346- msg: t4Result
347- }
348- }
349- } else if (typeof t4Result === ' object' && t4Result !== null ) {
350- // T4返回的是对象,检查是否包含action字段
351- let extractedAction = t4Result .action || t4Result
352-
353- // 检查action字段是否也是JSON字符串,如果是则进行二次解析
354- if (typeof extractedAction === ' string' ) {
355- try {
356- extractedAction = JSON .parse (extractedAction)
357- } catch (secondParseError) {
358- console .warn (' action字段不是有效的JSON字符串:' , extractedAction)
359- }
360- }
361-
362- actionConfig = extractedAction
363- } else {
364- throw new Error (' T4接口返回了无效的数据格式' )
365- }
366- } else {
367- throw new Error (' T4接口调用失败或返回空结果' )
368- }
315+ actionConfig = extractActionFromResult (t4Result, action)
369316 } catch (t4Error) {
370317 console .error (' T4接口调用失败:' , t4Error)
371- // T4接口调用失败时,显示错误信息
372- actionConfig = {
373- type: ' msgbox' ,
318+ actionConfig = createMessageAction ({
374319 title: ' 动作执行失败' ,
375- msg: ` 调用T4接口失败: ${ t4Error .message } `
376- }
320+ msg: ` 调用T4接口失败: ${ t4Error .message } ` ,
321+ actionIdPrefix: ' global_error'
322+ })
377323 }
378324 }
379325 } else if (typeof action .action === ' object' && action .action !== null ) {
@@ -382,6 +328,11 @@ const handleActionClick = async (action) => {
382328 throw new Error (' 无效的动作配置' )
383329 }
384330
331+ actionConfig = normalizeActionConfig (actionConfig, {
332+ ensureActionId: true ,
333+ actionIdPrefix: ' global'
334+ })
335+
385336 // 添加站源信息到配置中
386337 actionConfig .siteKey = action .siteKey
387338 actionConfig .siteName = action .siteName
0 commit comments