Commit 72b813a2354ffb861e27c5a24ba34940e57d2bba

Authored by ww
1 parent eba9e51c

fix: 修复脚本管理保存数据结构变更

... ... @@ -43,6 +43,7 @@ export interface TransportScriptParamsType {
43 43 scriptLanguage: ScriptLanguageEnum;
44 44 status: number;
45 45 serviceType?: string;
  46 + scriptType: string;
46 47 }
47 48
48 49 export interface TransportScriptRecordType {
... ... @@ -58,4 +59,5 @@ export interface TransportScriptRecordType {
58 59 convertJs?: string;
59 60 serviceType?: string;
60 61 saveOriginalData: boolean;
  62 + scriptType: string;
61 63 }
... ...
... ... @@ -3,6 +3,8 @@
3 3 import { BasicForm, useForm } from '/@/components/Form';
4 4 import { formSchemas } from './create.config';
5 5 import { NodeData } from '../../../types/node';
  6 + import { ScriptEditorValueType } from '/@/views/rule/designer/src/components/JavaScriptFilterModal';
  7 + import { ScriptFieldsEnum } from '../../../enum/formField/transformation';
6 8
7 9 defineProps<{
8 10 config: NodeData;
... ... @@ -16,12 +18,15 @@
16 18 const getValue: CreateModalDefineExposeType['getFieldsValue'] = async () => {
17 19 await validate();
18 20 const value = getFieldsValue() || {};
19   - return value;
  21 + const { scriptLang, jsScript, tbelScript } = (value || {})?.jsScript as ScriptEditorValueType;
  22 + return { scriptLang, jsScript, tbelScript };
20 23 };
21 24
22 25 const setValue: CreateModalDefineExposeType['setFieldsValue'] = (value) => {
23 26 resetFields();
24   - setFieldsValue(value);
  27 + setFieldsValue({
  28 + [ScriptFieldsEnum.JS_SCRIPT]: value,
  29 + });
25 30 };
26 31
27 32 defineExpose({
... ...
... ... @@ -27,7 +27,9 @@ export const ScriptConfig: NodeItemConfigType = {
27 27 customRelations: false,
28 28 ruleChainNode: false,
29 29 defaultConfiguration: {
  30 + scriptLang: 'TBEL',
30 31 jsScript: 'return {msg: msg, metadata: metadata, msgType: msgType};',
  32 + tbelScript: 'return {msg: msg, metadata: metadata, msgType: msgType};',
31 33 },
32 34 uiResources: ['static/rulenode/rulenode-core-config.js'],
33 35 configDirective: 'tbTransformationNodeScriptConfig',
... ...
... ... @@ -10,7 +10,7 @@
10 10 const props = defineProps<{
11 11 onlyTest?: boolean;
12 12 visible?: boolean;
13   - language?: ScriptLanguageEnum;
  13 + language: ScriptLanguageEnum;
14 14 messageValue?: Recordable;
15 15 beforeTest?: (params: ScriptTestParams) => Promise<Recordable>;
16 16 javaScriptEditorProps?: InstanceType<typeof JavaScriptFunctionEditor>['$props'];
... ...
... ... @@ -36,14 +36,15 @@
36 36 const [register, { openModal }] = useModal();
37 37
38 38 const handleOpen = () => {
  39 + const { scriptLang, jsScript, tbelScript } = props.value || {};
39 40 openModal(true, {
40   - record: props.value?.value,
  41 + record: scriptLang === ScriptLanguageEnum.JavaScript ? jsScript : tbelScript,
41 42 mode: DataActionModeEnum.CREATE,
42 43 } as ModalParamsType<string>);
43 44 };
44 45
45 46 watch(
46   - () => props.value?.scriptLanguage,
  47 + () => props.value?.scriptLang,
47 48 (target) => {
48 49 target && (activeLanguage.value = target);
49 50 },
... ... @@ -56,28 +57,49 @@
56 57
57 58 const getValue = computed<string>({
58 59 get() {
59   - return props.value?.value || '';
  60 + const { scriptLang, jsScript = '', tbelScript = '' } = props.value || {};
  61 + return scriptLang === ScriptLanguageEnum.JavaScript ? jsScript : tbelScript;
60 62 },
61 63 set(value) {
  64 + const { jsScript, tbelScript } = props.value || {};
  65 + const scriptLang = unref(activeLanguage);
62 66 emit(
63 67 'update:value',
64 68 toRaw({
65   - value,
66   - scriptLanguage: unref(activeLanguage),
  69 + jsScript: scriptLang === ScriptLanguageEnum.JavaScript ? value : jsScript,
  70 + tbelScript: scriptLang === ScriptLanguageEnum.TBEL ? value : tbelScript,
  71 + scriptLang,
67 72 } as ScriptEditorValueType)
68 73 );
69 74 },
70 75 });
71 76
72 77 const handleSave = (value: Record<'javascriptFunction', string>) => {
  78 + const { jsScript, tbelScript } = props.value || {};
  79 + const scriptLang = unref(activeLanguage);
73 80 emit(
74 81 'update:value',
75 82 toRaw({
76   - value: value.javascriptFunction,
77   - scriptLanguage: unref(activeLanguage),
  83 + jsScript: scriptLang === ScriptLanguageEnum.JavaScript ? value : jsScript,
  84 + tbelScript: scriptLang === ScriptLanguageEnum.TBEL ? value : tbelScript,
  85 + scriptLang,
78 86 } as ScriptEditorValueType)
79 87 );
80 88 };
  89 +
  90 + function onScriptLangteChange(item: string) {
  91 + activeLanguage.value = ScriptLanguageEnum[item];
  92 + const { jsScript, tbelScript } = props.value || {};
  93 + const scriptLang = unref(activeLanguage);
  94 + emit(
  95 + 'update:value',
  96 + toRaw({
  97 + scriptLang,
  98 + jsScript,
  99 + tbelScript,
  100 + } as ScriptEditorValueType)
  101 + );
  102 + }
81 103 </script>
82 104
83 105 <template>
... ... @@ -91,7 +113,7 @@
91 113 :key="item"
92 114 class="cursor-pointer text-center w-40 rounded-3xl h-8 leading-8"
93 115 :class="activeLanguage === ScriptLanguageEnum[item] && 'bg-blue-500 text-light-50'"
94   - @click="activeLanguage = ScriptLanguageEnum[item]"
  116 + @click="onScriptLangteChange(item)"
95 117 >
96 118 {{ item }}
97 119 </div>
... ...
... ... @@ -5,6 +5,7 @@ export { default as JavascriptEditorWithTestModal } from './JavascriptEditorWith
5 5 export { default as JavaScriptFilterTest } from './JavaScriptFilterTest.vue';
6 6
7 7 export interface ScriptEditorValueType {
8   - scriptLanguage: ScriptLanguageEnum;
9   - value: string;
  8 + scriptLang: ScriptLanguageEnum;
  9 + jsScript: string;
  10 + tbelScript: string;
10 11 }
... ...
... ... @@ -7,7 +7,6 @@
7 7 TransportScriptParamsType,
8 8 TransportScriptRecordType,
9 9 } from '/@/api/scriptmanage/model/scriptModel';
10   - import { ScriptLanguageEnum } from '/@/enums/scriptEnum';
11 10 import { computed, ref, unref } from 'vue';
12 11 import { DataActionModeEnum } from '/@/enums/toolEnum';
13 12
... ... @@ -21,22 +20,26 @@
21 20 resetFields();
22 21 clearValidate();
23 22 if ([DataActionModeEnum.UPDATE, DataActionModeEnum.READ].includes(mode)) {
24   - const { name, description, convertTbel, convertJs, scriptLanguage, serviceType } =
25   - record || {};
26   - activeLanguage.value = scriptLanguage;
  23 + const {
  24 + name,
  25 + description,
  26 + convertTbel,
  27 + convertJs,
  28 + scriptLanguage,
  29 + serviceType,
  30 + scriptType = 'TRANSPORT_TCP_UP',
  31 + } = record || {};
27 32 setFieldsValue({
28 33 name,
29 34 description,
30 35 serviceType,
31 36 language: scriptLanguage,
32 37 scriptContent: {
33   - scriptLanguage: 'JS',
34   - value: convertJs,
35   - },
36   - tbelContent: {
37   - scriptLanguage: 'TBEL',
38   - value: convertTbel,
  38 + scriptLang: scriptLanguage,
  39 + jsScript: convertJs,
  40 + tbelScript: convertTbel,
39 41 },
  42 + scriptType,
40 43 } as FormFieldsValueType);
41 44 }
42 45 currentData.value = data;
... ... @@ -57,26 +60,20 @@
57 60 try {
58 61 setModalProps({ loading: true, confirmLoading: true });
59 62 await validate();
60   - const {
61   - name,
62   - description,
63   - scriptContent: { value },
64   - language,
65   - scriptType,
66   - tbelContent: { value: taelValue },
67   - serviceType,
68   - } = getFieldsValue() as FormFieldsValueType;
  63 + const { name, description, scriptContent, scriptType, serviceType } =
  64 + getFieldsValue() as FormFieldsValueType;
69 65
  66 + const { scriptLang, tbelScript, jsScript } = scriptContent || {};
70 67 const params: TransportScriptParamsType = {
71 68 name,
72 69 description,
73 70 status: 1,
74 71 scriptType,
75   - scriptLanguage: language,
  72 + scriptLanguage: scriptLang,
  73 + convertJs: jsScript,
  74 + convertTbel: tbelScript,
76 75 serviceType,
77 76 };
78   - params.convertJs = value;
79   - params.convertTbel = taelValue;
80 77
81 78 if (unref(currentData)?.mode === DataActionModeEnum.UPDATE) {
82 79 params.id = unref(currentData)!.record.id;
... ... @@ -89,11 +86,6 @@
89 86 setModalProps({ loading: false, confirmLoading: false });
90 87 }
91 88 };
92   - const activeLanguage = ref<string>(ScriptLanguageEnum.JavaScript);
93   - const languageChange = (type, model) => {
94   - activeLanguage.value = type;
95   - model.language = type;
96   - };
97 89 </script>
98 90
99 91 <template>
... ... @@ -104,20 +96,6 @@
104 96 @ok="handleOk"
105 97 :showOkBtn="currentData?.mode !== DataActionModeEnum.READ"
106 98 >
107   - <BasicForm @register="registerForm">
108   - <template #language="{ model }">
109   - <div class="bg-gray-200 text-gray-400 font-medium rounded-3xl flex w-80 mb-2 mx-auto">
110   - <div
111   - v-for="item in Object.keys(ScriptLanguageEnum)"
112   - :key="item"
113   - class="cursor-pointer text-center w-40 rounded-3xl h-8 leading-8"
114   - :class="activeLanguage === ScriptLanguageEnum[item] && 'bg-blue-500 text-light-50'"
115   - @click="languageChange(ScriptLanguageEnum[item], model)"
116   - >
117   - {{ item }}
118   - </div>
119   - </div>
120   - </template>
121   - </BasicForm>
  99 + <BasicForm @register="registerForm" />
122 100 </BasicModal>
123 101 </template>
... ...
... ... @@ -9,6 +9,7 @@ import { Tag, Tooltip } from 'ant-design-vue';
9 9 import Icon from '/@/components/Icon';
10 10 import { ScriptLanguageEnum } from '/@/enums/scriptEnum';
11 11 import { findDictItemByCode } from '/@/api/system/dict';
  12 +import { DeviceTypeEnum } from '../../../dataFlow/cpns/config';
12 13
13 14 useComponentRegister('JavascriptEditorWithTestModal', JavascriptEditorWithTestModal);
14 15
... ... @@ -16,17 +17,15 @@ export enum FormFieldsEnum {
16 17 Name = 'name',
17 18 Description = 'description',
18 19 ScriptContent = 'scriptContent',
19   - TbelContent = 'tbelContent',
20 20 ServiceType = 'serviceType',
21 21 }
22 22
23 23 export interface FormFieldsValueType {
24 24 [FormFieldsEnum.Name]: string;
25 25 [FormFieldsEnum.ScriptContent]: ScriptEditorValueType;
26   - [FormFieldsEnum.TbelContent]: ScriptEditorValueType;
27 26 [FormFieldsEnum.Description]?: string;
28 27 [FormFieldsEnum.ServiceType]?: string;
29   - language: string;
  28 + scriptType: string;
30 29 }
31 30
32 31 export const formSchemas: FormSchema[] = [
... ... @@ -65,32 +64,28 @@ export const formSchemas: FormSchema[] = [
65 64 labelField: 'itemText',
66 65 valueField: 'itemValue',
67 66 onChange(e) {
68   - if (e === 'SENSOR' || e === 'DIRECT_CONNECTION') {
69   - formModel[FormFieldsEnum.ScriptContent].value = getDirectWithSensorScript().JavaScript;
70   - formModel[FormFieldsEnum.TbelContent].value = getDirectWithSensorScript().TBEL;
71   - } else {
72   - formModel[FormFieldsEnum.ScriptContent].value = getGatewayScript().JavaScript;
73   - formModel[FormFieldsEnum.TbelContent].value = getGatewayScript().TBEL;
74   - }
  67 + const { jsScript, tbelScript, scriptLang } = (formModel[FormFieldsEnum.ScriptContent] ||
  68 + {}) as ScriptEditorValueType;
  69 +
  70 + const { JavaScript, TBEL } =
  71 + e === DeviceTypeEnum.SENSOR || e === DeviceTypeEnum.DIRECT_CONNECTION
  72 + ? getDirectWithSensorScript()
  73 + : getGatewayScript();
  74 +
  75 + formModel[FormFieldsEnum.ScriptContent] = {
  76 + scriptLang,
  77 + jsScript: scriptLang === ScriptLanguageEnum.JavaScript ? JavaScript : jsScript,
  78 + tbelScript: scriptLang === ScriptLanguageEnum.TBEL ? TBEL : tbelScript,
  79 + };
75 80 },
76 81 };
77 82 },
78 83 },
79   - {
80   - label: '',
81   - field: 'language',
82   - component: 'Input',
83   - defaultValue: ScriptLanguageEnum.JavaScript,
84   - slot: 'language',
85   - },
86   - //Javascript
  84 +
87 85 {
88 86 label: '',
89 87 field: FormFieldsEnum.ScriptContent,
90 88 component: 'JavascriptEditorWithTestModal',
91   - ifShow: ({ model }) => {
92   - return model.language === ScriptLanguageEnum.JavaScript;
93   - },
94 89 componentProps: {
95 90 buttonName: '测试转换脚本',
96 91 javaScriptEditorProps: {
... ... @@ -101,14 +96,14 @@ export const formSchemas: FormSchema[] = [
101 96 messageValue: {
102 97 params: '010304026C00883BF0',
103 98 },
104   - isShowLanguage: false,
105 99 beforeTest: (params: Recordable) => {
106 100 return { ...params, needMetadataAndMsgType: true };
107 101 },
108 102 },
109 103 defaultValue: {
110   - scriptLanguage: ScriptLanguageEnum.JavaScript,
111   - value: getGatewayScript().JavaScript,
  104 + scriptLang: ScriptLanguageEnum.JavaScript,
  105 + jsScript: getGatewayScript().JavaScript,
  106 + tbelScript: getGatewayScript().TBEL,
112 107 } as ScriptEditorValueType,
113 108 changeEvent: 'update:value',
114 109 valueField: 'value',
... ... @@ -126,9 +121,13 @@ export const formSchemas: FormSchema[] = [
126 121 event: 'directWithSensor',
127 122 onClick: () => {
128 123 const { JavaScript, TBEL } = getDirectWithSensorScript();
  124 + const { jsScript, tbelScript } = (model[FormFieldsEnum.ScriptContent] ||
  125 + {}) as ScriptEditorValueType;
129 126 model[FormFieldsEnum.ScriptContent] = {
130   - scriptLanguage,
131   - value: scriptLanguage === ScriptLanguageEnum.JavaScript ? JavaScript : TBEL,
  127 + scriptLang: scriptLanguage,
  128 + jsScript:
  129 + scriptLanguage === ScriptLanguageEnum.JavaScript ? JavaScript : jsScript,
  130 + tbelScript: scriptLanguage === ScriptLanguageEnum.TBEL ? TBEL : tbelScript,
132 131 };
133 132 },
134 133 },
... ... @@ -137,9 +136,13 @@ export const formSchemas: FormSchema[] = [
137 136 event: 'gateway',
138 137 onClick: () => {
139 138 const { JavaScript, TBEL } = getGatewayScript();
  139 + const { jsScript, tbelScript } = (model[FormFieldsEnum.ScriptContent] ||
  140 + {}) as ScriptEditorValueType;
140 141 model[FormFieldsEnum.ScriptContent] = {
141   - scriptLanguage,
142   - value: scriptLanguage === ScriptLanguageEnum.JavaScript ? JavaScript : TBEL,
  142 + scriptLang: scriptLanguage,
  143 + jsScript:
  144 + scriptLanguage === ScriptLanguageEnum.JavaScript ? JavaScript : jsScript,
  145 + tbelScript: scriptLanguage === ScriptLanguageEnum.TBEL ? TBEL : tbelScript,
143 146 };
144 147 },
145 148 },
... ... @@ -169,96 +172,6 @@ export const formSchemas: FormSchema[] = [
169 172 };
170 173 },
171 174 },
172   - // TBEL
173   - {
174   - label: '',
175   - field: FormFieldsEnum.TbelContent,
176   - component: 'JavascriptEditorWithTestModal',
177   - ifShow: ({ model }) => {
178   - return model.language === ScriptLanguageEnum.TBEL;
179   - },
180   - componentProps: {
181   - buttonName: '测试转换脚本',
182   - javaScriptEditorProps: {
183   - functionName: 'Transform',
184   - paramsName: ['msg', 'metadata', 'msgType'],
185   - scriptType: 'update',
186   - },
187   - messageValue: {
188   - params: '010304026C00883BF0',
189   - },
190   - isShowLanguage: false,
191   - beforeTest: (params: Recordable) => {
192   - return { ...params, needMetadataAndMsgType: true };
193   - },
194   - },
195   - defaultValue: {
196   - scriptLanguage: ScriptLanguageEnum.TBEL,
197   - value: getGatewayScript().TBEL,
198   - } as ScriptEditorValueType,
199   - changeEvent: 'update:value',
200   - valueField: 'value',
201   - renderComponentContent: ({ model }) => {
202   - return {
203   - beforeFormat: ({ scriptLanguage }) => {
204   - return [
205   - h(Tooltip, { title: '脚本用例' }, () =>
206   - h(
207   - Dropdown,
208   - {
209   - trigger: ['click'],
210   - dropMenuList: [
211   - {
212   - text: '直连/子设备用例',
213   - event: 'directWithSensor',
214   - onClick: () => {
215   - const { JavaScript, TBEL } = getDirectWithSensorScript();
216   - model[FormFieldsEnum.TbelContent] = {
217   - scriptLanguage,
218   - value:
219   - scriptLanguage === ScriptLanguageEnum.JavaScript ? JavaScript : TBEL,
220   - };
221   - },
222   - },
223   - {
224   - text: '网关用例',
225   - event: 'gateway',
226   - onClick: () => {
227   - const { JavaScript, TBEL } = getGatewayScript();
228   - model[FormFieldsEnum.TbelContent] = {
229   - scriptLanguage,
230   - value:
231   - scriptLanguage === ScriptLanguageEnum.JavaScript ? JavaScript : TBEL,
232   - };
233   - },
234   - },
235   - ],
236   - },
237   - () =>
238   - h(
239   - Tag,
240   - { color: '#2a79ef', class: 'cursor-pointer' },
241   - {
242   - icon: () =>
243   - h(Icon, {
244   - icon: 'magic',
245   - prefix: 'mdi',
246   - size: 14,
247   - class: 'cursor-pointer svg:text-sm',
248   - }),
249   - default: () => '脚本用例',
250   - }
251   - )
252   - )
253   - ),
254   - h(Tooltip, { title: '复制' }, () =>
255   - h(Icon, { icon: 'copy-filled', prefix: 'ant-design', class: 'cursor-pointer' })
256   - ),
257   - ];
258   - },
259   - };
260   - },
261   - },
262 175 {
263 176 field: FormFieldsEnum.Description,
264 177 label: '备注',
... ...
... ... @@ -174,7 +174,7 @@
174 174 }
175 175 };
176 176
177   - const testScriptLanguage = ref<string>(ScriptLanguageEnum.JavaScript);
  177 + const testScriptLanguage = ref<ScriptLanguageEnum>(ScriptLanguageEnum.JavaScript);
178 178 const handleOpenTestModal = (record: TransportScriptRecordType) => {
179 179 const { scriptLanguage = ScriptLanguageEnum.JavaScript, convertJs, convertTbel } = record;
180 180 testScriptLanguage.value = scriptLanguage!;
... ...
... ... @@ -150,8 +150,9 @@ export const formSchema: FormSchema[] = [
150 150 label: '转换函数',
151 151 changeEvent: 'update:value',
152 152 defaultValue: {
153   - value: 'return {msg: msg, metadata: metadata, msgType: msgType};',
154   - scriptLanguage: ScriptLanguageEnum.JavaScript,
  153 + jsScript: 'return {msg: msg, metadata: metadata, msgType: msgType};',
  154 + scriptLang: ScriptLanguageEnum.JavaScript,
  155 + tbelScript: 'return {msg: msg, metadata: metadata, msgType: msgType};',
155 156 },
156 157 componentProps: {
157 158 javaScriptEditorProps: {
... ...
... ... @@ -32,6 +32,7 @@
32 32 resetFields();
33 33 setDrawerProps({ confirmLoading: false });
34 34 isUpdate.value = data.isUpdate;
  35 +
35 36 switch (isUpdate.value) {
36 37 case 'view':
37 38 isView.value = false;
... ... @@ -41,7 +42,10 @@
41 42 loading: false,
42 43 });
43 44 editId.value = data.record.id;
44   - setFieldsValue({ ...data.record, function: data.record?.configuration?.jsScript });
  45 + setFieldsValue({
  46 + ...data.record,
  47 + function: data?.record?.configuration,
  48 + });
45 49 break;
46 50 case true:
47 51 isView.value = true;
... ... @@ -51,7 +55,10 @@
51 55 loading: false,
52 56 });
53 57 editId.value = data.record.id;
54   - setFieldsValue({ ...data.record, function: data.record?.configuration?.jsScript });
  58 + setFieldsValue({
  59 + ...data.record,
  60 + function: data?.record?.configuration,
  61 + });
55 62 break;
56 63 case false:
57 64 isView.value = true;
... ... @@ -75,10 +82,9 @@
75 82 setDrawerProps({ confirmLoading: true });
76 83 const fieldsValue = await validate();
77 84 if (!fieldsValue) return;
  85 +
78 86 await createOrEditTransformScriptApi({
79   - configuration: {
80   - jsScript: fieldsValue.function,
81   - },
  87 + configuration: fieldsValue.function,
82 88 type: 'org.thingsboard.rule.engine.transform.TbTransformMsgNode',
83 89 ...fieldsValue,
84 90 ...editIdPost,
... ...