listTableList.ts 4.8 KB

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