listTableList.ts 5.0 KB

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