_mock.ts 4.18 KB
Newer Older
1
import { parse } from 'url';
陈帅's avatar
陈帅 committed
2
import { TableListItem, TableListParams } from './data.d';
陈帅'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]') {
陈帅's avatar
陈帅 committed
42 43
    // eslint-disable-next-line prefer-destructuring
    url = req.url;
44 45
  }

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

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

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

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

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

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

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

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

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

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

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

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

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