import { Request, Response } from 'express'; import { parse } from 'url'; import { TableListItem, TableListParams } from '../src/pages/negotiation/find/data.d'; const genList = (current: number, pageSize: number) => { const tableListDataSource: TableListItem[] = []; for (let i = 0; i < pageSize; i += 1) { const index = (current - 1) * 10 + i; tableListDataSource.push({ key: index, disabled: i % 6 === 0, name: `项目 ${index}`, buyer: '中国联通总部', buyway: '竞争性谈判', area: '北京市-市辖区-东城区', classify: '工程', beginTime: '2020-11-01 14:20:43', //callNo: Math.floor(Math.random() * 1000), 服务调用次数 //status: Math.floor(Math.random() * 10) % 4, //updatedAt: new Date(), //createdAt: new Date(), //progress: Math.ceil(Math.random() * 100),标签、进度条 }); } //撤销方法 tableListDataSource.reverse(); return tableListDataSource; }; let tableListDataSource = genList(1, 100); function getRule(req: Request, res: Response, u: string) { let realUrl = u; if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') { realUrl = req.url; } const { current = 1, pageSize = 10 } = req.query; const params = (parse(realUrl, true).query as unknown) as TableListParams; let dataSource = [...tableListDataSource].slice( ((current as number) - 1) * (pageSize as number), (current as number) * (pageSize as number), ); const sorter = JSON.parse(params.sorter as any); if (sorter) { dataSource = dataSource.sort((prev, next) => { let sortNumber = 0; Object.keys(sorter).forEach((key) => { if (sorter[key] === 'descend') { if (prev[key] - next[key] > 0) { sortNumber += -1; } else { sortNumber += 1; } return; } if (prev[key] - next[key] > 0) { sortNumber += 1; } else { sortNumber += -1; } }); return sortNumber; }); } if (params.filter) { const filter = JSON.parse(params.filter as any) as { [key: string]: string[]; }; if (Object.keys(filter).length > 0) { dataSource = dataSource.filter((item) => { return Object.keys(filter).some((key) => { if (!filter[key]) { return true; } if (filter[key].includes(`${item[key]}`)) { return true; } return false; }); }); } } if (params.name) { dataSource = dataSource.filter((data) => data.name.includes(params.name || '')); } const result = { data: dataSource, total: tableListDataSource.length, success: true, pageSize, current: parseInt(`${params.currentPage}`, 10) || 1, }; return res.json(result); }; function postRule(req: Request, res: Response, u: string, b: Request) { let realUrl = u; if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') { realUrl = req.url; } const body = (b && b.body) || req.body; const { method, key, name, buyer, buyway, area, classify, beginTime} = body; switch (method) { /* eslint no-case-declarations:0 */ case 'delete': tableListDataSource = tableListDataSource.filter((item) => key!=item.key); break; case 'post': (() => { const i = Math.ceil(Math.random() * 10000); const newRule = { key: tableListDataSource.length, disabled: i % 6 === 0, name, buyer, buyway, area, classify, beginTime, }; tableListDataSource.unshift(newRule); return res.json(newRule); })(); return; case 'update': (() => { let newRule = {}; tableListDataSource = tableListDataSource.map((item) => { if (item.key === key) { newRule = { ...item, name }; return { ...item, name }; } return item; }); return res.json(newRule); })(); return; default: break; } const result = { list: tableListDataSource, pagination: { total: tableListDataSource.length, }, }; res.json(result); } export default { 'GET /api/tanPanFind': getRule, 'POST /api/tanPanFind': postRule, };