Commit 6ad91962d462d3400ab7336c77d432a0c92c9958
1 parent
02fc83cf
fix: 修复JavaScriptFilterTest组件无法进行测试
Showing
3 changed files
with
41 additions
and
1 deletions
1 | -import { DeviceInfoItemType, DeviceTypeItem, PageParams } from './model'; | |
1 | +import { DeviceInfoItemType, DeviceTypeItem, PageParams, ScriptTestParams } from './model'; | |
2 | 2 | import { TBPaginationResult } from '/#/axios'; |
3 | 3 | import { defHttp } from '/@/utils/http/axios'; |
4 | 4 | |
... | ... | @@ -6,6 +6,7 @@ enum Api { |
6 | 6 | GET_DEVICE_INFOS = '/tenant/deviceInfos', |
7 | 7 | GET_DEVICE_TYPE = '/device/types', |
8 | 8 | TENANT_QUEUE = '/tenant/queues', |
9 | + TEST_SCRIPT = '/ruleChain/testScript', | |
9 | 10 | } |
10 | 11 | |
11 | 12 | enum Entity { |
... | ... | @@ -142,3 +143,13 @@ export const getEntityEdge = (params: PageParams) => { |
142 | 143 | } |
143 | 144 | ); |
144 | 145 | }; |
146 | + | |
147 | +export const testScript = (data: ScriptTestParams) => { | |
148 | + return defHttp.post<Record<'error' | 'output', string>>( | |
149 | + { | |
150 | + url: Api.TEST_SCRIPT, | |
151 | + data, | |
152 | + }, | |
153 | + { joinPrefix: false } | |
154 | + ); | |
155 | +}; | ... | ... |
... | ... | @@ -52,3 +52,12 @@ export interface PageParams { |
52 | 52 | sortProperty?: string; |
53 | 53 | sortOrder?: string; |
54 | 54 | } |
55 | + | |
56 | +export interface ScriptTestParams { | |
57 | + argNames?: string[]; | |
58 | + metadata?: Recordable; | |
59 | + msg?: string; | |
60 | + msgType?: string; | |
61 | + script?: string; | |
62 | + scriptType?: string; | |
63 | +} | ... | ... |
... | ... | @@ -77,13 +77,33 @@ |
77 | 77 | return { msg, msgType, metadata: toRaw(unref(metadata)), javascriptFunction }; |
78 | 78 | }; |
79 | 79 | |
80 | + function executeTestScript() { | |
81 | + try { | |
82 | + const { javaScriptEditorProps } = props; | |
83 | + const { paramsName } = javaScriptEditorProps; | |
84 | + const fn = new Function(...(paramsName || [])!, unref(scriptContent)); | |
85 | + const value = getValue(); | |
86 | + const executeParams = paramsName!.map((key) => value[key]); | |
87 | + | |
88 | + return fn(...executeParams); | |
89 | + } catch (error) { | |
90 | + return error; | |
91 | + } | |
92 | + } | |
93 | + | |
80 | 94 | const handleTestScript = async () => { |
81 | 95 | const flag = await handleValidate(); |
96 | + | |
82 | 97 | flag && emit('test', getValue()); |
98 | + | |
99 | + const result = executeTestScript(); | |
100 | + | |
101 | + outputContent.value = result; | |
83 | 102 | }; |
84 | 103 | |
85 | 104 | const handleSave = async () => { |
86 | 105 | const flag = await handleValidate(); |
106 | + | |
87 | 107 | flag && emit('save', getValue()); |
88 | 108 | }; |
89 | 109 | ... | ... |