86133 | aaa3f5d | 2025-04-20 21:33:29 +0800 | [diff] [blame] | 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) as Array<keyof API.RuleListItem>).forEach((key) => { |
| 56 | let nextSort = next?.[key] as number; |
| 57 | let preSort = prev?.[key] as number; |
| 58 | if (sorter[key] === 'descend') { |
| 59 | if (preSort - nextSort > 0) { |
| 60 | sortNumber += -1; |
| 61 | } else { |
| 62 | sortNumber += 1; |
| 63 | } |
| 64 | return; |
| 65 | } |
| 66 | if (preSort - nextSort > 0) { |
| 67 | sortNumber += 1; |
| 68 | } else { |
| 69 | sortNumber += -1; |
| 70 | } |
| 71 | }); |
| 72 | return sortNumber; |
| 73 | }); |
| 74 | } |
| 75 | if (params.filter) { |
| 76 | const filter = JSON.parse(params.filter as any) as { |
| 77 | [key: string]: string[]; |
| 78 | }; |
| 79 | if (Object.keys(filter).length > 0) { |
| 80 | dataSource = dataSource.filter((item) => { |
| 81 | return (Object.keys(filter) as Array<keyof API.RuleListItem>).some((key) => { |
| 82 | if (!filter[key]) { |
| 83 | return true; |
| 84 | } |
| 85 | if (filter[key].includes(`${item[key]}`)) { |
| 86 | return true; |
| 87 | } |
| 88 | return false; |
| 89 | }); |
| 90 | }); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if (params.name) { |
| 95 | dataSource = dataSource.filter((data) => data?.name?.includes(params.name || '')); |
| 96 | } |
| 97 | const result = { |
| 98 | data: dataSource, |
| 99 | total: tableListDataSource.length, |
| 100 | success: true, |
| 101 | pageSize, |
| 102 | current: parseInt(`${params.current}`, 10) || 1, |
| 103 | }; |
| 104 | |
| 105 | return res.json(result); |
| 106 | } |
| 107 | |
| 108 | function postRule(req: Request, res: Response, u: string, b: Request) { |
| 109 | let realUrl = u; |
| 110 | if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') { |
| 111 | realUrl = req.url; |
| 112 | } |
| 113 | |
| 114 | const body = (b && b.body) || req.body; |
| 115 | const { method, name, desc, key } = body; |
| 116 | |
| 117 | switch (method) { |
| 118 | /* eslint no-case-declarations:0 */ |
| 119 | case 'delete': |
| 120 | tableListDataSource = tableListDataSource.filter((item) => key.indexOf(item.key) === -1); |
| 121 | break; |
| 122 | case 'post': |
| 123 | (() => { |
| 124 | const i = Math.ceil(Math.random() * 10000); |
| 125 | const newRule: API.RuleListItem = { |
| 126 | key: tableListDataSource.length, |
| 127 | href: 'https://ant.design', |
| 128 | avatar: [ |
| 129 | 'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png', |
| 130 | 'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png', |
| 131 | ][i % 2], |
| 132 | name, |
| 133 | owner: '曲丽丽', |
| 134 | desc, |
| 135 | callNo: Math.floor(Math.random() * 1000), |
| 136 | status: Math.floor(Math.random() * 10) % 2, |
| 137 | updatedAt: moment().format('YYYY-MM-DD'), |
| 138 | createdAt: moment().format('YYYY-MM-DD'), |
| 139 | progress: Math.ceil(Math.random() * 100), |
| 140 | }; |
| 141 | tableListDataSource.unshift(newRule); |
| 142 | return res.json(newRule); |
| 143 | })(); |
| 144 | return; |
| 145 | |
| 146 | case 'update': |
| 147 | (() => { |
| 148 | let newRule = {}; |
| 149 | tableListDataSource = tableListDataSource.map((item) => { |
| 150 | if (item.key === key) { |
| 151 | newRule = { ...item, desc, name }; |
| 152 | return { ...item, desc, name }; |
| 153 | } |
| 154 | return item; |
| 155 | }); |
| 156 | return res.json(newRule); |
| 157 | })(); |
| 158 | return; |
| 159 | default: |
| 160 | break; |
| 161 | } |
| 162 | |
| 163 | const result = { |
| 164 | list: tableListDataSource, |
| 165 | pagination: { |
| 166 | total: tableListDataSource.length, |
| 167 | }, |
| 168 | }; |
| 169 | |
| 170 | res.json(result); |
| 171 | } |
| 172 | |
| 173 | export default { |
| 174 | 'GET /api/rule': getRule, |
| 175 | 'POST /api/rule': postRule, |
| 176 | }; |