packageDetail.config.ts 6.53 KB
import { getDeviceProfileInfos } from '/@/api/ota';

import { FormSchema } from '/@/components/Form';
import { FileItem } from '/@/components/Form/src/components/ApiUpload.vue';
import { useI18n } from '/@/hooks/web/useI18n';
import { createPickerSearch } from '/@/utils/pickerSearch';

export enum PackageField {
  TITLE = 'title',
  VERSION = 'version',
  VERSION_TAG = 'tag',
  PACKAGE_TYPE = 'type',
  URL = 'url',
  IS_URL = 'isUrl',
  CHECK_SUM_ALG = 'checksumAlgorithm',
  CHECK_SUM = 'checksum',
  DESCRIPTION = 'description',
  DEVICE_PROFILE_INFO = 'deviceProfileId',

  CREATE_TIME = 'createdTime',
  FILE_SIZE = 'dataSize',
  FILE_NAME = 'fileName',
  ADDITIONAL_INFO = 'additionalInfo',
  FILE_TYPE = 'contentType',

  PACKAGE_BINARY_FILE = 'fileList',
  VALIDATE_WAY = 'validateWay',
}

export enum PackageType {
  FIRMWARE = 'FIRMWARE',
  SOFTWARE = 'SOFTWARE',
}

export enum CheckSumWay {
  AUTO = 'auto',
  MANUAL = 'manual',
}

export enum ALG {
  MD5 = 'MD$',
  SHA_256 = 'SHA256',
  SHA_384 = 'SHA384',
  SHA_512 = 'SHA512',
  CRC_32 = 'CRC32',
  MURMUR3_32 = 'MURMUR332',
  MURMUR3_128 = 'MURMUR3128',
}

const getVersionTag = (title: string, version: string) => {
  return `${title ?? ''} ${version ?? ''}`;
};

const { t } = useI18n();

export const formSchema: FormSchema[] = [
  {
    field: PackageField.TITLE,
    label: t('monitor.ota.title'),
    component: 'Input',
    rules: [{ required: true }],
    componentProps: ({ formActionType, formModel }) => {
      const { setFieldsValue } = formActionType;
      return {
        maxLength: 36,
        onChange: (value: Event) => {
          setFieldsValue({
            [PackageField.VERSION_TAG]: getVersionTag(
              (value.target as HTMLInputElement).value,
              formModel[PackageField.VERSION]
            ),
          });
        },
      };
    },
  },
  {
    field: PackageField.VERSION,
    label: t('monitor.ota.version'),
    component: 'Input',
    rules: [{ required: true }],
    componentProps: ({ formActionType, formModel }) => {
      const { setFieldsValue } = formActionType;
      return {
        maxLength: 36,
        onChange: (value: Event) => {
          setFieldsValue({
            [PackageField.VERSION_TAG]: getVersionTag(
              formModel[PackageField.TITLE],
              (value.target as HTMLInputElement).value
            ),
          });
        },
      };
    },
  },
  {
    field: PackageField.VERSION_TAG,
    label: t('monitor.ota.versionLabel'),
    component: 'Input',
    helpMessage: [t('monitor.ota.modal.versionLabelMessage')],
    componentProps: () => {
      return {
        maxLength: 36,
      };
    },
  },
  {
    field: PackageField.DEVICE_PROFILE_INFO,
    label: t('business.affiliatedProductText'),
    component: 'ApiSelectScrollLoad',
    helpMessage: [t('monitor.ota.modal.productMessage')],
    rules: [{ required: true }],
    defaultValue: 'default',
    componentProps: () => {
      return {
        labelField: 'name',
        valueField: 'id',
        pagination: { page: 0, pageSize: 10 },
        api: async (params: Recordable) => {
          const { totalElements, data } = await getDeviceProfileInfos(params);
          const dataList = data.map((item) => ({
            ...item,
            id: item.id.id,
          }));
          return { items: dataList, total: totalElements };
        },
        ...createPickerSearch(),
      };
    },
  },
  {
    field: PackageField.PACKAGE_TYPE,
    label: t('monitor.ota.modal.packType'),
    component: 'Select',
    helpMessage: [t('monitor.ota.modal.packMessage')],
    defaultValue: PackageType.FIRMWARE,
    rules: [{ required: true }],
    componentProps: () => {
      return {
        options: [
          { label: t('monitor.ota.firm'), value: PackageType.FIRMWARE },
          { label: t('monitor.ota.soft'), value: PackageType.SOFTWARE },
        ],
      };
    },
  },
  {
    field: PackageField.IS_URL,
    label: t('monitor.ota.modal.uploadMethod'),
    component: 'RadioGroup',
    defaultValue: false,
    componentProps: () => {
      return {
        defaultValue: false,
        options: [
          { label: t('monitor.ota.modal.uploadFiles'), value: false },
          { label: t('monitor.ota.modal.usingURL'), value: true },
        ],
      };
    },
  },
  {
    field: PackageField.PACKAGE_BINARY_FILE,
    label: t('monitor.ota.modal.binaryFiles'),
    ifShow: ({ model }) => {
      return !model[PackageField.IS_URL];
    },
    component: 'ApiUpload',
    valueField: PackageField.PACKAGE_BINARY_FILE,
    changeEvent: `update:${PackageField.PACKAGE_BINARY_FILE}`,
    rules: [
      {
        required: true,
        message: t('monitor.ota.modal.place') + t('monitor.ota.modal.binaryFiles'),
        type: 'array',
      },
    ],
    componentProps: {
      maxFileLimit: 1,
      api: (file: FileItem) => {
        return { uid: file.uid, name: file.name, file };
      },
    },
  },
  {
    field: PackageField.URL,
    label: t('monitor.ota.modal.externalUrl'),
    component: 'Input',
    ifShow: ({ model }) => {
      return model[PackageField.IS_URL];
    },
    rules: [{ required: true }],
    componentProps: {
      maxLength: 36,
    },
  },
  {
    field: PackageField.VALIDATE_WAY,
    label: t('monitor.ota.modal.rule'),
    component: 'RadioGroup',
    defaultValue: CheckSumWay.AUTO,
    ifShow: ({ model }) => {
      return !model[PackageField.IS_URL];
    },
    componentProps: () => {
      return {
        options: [
          { label: t('monitor.ota.modal.auto'), value: CheckSumWay.AUTO },
          { label: t('monitor.ota.modal.manual'), value: CheckSumWay.MANUAL },
        ],
      };
    },
  },
  {
    field: PackageField.CHECK_SUM_ALG,
    label: t('monitor.ota.modal.checksum'),
    component: 'Select',
    ifShow: ({ model }) => {
      return model[PackageField.VALIDATE_WAY] === CheckSumWay.MANUAL && !model[PackageField.IS_URL];
    },
    defaultValue: ALG.SHA_256,
    componentProps: {
      options: Object.keys(ALG).map((key) => {
        return {
          label: String(ALG[key]).toUpperCase(),
          value: ALG[key],
        };
      }),
    },
  },
  {
    field: PackageField.CHECK_SUM,
    label: t('monitor.ota.checksum'),
    component: 'Input',
    ifShow: ({ model }) => {
      return model[PackageField.VALIDATE_WAY] === CheckSumWay.MANUAL && !model[PackageField.IS_URL];
    },
    helpMessage: [t('monitor.ota.modal.checksumMessage')],
    componentProps: {
      maxLength: 36,
    },
  },
  {
    field: PackageField.DESCRIPTION,
    label: t('common.descText'),
    component: 'InputTextArea',
    componentProps: {
      maxLength: 255,
    },
  },
];