Commit 6d39d553559a8249fbd6e730fd04a1c873c5dcbd

Authored by ww
1 parent 0109c9c5

perf: 优化tcp产品创建物模型时操作类型关联数据类型

1 import { findDictItemByCode } from '/@/api/system/dict'; 1 import { findDictItemByCode } from '/@/api/system/dict';
2 import { FormSchema } from '/@/components/Table'; 2 import { FormSchema } from '/@/components/Table';
3 import { DictEnum } from '/@/enums/dictEnum'; 3 import { DictEnum } from '/@/enums/dictEnum';
  4 +import {
  5 + DataTypeEnum,
  6 + RegisterActionTypeEnum,
  7 + RegisterActionTypeNameEnum,
  8 + RegisterDataTypeEnum,
  9 +} from '/@/enums/objectModelEnum';
4 10
5 export enum FormFieldsEnum { 11 export enum FormFieldsEnum {
6 REGISTER_ADDRESS = 'registerAddress', 12 REGISTER_ADDRESS = 'registerAddress',
7 DATA_TYPE = 'dataType', 13 DATA_TYPE = 'dataType',
8 ACTION_TYPE = 'actionType', 14 ACTION_TYPE = 'actionType',
9 ZOOM_FACTOR = 'zoomFactor', 15 ZOOM_FACTOR = 'zoomFactor',
  16 +
  17 + OBJECT_MODEL_TYPE = 'objectModelType',
  18 +}
  19 +
  20 +function getActionTypeByObjectModelType(dataType: DataTypeEnum) {
  21 + const list: Record<'label' | 'value', string>[] = [];
  22 + if (dataType === DataTypeEnum.BOOL) {
  23 + list.push({ label: RegisterActionTypeNameEnum.BOOL, value: RegisterActionTypeEnum.BOOL });
  24 + } else if (dataType === DataTypeEnum.NUMBER_INT) {
  25 + list.push({ label: RegisterActionTypeNameEnum.INT, value: RegisterActionTypeEnum.INT });
  26 + } else if (dataType === DataTypeEnum.NUMBER_DOUBLE) {
  27 + list.push({ label: RegisterActionTypeNameEnum.DOUBLE, value: RegisterActionTypeEnum.DOUBLE });
  28 + } else {
  29 + list.push(
  30 + ...Object.keys(RegisterActionTypeEnum).map((key) => ({
  31 + label: RegisterActionTypeNameEnum[key],
  32 + value: RegisterActionTypeEnum[key],
  33 + }))
  34 + );
  35 + }
  36 +
  37 + return list;
10 } 38 }
11 39
12 export const formSchemas: FormSchema[] = [ 40 export const formSchemas: FormSchema[] = [
13 { 41 {
  42 + field: FormFieldsEnum.OBJECT_MODEL_TYPE,
  43 + component: 'Input',
  44 + label: '物模型数据类型',
  45 + ifShow: false,
  46 + },
  47 + {
14 field: FormFieldsEnum.REGISTER_ADDRESS, 48 field: FormFieldsEnum.REGISTER_ADDRESS,
15 component: 'RegisterAddressInput', 49 component: 'RegisterAddressInput',
16 label: '寄存器地址', 50 label: '寄存器地址',
@@ -26,6 +60,7 @@ export const formSchemas: FormSchema[] = [ @@ -26,6 +60,7 @@ export const formSchemas: FormSchema[] = [
26 component: 'ApiSelect', 60 component: 'ApiSelect',
27 label: '数据格式', 61 label: '数据格式',
28 rules: [{ message: '请选择数据格式', required: true }], 62 rules: [{ message: '请选择数据格式', required: true }],
  63 + defaultValue: RegisterDataTypeEnum.UN_SHORT,
29 componentProps: { 64 componentProps: {
30 api: findDictItemByCode, 65 api: findDictItemByCode,
31 params: { 66 params: {
@@ -42,15 +77,13 @@ export const formSchemas: FormSchema[] = [ @@ -42,15 +77,13 @@ export const formSchemas: FormSchema[] = [
42 component: 'Select', 77 component: 'Select',
43 label: '操作类型', 78 label: '操作类型',
44 rules: [{ message: '请选择操作类型', required: true }], 79 rules: [{ message: '请选择操作类型', required: true }],
45 - componentProps: {  
46 - options: [  
47 - { label: '05 写入单个线圈寄存器', value: '05' },  
48 - { label: '06 写入单个保持寄存器', value: '06' },  
49 - // { label: '0F 写入多个线圈状态', value: '0F' },  
50 - { label: '16 写入多个保持寄存器', value: '16' },  
51 - ],  
52 - placeholder: '请选择操作类型',  
53 - getPopupContainer: () => document.body, 80 + componentProps: ({ formModel }) => {
  81 + const objectModelType = formModel[FormFieldsEnum.OBJECT_MODEL_TYPE];
  82 + return {
  83 + options: getActionTypeByObjectModelType(objectModelType),
  84 + placeholder: '请选择操作类型',
  85 + getPopupContainer: () => document.body,
  86 + };
54 }, 87 },
55 }, 88 },
56 { 89 {
@@ -59,6 +92,8 @@ export const formSchemas: FormSchema[] = [ @@ -59,6 +92,8 @@ export const formSchemas: FormSchema[] = [
59 label: '缩放因子', 92 label: '缩放因子',
60 helpMessage: ['缩放因子不能为0,默认为1'], 93 helpMessage: ['缩放因子不能为0,默认为1'],
61 defaultValue: 1, 94 defaultValue: 1,
  95 + ifShow: ({ model }) =>
  96 + ![DataTypeEnum.BOOL, DataTypeEnum.STRING].includes(model[FormFieldsEnum.OBJECT_MODEL_TYPE]),
62 componentProps: { 97 componentProps: {
63 min: 1, 98 min: 1,
64 placeholder: '请输入缩放因子', 99 placeholder: '请输入缩放因子',
@@ -22,13 +22,11 @@ @@ -22,13 +22,11 @@
22 22
23 const emit = defineEmits(['update:value']); 23 const emit = defineEmits(['update:value']);
24 24
25 - const [  
26 - registerForm,  
27 - { setFieldsValue, getFieldsValue, setProps, validate, resetFields, updateSchema },  
28 - ] = useForm({  
29 - schemas: formSchemas,  
30 - showActionButtonGroup: false,  
31 - }); 25 + const [registerForm, { setFieldsValue, getFieldsValue, setProps, validate, resetFields }] =
  26 + useForm({
  27 + schemas: formSchemas,
  28 + showActionButtonGroup: false,
  29 + });
32 30
33 const handleClick = async () => { 31 const handleClick = async () => {
34 show.value = true; 32 show.value = true;
@@ -41,6 +39,7 @@ @@ -41,6 +39,7 @@
41 const handleSubmit = async () => { 39 const handleSubmit = async () => {
42 await validate(); 40 await validate();
43 const value = getFieldsValue(); 41 const value = getFieldsValue();
  42 + Reflect.deleteProperty(value, FormFieldsEnum.OBJECT_MODEL_TYPE);
44 emit('update:value', value); 43 emit('update:value', value);
45 show.value = false; 44 show.value = false;
46 }; 45 };
@@ -48,12 +47,7 @@ @@ -48,12 +47,7 @@
48 watch(show, async (value) => { 47 watch(show, async (value) => {
49 if (value) { 48 if (value) {
50 await nextTick(); 49 await nextTick();
51 - updateSchema([  
52 - {  
53 - field: FormFieldsEnum.ZOOM_FACTOR,  
54 - ifShow: props.dataType == DataTypeEnum.BOOL ? false : true,  
55 - },  
56 - ]); 50 + setFieldsValue({ [FormFieldsEnum.OBJECT_MODEL_TYPE]: props.dataType });
57 } 51 }
58 }); 52 });
59 53
@@ -6,3 +6,23 @@ export enum DataTypeEnum { @@ -6,3 +6,23 @@ export enum DataTypeEnum {
6 BOOL = 'BOOL', 6 BOOL = 'BOOL',
7 ENUM = 'ENUM', 7 ENUM = 'ENUM',
8 } 8 }
  9 +
  10 +export enum RegisterDataTypeEnum {
  11 + UN_SHORT = 'unshort',
  12 +}
  13 +
  14 +export enum RegisterDataTypeNameEnum {
  15 + UN_SHORT = '16位有符号',
  16 +}
  17 +
  18 +export enum RegisterActionTypeEnum {
  19 + BOOL = '05',
  20 + INT = '06',
  21 + DOUBLE = '16',
  22 +}
  23 +
  24 +export enum RegisterActionTypeNameEnum {
  25 + BOOL = '05写入单个线圈寄存器',
  26 + INT = '06写入单个保持寄存器',
  27 + DOUBLE = '16写入多个保持寄存器',
  28 +}