|
| 1 | +import { Request, Response } from 'express'; |
| 2 | +import moment from 'moment'; |
| 3 | +import { parse } from 'url'; |
| 4 | + |
| 5 | +// mock tableListDataSource |
| 6 | +const genList = (current: number, pageSize: number) => { |
| 7 | + const tableListDataSource: API.RuleListItem[] = []; |
| 8 | + |
| 9 | + for (let i = 0; i < pageSize; i += 1) { |
| 10 | + const index = (current - 1) * 10 + i; |
| 11 | + tableListDataSource.push({ |
| 12 | + key: index, |
| 13 | + disabled: i % 6 === 0, |
| 14 | + href: 'https://ant.design', |
| 15 | + avatar: [ |
| 16 | + 'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png', |
| 17 | + 'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png', |
| 18 | + ][i % 2], |
| 19 | + name: `TradeCode ${index}`, |
| 20 | + owner: '曲丽丽', |
| 21 | + desc: '这是一段描述', |
| 22 | + callNo: Math.floor(Math.random() * 1000), |
| 23 | + status: Math.floor(Math.random() * 10) % 4, |
| 24 | + updatedAt: moment().format('YYYY-MM-DD'), |
| 25 | + createdAt: moment().format('YYYY-MM-DD'), |
| 26 | + progress: Math.ceil(Math.random() * 100), |
| 27 | + }); |
| 28 | + } |
| 29 | + tableListDataSource.reverse(); |
| 30 | + return tableListDataSource; |
| 31 | +}; |
| 32 | + |
| 33 | +let tableListDataSource = genList(1, 100); |
| 34 | + |
| 35 | +function getRule(req: Request, res: Response, u: string) { |
| 36 | + let realUrl = u; |
| 37 | + if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') { |
| 38 | + realUrl = req.url; |
| 39 | + } |
| 40 | + const { current = 1, pageSize = 10 } = req.query; |
| 41 | + const params = parse(realUrl, true).query as unknown as API.PageParams & |
| 42 | + API.RuleListItem & { |
| 43 | + sorter: any; |
| 44 | + filter: any; |
| 45 | + }; |
| 46 | + |
| 47 | + let dataSource = [...tableListDataSource].slice( |
| 48 | + ((current as number) - 1) * (pageSize as number), |
| 49 | + (current as number) * (pageSize as number), |
| 50 | + ); |
| 51 | + if (params.sorter) { |
| 52 | + const sorter = JSON.parse(params.sorter); |
| 53 | + dataSource = dataSource.sort((prev, next) => { |
| 54 | + let sortNumber = 0; |
| 55 | + Object.keys(sorter).forEach((key) => { |
| 56 | + if (sorter[key] === 'descend') { |
| 57 | + if (prev[key] - next[key] > 0) { |
| 58 | + sortNumber += -1; |
| 59 | + } else { |
| 60 | + sortNumber += 1; |
| 61 | + } |
| 62 | + return; |
| 63 | + } |
| 64 | + if (prev[key] - next[key] > 0) { |
| 65 | + sortNumber += 1; |
| 66 | + } else { |
| 67 | + sortNumber += -1; |
| 68 | + } |
| 69 | + }); |
| 70 | + return sortNumber; |
| 71 | + }); |
| 72 | + } |
| 73 | + if (params.filter) { |
| 74 | + const filter = JSON.parse(params.filter as any) as { |
| 75 | + [key: string]: string[]; |
| 76 | + }; |
| 77 | + if (Object.keys(filter).length > 0) { |
| 78 | + dataSource = dataSource.filter((item) => { |
| 79 | + return Object.keys(filter).some((key) => { |
| 80 | + if (!filter[key]) { |
| 81 | + return true; |
| 82 | + } |
| 83 | + if (filter[key].includes(`${item[key]}`)) { |
| 84 | + return true; |
| 85 | + } |
| 86 | + return false; |
| 87 | + }); |
| 88 | + }); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if (params.name) { |
| 93 | + dataSource = dataSource.filter((data) => data?.name?.includes(params.name || '')); |
| 94 | + } |
| 95 | + const result = { |
| 96 | + data: dataSource, |
| 97 | + total: tableListDataSource.length, |
| 98 | + success: true, |
| 99 | + pageSize, |
| 100 | + current: parseInt(`${params.current}`, 10) || 1, |
| 101 | + }; |
| 102 | + |
| 103 | + return res.json(result); |
| 104 | +} |
| 105 | + |
| 106 | +function postRule(req: Request, res: Response, u: string, b: Request) { |
| 107 | + let realUrl = u; |
| 108 | + if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') { |
| 109 | + realUrl = req.url; |
| 110 | + } |
| 111 | + |
| 112 | + const body = (b && b.body) || req.body; |
| 113 | + const { method, name, desc, key } = body; |
| 114 | + |
| 115 | + switch (method) { |
| 116 | + /* eslint no-case-declarations:0 */ |
| 117 | + case 'delete': |
| 118 | + tableListDataSource = tableListDataSource.filter((item) => key.indexOf(item.key) === -1); |
| 119 | + break; |
| 120 | + case 'post': |
| 121 | + (() => { |
| 122 | + const i = Math.ceil(Math.random() * 10000); |
| 123 | + const newRule: API.RuleListItem = { |
| 124 | + key: tableListDataSource.length, |
| 125 | + href: 'https://ant.design', |
| 126 | + avatar: [ |
| 127 | + 'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png', |
| 128 | + 'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png', |
| 129 | + ][i % 2], |
| 130 | + name, |
| 131 | + owner: '曲丽丽', |
| 132 | + desc, |
| 133 | + callNo: Math.floor(Math.random() * 1000), |
| 134 | + status: Math.floor(Math.random() * 10) % 2, |
| 135 | + updatedAt: moment().format('YYYY-MM-DD'), |
| 136 | + createdAt: moment().format('YYYY-MM-DD'), |
| 137 | + progress: Math.ceil(Math.random() * 100), |
| 138 | + }; |
| 139 | + tableListDataSource.unshift(newRule); |
| 140 | + return res.json(newRule); |
| 141 | + })(); |
| 142 | + return; |
| 143 | + |
| 144 | + case 'update': |
| 145 | + (() => { |
| 146 | + let newRule = {}; |
| 147 | + tableListDataSource = tableListDataSource.map((item) => { |
| 148 | + if (item.key === key) { |
| 149 | + newRule = { ...item, desc, name }; |
| 150 | + return { ...item, desc, name }; |
| 151 | + } |
| 152 | + return item; |
| 153 | + }); |
| 154 | + return res.json(newRule); |
| 155 | + })(); |
| 156 | + return; |
| 157 | + default: |
| 158 | + break; |
| 159 | + } |
| 160 | + |
| 161 | + const result = { |
| 162 | + list: tableListDataSource, |
| 163 | + pagination: { |
| 164 | + total: tableListDataSource.length, |
| 165 | + }, |
| 166 | + }; |
| 167 | + |
| 168 | + res.json(result); |
| 169 | +} |
| 170 | + |
| 171 | +export default { |
| 172 | + 'GET /api/rule': getRule, |
| 173 | + 'POST /api/rule': postRule, |
| 174 | +}; |
0 commit comments