pageUtils.ts 3.64 KB
import { history } from 'umi';

const eq = (source: any, comparison: any) => {
  if (Object.keys(source).length !== Object.keys(comparison).length) {
    return false;
  }

  return (
    JSON.stringify(source) === JSON.stringify({ ...source, ...comparison })
  );
};

/**
 * URL参数设置
 * @param params
 * @param form `import {useTable} from 'table-render'; useTable()`
 */
const urlParamSet = (params: any = {}) => {
  const urlParams = {};
  Object.keys(params).forEach((key: string) => {
    urlParams[key] = params[key]?.toString() ?? '';
  });
  if (eq(history.location.query, urlParams)) {
    return;
  }

  // const historyType = history.location.search ? 'push' : 'replace';
  history['replace']({
    query: urlParams,
  });
};

/**
 * 分页参数名重设
 * `current` -> pageNum
 * @param params
 */
const paginationSet = (params: any) => {
  if (JSON.parse(JSON.stringify(params)).hasOwnProperty('current')) {
    // eslint-disable-next-line no-param-reassign
    params.pageNum = params.current;
    // eslint-disable-next-line no-param-reassign
    delete params.current;
  }
};

export function paramsProcess(
  params: any,
  settings?: any,
  resetQuery: boolean = true,
) {
  let query = { ...params };
  Object.keys(params)
    .filter((key) => params[key] === '')
    .forEach((key) => delete query[key]);
  if (settings && Object.keys(settings).length) {
    Object.keys(params)
      .filter(
        (key) => settings.hasOwnProperty(key) && settings[key].type === 'range',
      )
      .forEach((key) => {
        delete query[key];
        [query[`${key}Start`], query[`${key}End`]] = params[key];
      });
  } else {
    query = params;
  }

  paginationSet(query);
  if (resetQuery) {
    urlParamSet(query);
  }
  return query;
}

export function resetQueryParams(params: any, settings: any) {
  const restParams = {};
  Object.keys(params)
    .filter(
      (key) => key !== 'pageNum' && key !== 'pageSize' && key !== 'current',
    )
    .forEach((key) => {
      let flag = '';
      if (key.endsWith('Start')) {
        flag = 'start';
      } else if (key.endsWith('End')) {
        flag = 'end';
      }

      let propertyKey = key;
      if (!settings.hasOwnProperty(propertyKey)) {
        if (!flag) {
          return;
        }

        propertyKey =
          flag === 'start'
            ? propertyKey.slice(0, -5)
            : propertyKey.slice(0, -3);
        if (!settings.hasOwnProperty(propertyKey)) {
          return;
        }
      }

      const querySetting = settings[propertyKey];
      if (querySetting.type === 'boolean') {
        restParams[propertyKey] = params[key].toString() === 'true';
      } else if (
        querySetting.type === 'number' ||
        querySetting.type === 'integer'
      ) {
        restParams[propertyKey] = parseFloat(params[key].toString());
      } else if (querySetting.type === 'range') {
        const range = restParams.hasOwnProperty(propertyKey)
          ? restParams[propertyKey]
          : ['', ''];
        if (flag === 'start') {
          range[0] = params[key];
        } else {
          range[1] = params[key];
        }

        restParams[propertyKey] = range;
      } else {
        restParams[propertyKey] = params[key];
      }
    });
  return restParams;
}

/**
 * 将 query 重置为默认字段
 */
export function resetToSysFields(query: Record<string, any> = {}) {
  const params = {};
  //重置后需要保留的字段
  const FIXED_FIELDS = [
    '_code',
    'appId',
    'pageSize',
    'viewCode',
    'viewType',
    '_filter',
  ];

  Object.keys(query).forEach((key) => {
    if (FIXED_FIELDS.indexOf(key) > -1 && query[key]) {
      params[key] = query[key];
    }
  });

  return params;
}