rule.js 3.64 KB
Newer Older
1
import { parse } from 'url';
2 3 4 5 6 7

// mock tableListDataSource
let tableListDataSource = [];
for (let i = 0; i < 46; i += 1) {
  tableListDataSource.push({
    key: i,
jim's avatar
jim committed
8
    disabled: i % 6 === 0,
9
    href: 'https://ant.design',
jim's avatar
jim committed
10 11 12 13
    avatar: [
      'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
      'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
    ][i % 2],
ddcat1115's avatar
ddcat1115 committed
14
    name: `TradeCode ${i}`,
15 16
    title: `一个任务名称 ${i}`,
    owner: '曲丽丽',
ddcat1115's avatar
ddcat1115 committed
17
    desc: '这是一段描述',
18
    callNo: Math.floor(Math.random() * 1000),
nikogu's avatar
nikogu committed
19
    status: Math.floor(Math.random() * 10) % 4,
afc163's avatar
afc163 committed
20 21
    updatedAt: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
    createdAt: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
22 23 24 25 26 27 28
    progress: Math.ceil(Math.random() * 100),
  });
}

export function getRule(req, res, u) {
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
afc163's avatar
afc163 committed
29
    url = req.url; // eslint-disable-line
30 31
  }

32
  const params = parse(url, true).query;
33 34 35 36 37 38 39 40 41 42 43 44 45 46

  let dataSource = [...tableListDataSource];

  if (params.sorter) {
    const s = params.sorter.split('_');
    dataSource = dataSource.sort((prev, next) => {
      if (s[1] === 'descend') {
        return next[s[0]] - prev[s[0]];
      }
      return prev[s[0]] - next[s[0]];
    });
  }

  if (params.status) {
47 48
    const status = params.status.split(',');
    let filterDataSource = [];
jim's avatar
jim committed
49
    status.forEach(s => {
50 51 52 53 54
      filterDataSource = filterDataSource.concat(
        [...dataSource].filter(data => parseInt(data.status, 10) === parseInt(s[0], 10))
      );
    });
    dataSource = filterDataSource;
55 56
  }

ddcat1115's avatar
ddcat1115 committed
57 58
  if (params.name) {
    dataSource = dataSource.filter(data => data.name.indexOf(params.name) > -1);
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  }

  let pageSize = 10;
  if (params.pageSize) {
    pageSize = params.pageSize * 1;
  }

  const result = {
    list: dataSource,
    pagination: {
      total: dataSource.length,
      pageSize,
      current: parseInt(params.currentPage, 10) || 1,
    },
  };

  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }
}

export function postRule(req, res, u, b) {
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
afc163's avatar
afc163 committed
85
    url = req.url; // eslint-disable-line
86 87 88
  }

  const body = (b && b.body) || req.body;
ddcat1115's avatar
ddcat1115 committed
89
  const { method, name, desc, key } = body;
90 91 92 93

  switch (method) {
    /* eslint no-case-declarations:0 */
    case 'delete':
ddcat1115's avatar
ddcat1115 committed
94
      tableListDataSource = tableListDataSource.filter(item => key.indexOf(item.key) === -1);
95 96 97 98 99 100
      break;
    case 'post':
      const i = Math.ceil(Math.random() * 10000);
      tableListDataSource.unshift({
        key: i,
        href: 'https://ant.design',
jim's avatar
jim committed
101 102 103 104
        avatar: [
          'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
          'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
        ][i % 2],
ddcat1115's avatar
ddcat1115 committed
105
        name: `TradeCode ${i}`,
106 107
        title: `一个任务名称 ${i}`,
        owner: '曲丽丽',
ddcat1115's avatar
ddcat1115 committed
108
        desc,
109 110 111 112 113 114 115
        callNo: Math.floor(Math.random() * 1000),
        status: Math.floor(Math.random() * 10) % 2,
        updatedAt: new Date(),
        createdAt: new Date(),
        progress: Math.ceil(Math.random() * 100),
      });
      break;
ddcat1115's avatar
ddcat1115 committed
116
    case 'update':
jim's avatar
jim committed
117
      tableListDataSource = tableListDataSource.map(item => {
ddcat1115's avatar
ddcat1115 committed
118 119 120 121 122 123
        if (item.key === key) {
          return { ...item, desc, name };
        }
        return item;
      });
      break;
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    default:
      break;
  }

  const result = {
    list: tableListDataSource,
    pagination: {
      total: tableListDataSource.length,
    },
  };

  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }
}

export default {
  getRule,
  postRule,
};