listTableList.js 4.5KB

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