rule.js 3.5 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,
J
jim 已提交
8
    disabled: i % 6 === 0,
9
    href: 'https://ant.design',
J
jim 已提交
10 11 12 13
    avatar: [
      'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
      'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
    ][i % 2],
D
ddcat1115 已提交
14
    name: `TradeCode ${i}`,
15 16
    title: `一个任务名称 ${i}`,
    owner: '曲丽丽',
D
ddcat1115 已提交
17
    desc: '这是一段描述',
18
    callNo: Math.floor(Math.random() * 1000),
N
nikogu 已提交
19
    status: Math.floor(Math.random() * 10) % 4,
A
afc163 已提交
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
    progress: Math.ceil(Math.random() * 100),
  });
}

愚道 已提交
26
function getRule(req, res, u) {
27 28
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
A
afc163 已提交
29
    url = req.url; // eslint-disable-line
30 31
  }

32
  const params = parse(url, true).query;
33

陈帅 已提交
34
  let dataSource = tableListDataSource;
35 36 37 38 39 40 41 42 43 44 45 46

  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 = [];
J
jim 已提交
49
    status.forEach(s => {
50
      filterDataSource = filterDataSource.concat(
陈帅 已提交
51
        dataSource.filter(data => parseInt(data.status, 10) === parseInt(s[0], 10))
52 53 54
      );
    });
    dataSource = filterDataSource;
55 56
  }

D
ddcat1115 已提交
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
  }

  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,
    },
  };

愚道 已提交
75
  return res.json(result);
76 77
}

愚道 已提交
78
function postRule(req, res, u, b) {
79 80
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
A
afc163 已提交
81
    url = req.url; // eslint-disable-line
82 83 84
  }

  const body = (b && b.body) || req.body;
D
ddcat1115 已提交
85
  const { method, name, desc, key } = body;
86 87 88 89

  switch (method) {
    /* eslint no-case-declarations:0 */
    case 'delete':
D
ddcat1115 已提交
90
      tableListDataSource = tableListDataSource.filter(item => key.indexOf(item.key) === -1);
91 92 93 94 95 96
      break;
    case 'post':
      const i = Math.ceil(Math.random() * 10000);
      tableListDataSource.unshift({
        key: i,
        href: 'https://ant.design',
J
jim 已提交
97 98 99 100
        avatar: [
          'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
          'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
        ][i % 2],
D
ddcat1115 已提交
101
        name: `TradeCode ${i}`,
102 103
        title: `一个任务名称 ${i}`,
        owner: '曲丽丽',
D
ddcat1115 已提交
104
        desc,
105 106 107 108 109 110 111
        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;
D
ddcat1115 已提交
112
    case 'update':
J
jim 已提交
113
      tableListDataSource = tableListDataSource.map(item => {
D
ddcat1115 已提交
114
        if (item.key === key) {
陈帅 已提交
115 116
          Object.assign(item, { desc, name });
          return item;
D
ddcat1115 已提交
117 118 119 120
        }
        return item;
      });
      break;
121 122 123 124
    default:
      break;
  }

125
  return getRule(req, res, u);
126 127 128
}

export default {
愚道 已提交
129 130
  'GET /api/rule': getRule,
  'POST /api/rule': postRule,
131
};