25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

179 satır
4.5 KiB

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