import { Request, Response } from 'express'; import { TableListItem, TableListParams } from '@/pages/newList/data'; import { parse } from 'url'; // mock tableListDataSource 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, order_name: `良品铺子小零食 ${index}`,//订单名称 order_reference: (Math.floor(Math.random() * Math.random() * Math.random() * Math.random() * 10000)).toString(),//订单编号 order_status: (Math.floor(Math.random() * 10) % 4).toString(),//订单状态 buyer: 'buyer',//购买人 create_time: new Date(),//创建时间 update_time: new Date(),//更新日期 manager: 'manager',//管理者 d_store_name: '良品铺子旗舰店',//店铺名称 is_available: i % 6 === 0//是否可用 }); } // tableListDataSource.reverse();//倒序全部数据 return tableListDataSource; }; let tableListDataSource = genList(1, 100); const addRule = (req:Request ,res: Response) => {//添加函数 const body = req.body; const { order_name , order_reference } = body; (() => { const i = Math.ceil(Math.random() * 10000); const newRule = { key: tableListDataSource.length, order_name,//订单名称 order_reference,//订单编号 order_status: (Math.floor(Math.random() * 10) % 4).toString(),//订单状态 buyer: 'buyer',//购买人 create_time: new Date(),//创建时间 update_time: new Date(),//更新日期 manager: 'manager',//管理者 d_store_name: '良品铺子旗舰店',//店铺名称 is_available: tableListDataSource.length % 6 === 0//是否可用 }; tableListDataSource.unshift(newRule); return res.json(newRule); })(); return; } const removeRule = (req: Request,res: Response) => {//删除函数 const body = req.body const { key } = body tableListDataSource = tableListDataSource.filter((item) => key.indexOf(item.key) === -1); const result = { list: tableListDataSource, pagination: { total: tableListDataSource.length, }, }; res.json(result); } function getRule(req: Request, res: Response, u: string) {//查询函数 let realUrl = u; if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') {//Object.prototype.toString.call(realUrl)判断数据类型 realUrl = req.url; } const { current = 1, pageSize = 10 } = req.query; const params = (parse(realUrl, true).query as unknown) as TableListParams; let dataSource = [...tableListDataSource].slice(//提取出10行数据 ((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.order_name) { dataSource = dataSource.filter((data) => data.order_name.includes(params.order_name || '')); } const result = { data: dataSource, total: tableListDataSource.length, success: true, pageSize, current: parseInt(`${params.currentPage}`, 10) || 1, }; return res.json(result); } const updateRule = (req: Request,res:Response) => {//更新表格数据 const body = req.body; const { key, order_name,order_reference } = body; (() => { let newRule = {}; tableListDataSource = tableListDataSource.map((item) => { if (item.key === key) { newRule = { ...item, order_reference, order_name }; return { ...item, order_reference, order_name }; } return item; }); return res.json(newRule); })(); return; } export default { 'GET /api/queryrule': getRule, 'POST /api/removerule': removeRule, 'POST /api/addrule': addRule, 'POST /api/updaterule': updateRule };