util.tsx 4.74 KB
import { handleWindowOpen } from '@/utils';
import _ from 'lodash';

const TARGET_MAP = {
  BLANK: '_blank',
  SELF: '_self',
};
export const doBtnJump = (btnSchema, record, params) => {
  const { url = '', openType = 'BLANK' } = btnSchema && btnSchema.link;
  const target = TARGET_MAP[openType];
  if (!record) {
    if (target && url) {
      handleWindowOpen(url, target);
    }
    return;
  }
  /*  const paramsArr: string[] = [];
  params.forEach((param: any) => {
    if (!param.value) {
      return;
    }
    const {type, value} = param.value
    let paramValue = value;
    if (type === 'FIELD') {
      const dataValue = record[value];
      paramValue = Array.isArray(dataValue) ? dataValue.join(',') : dataValue
    }
    paramsArr.push(`${param.name}=${paramValue}`)
  })*/

  let resultUrl = '';
  const index = url.indexOf('?');

  if (index > -1) {
    const urlPrefix = url.substr(0, index);
    const urlSuffix = url.substr(index + 1);
    resultUrl = urlPrefix + '?' + params.join('&') + '&' + urlSuffix;
  } else {
    resultUrl = url + '?' + params.join('&');
  }

  if (target) {
    handleWindowOpen(resultUrl, target);
  }
};

export const handleBtnFormSchema = (schema: API.Schema) => {
  const properties = schema.properties || {};
  const values = {};
  const keys = Object.keys(properties);
  let hiddenAll = true;
  for (let i = 0; i < keys.length; i++) {
    const key = keys[i];
    const property = properties[key];
    if (!property.hidden) {
      hiddenAll = false;
    }
    if (property.qxProps && property.qxProps.defaultConfig) {
      const defaultConfig = property.qxProps.defaultConfig;

      if (property.type === 'array') {
        values[key] = [];
        (defaultConfig.values || []).forEach((item) => {
          //TODO
          if (item.type) {
          } else {
            values[key].push(item.value);
          }
        });
      } else {
        (defaultConfig.values || []).forEach((item) => {
          //TODO
          if (item.type) {
          } else {
            values[key] = item.value;
          }
        });
      }
    }
  }
  return {
    hiddenAll,
    values,
  };
};

export const getSchemaByType = (obj = {}) => {
  const SCHEMA = {
    DATE: {
      type: 'string',
      widget: 'qxDatetime',
      placeholder: '请输入日期',
      qxProps: {
        format: 'YEAR_SEC',
        dateFormat: 'YEAR_SEC',
        limitRange: false,
      },
      format: 'YYYY-MM-DD HH:mm:ss',
      fieldName: 'date_mlkqdo',
      inEffect: true,
      width: '50%',
    },
    TEXT: {
      type: 'string',
      width: '50%',
      widget: 'qxInput',
      max: 100,
      placeholder: '请输入',
      required: true,
      qxProps: {
        limitRange: false,
      },
      inEffect: true,
      fieldName: 'title',
    },
    NUM: {
      type: 'number',
      widget: 'qxNumber',
      width: '50%',
      qxProps: {
        unitPosition: 'after',
      },
      props: {
        privacy: false,
        precision: 0,
      },
    },
    USER: {
      type: 'array',
      widget: 'userSelector',
      items: {
        type: 'string',
      },
      qxProps: {
        mode: 'SINGLE',
        auth: 'JOIN',
      },
      max: 1,
      width: '50%',
    },
    ORG: {
      type: 'array',
      widget: 'orgSelector',
      items: {
        type: 'string',
      },
      max: 1,
      qxProps: {
        mode: 'SINGLE',
      },
      width: '50%',
    },
    BOOL: {
      type: 'boolean',
      width: '50%',
      widget: 'qxSwitch',
      required: true,
      qxProps: {},
    },
  };
  const type = obj.type || 'TEXT';

  const { hidden, required, readOnly, name, field, values } = obj;
  const _schema = {
    ...SCHEMA[type],
    hidden,
    required,
    readOnly,
    fieldName: field,
    propertyKey: field,
  };
  _schema.title = name;
  _schema['qxProps']['defaultConfig'] = {
    type: 'CUSTOM',
    values: values || [],
  };
  return _schema;
};

export const BUTTON_PARAMS_KEY = '_fParams';

const loopDeleteSchema = (schema: any) => {
  const properties = schema.properties;
  if (properties) {
    Object.keys(properties).forEach((item) => {
      if (properties[item].properties) {
        loopDeleteSchema(properties[item]);
      } else if (properties[item].auth === false) {
        delete properties[item];
      }
    });
  }
};

export const createParamSchema = (schema = {}) => {
  loopDeleteSchema(schema);
  const schemaObj = _.cloneDeep(schema);
  const paramsArr = schemaObj.editParams;
  if (!paramsArr || paramsArr.length <= 0) return schema;

  const properties = {};
  paramsArr.forEach((param: object) => {
    properties[param.field] = getSchemaByType(param);
    properties[param.field].bind = BUTTON_PARAMS_KEY + '.' + param.field;
  });

  schemaObj.properties = { ...schemaObj.properties, ...properties };

  return schemaObj;
};