_mock.ts 4.15 KB
Newer Older
1
import { parse } from 'url';
陈帅's avatar
陈帅 committed
2
import { TableListItem, TableListParams } from './data';
陈帅's avatar
陈帅 committed
3

4
// mock tableListDataSource
陈帅's avatar
陈帅 committed
5 6 7
let tableListDataSource: TableListItem[] = [];

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

陈帅's avatar
陈帅 committed
28 29 30 31 32 33 34
function getRule(
  req: { url: any },
  res: {
    json: (
      arg0: {
        list: TableListItem[];
        pagination: { total: number; pageSize: number; current: number };
陈帅's avatar
陈帅 committed
35
      },
陈帅's avatar
陈帅 committed
36 37
    ) => void;
  },
陈帅's avatar
陈帅 committed
38
  u: any,
陈帅's avatar
陈帅 committed
39
) {
40 41
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
afc163's avatar
afc163 committed
42
    url = req.url; // eslint-disable-line
43 44
  }

陈帅's avatar
陈帅 committed
45
  const params = (parse(url, true).query as unknown) as TableListParams;
46

陈帅's avatar
陈帅 committed
47
  let dataSource = tableListDataSource;
48 49 50 51 52 53 54 55 56 57 58 59

  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) {
60
    const status = params.status.split(',');
陈帅's avatar
陈帅 committed
61
    let filterDataSource: TableListItem[] = [];
陈帅's avatar
陈帅 committed
62
    status.forEach((s: string) => {
63
      filterDataSource = filterDataSource.concat(
陈帅's avatar
陈帅 committed
64
        dataSource.filter(item => {
陈帅's avatar
陈帅 committed
65
          if (parseInt(`${item.status}`, 10) === parseInt(s.split('')[0], 10)) {
陈帅's avatar
陈帅 committed
66 67 68
            return true;
          }
          return false;
陈帅's avatar
陈帅 committed
69
        }),
70 71 72
      );
    });
    dataSource = filterDataSource;
73 74
  }

ddcat1115's avatar
ddcat1115 committed
75 76
  if (params.name) {
    dataSource = dataSource.filter(data => data.name.indexOf(params.name) > -1);
77 78
  }

陈帅's avatar
陈帅 committed
79
  let pageSize = 10;
80
  if (params.pageSize) {
陈帅's avatar
陈帅 committed
81
    pageSize = parseInt(`${params.pageSize}`, 0);
82 83 84 85 86 87 88
  }

  const result = {
    list: dataSource,
    pagination: {
      total: dataSource.length,
      pageSize,
期贤's avatar
期贤 committed
89
      current: parseInt(`${params.currentPage}`, 10) || 1,
90 91 92
    },
  };

愚道's avatar
愚道 committed
93
  return res.json(result);
94 95
}

陈帅's avatar
陈帅 committed
96 97 98 99
function postRule(
  req: { url: any; body: any },
  res: { json: (arg0: { list: TableListItem[]; pagination: { total: number } }) => void },
  u: any,
陈帅's avatar
陈帅 committed
100
  b: { body: any },
陈帅's avatar
陈帅 committed
101
) {
102 103
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
afc163's avatar
afc163 committed
104
    url = req.url; // eslint-disable-line
105 106 107
  }

  const body = (b && b.body) || req.body;
ddcat1115's avatar
ddcat1115 committed
108
  const { method, name, desc, key } = body;
109 110 111 112

  switch (method) {
    /* eslint no-case-declarations:0 */
    case 'delete':
ddcat1115's avatar
ddcat1115 committed
113
      tableListDataSource = tableListDataSource.filter(item => key.indexOf(item.key) === -1);
114 115 116 117 118 119
      break;
    case 'post':
      const i = Math.ceil(Math.random() * 10000);
      tableListDataSource.unshift({
        key: i,
        href: 'https://ant.design',
jim's avatar
jim committed
120 121 122 123
        avatar: [
          'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
          'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
        ][i % 2],
ddcat1115's avatar
ddcat1115 committed
124
        name: `TradeCode ${i}`,
125 126
        title: `一个任务名称 ${i}`,
        owner: '曲丽丽',
ddcat1115's avatar
ddcat1115 committed
127
        desc,
128 129 130 131 132 133 134
        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
135
    case 'update':
jim's avatar
jim committed
136
      tableListDataSource = tableListDataSource.map(item => {
ddcat1115's avatar
ddcat1115 committed
137
        if (item.key === key) {
陈帅's avatar
陈帅 committed
138 139
          Object.assign(item, { desc, name });
          return item;
ddcat1115's avatar
ddcat1115 committed
140 141 142 143
        }
        return item;
      });
      break;
144 145 146 147
    default:
      break;
  }

148 149 150 151 152 153 154 155
  const result = {
    list: tableListDataSource,
    pagination: {
      total: tableListDataSource.length,
    },
  };

  return res.json(result);
156 157 158
}

export default {
陈帅's avatar
陈帅 committed
159 160
  'GET /api/rule': getRule,
  'POST /api/rule': postRule,
161
};