publicHandle.js 3.02 KB
import { message } from 'antd';

// import { useIntl } from 'umi';
// const { formatMessage } = useIntl();

export const dispatchHandle = (dispatch, type, payload = {}, callback, errCallback) => {
  dispatch({
    type,
    payload,
    callback: (data, res) => {
      if (callback) {
        if (typeof callback === 'function') callback(data, res);
        if (callback === 'success') message.success('操作成功!');
      }
    },
    errCallback: () => {
      if (errCallback) {
        if (typeof errCallback === 'function') errCallback();
      }
    },
  });
};

// dispatchHandle

export const FormReset = (that, callback) => {
  const { form } = that.props;
  form.resetFields();
  if (callback && typeof callback === 'function') {
    callback();
  }
};

export const HandleSearch = (that, opts = {}) => {
  const { form } = that.props;
  const { formValues } = that.state;
  const { handle, callback } = opts;

  form.validateFields((err, fieldsValue) => {
    if (err) return;

    const myValues = {};

    if (handle && typeof handle === 'function') {
      handle(fieldsValue);
    }

    for (let x in fieldsValue) {
      if (fieldsValue[x] === null) delete fieldsValue[x]
    }

    const values = {
      // ...formValues,
      ...fieldsValue,
    };

    that.setState({
      formValues: { ...myValues, ...values },
    });

    that.getListItems(values);

  });
};

export const HandleTableChange = (that, pagination) => {
  const { formValues } = that.state;
  const params = {
    ...formValues,
    pageNumber: pagination.current,
    pageSize: pagination.pageSize,
  };
  that.setState({
    formValues: params,
  });
  that.getListItems(params);
};


// 编辑页面方法
export const SetIsEditState = (that, param = {}) => {
  const { isEdit, type, payload } = param;
  that.setState({
    isEdit: isEdit || false,
  });
  type && dispatchHandle(that, type, payload);
}

export const HandleSubmit = (that, type, opts = {}) => {
  const { form } = that.props;
  const { handle, callback } = opts;
  form.validateFields((err, fieldsValue) => {
    if (err) return;
    if (handle && typeof handle === 'function') {
      handle(fieldsValue);
    }
    dispatchHandle(that, type, fieldsValue, () => {
      // message.success(formatMessage({ id: 'app.msg.message.success' }));
      if (callback && typeof callback === 'function') {
        callback();
      }
    });

  });
};
/* 表格数据格式处理 new add by peter */
export const HandleResultList = (result = []) => {
  let dataSource = [];
  if (Array.isArray(result) && result.length > 0) {
      //数组列表
      dataSource = [...result];
      dataSource.map((item, index) => {
          item.key = item.id;
      })
  } else if (Array.isArray(result) && result.length == 0) {
      //空数组
      dataSource = [];
  } else if (result && (Object.keys(result).length > 0)) {
      //单一object,通过keyword查询出来的
      dataSource.push(result);
      dataSource.map((item, index) => {
          item.key = item.id;
      })
  }
  return dataSource;
 // console.log(dataSource);
}