_mock.ts 4.16 KB
Newer Older
1
import { parse } from 'url';
陈帅's avatar
陈帅 committed
2
import { TableListItem, TableListParams } from './data';
3
// mock tableListDataSource
陈帅's avatar
陈帅 committed
4 5 6
let tableListDataSource: TableListItem[] = [];

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

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

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

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

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

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

陈帅's avatar
陈帅 committed
78
  let pageSize: number = 10;
79
  if (params.pageSize) {
陈帅's avatar
陈帅 committed
80
    pageSize = parseInt(params.pageSize + '', 0);
81 82 83 84 85 86 87 88 89 90 91
  }

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

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

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

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

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

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

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

export default {
158 159
  'GET /api/BLOCK_NAME': getRule,
  'POST /api/BLOCK_NAME': postRule,
160
};