getInfoGenerator.js 1.3 KB
Newer Older
王绍森's avatar
王绍森 committed
1 2 3 4 5 6 7
import { getInfo } from '@/highOrderComponent/Service';

export const DataType = {
  string: 'string',
  int: 'int',
  long: 'long',
  text: 'text',
王绍森's avatar
王绍森 committed
8
  date: 'date',
王绍森's avatar
王绍森 committed
9 10
};

11
export function parseParams(condition, params) {
王绍森's avatar
王绍森 committed
12 13 14 15 16 17 18 19 20
    // 有dataType的查询条件封装到queryInfo里,其他的直接按参数查询
    // queryInfo是{key, value, dataType}对象组成的数组

    const newParams = Object.keys(params).reduce((acc, key) => {
      const value = params[key];
      const conditionItemHasDataType = condition.find(
        item => item.key === key && typeof item.dataType !== 'undefined'
      );

王绍森's avatar
王绍森 committed
21
      if (conditionItemHasDataType && typeof value !== 'undefined') {
王绍森's avatar
王绍森 committed
22 23 24 25 26 27 28
        const oldQueryInfo = acc.queryInfo || [];
        return {
          ...acc,
          queryInfo: oldQueryInfo.concat({
            key,
            value,
            dataType: conditionItemHasDataType.dataType,
王绍森's avatar
王绍森 committed
29
            op: conditionItemHasDataType.op || 'eq',
王绍森's avatar
王绍森 committed
30 31 32 33 34 35 36
          }),
        };
      }
      return { ...acc, [key]: value };
    }, {});

    newParams.queryInfo = JSON.stringify(newParams.queryInfo || []);
37 38 39 40 41 42
    return newParams;
}

export default function getInfoGenerator(condition) {
  return (params, url) => {
    const newParams = parseParams(condition, params);
王绍森's avatar
王绍森 committed
43 44 45
    return getInfo(newParams, url, { method: 'GET' });
  };
}