listTableList.ts 5.0 KB

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