动态菜单和动态路由的 antd pro

rule.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { parse } from 'url';
  2. // mock tableListDataSource
  3. let tableListDataSource = [];
  4. for (let i = 0; i < 46; i += 1) {
  5. tableListDataSource.push({
  6. key: i,
  7. disabled: i % 6 === 0,
  8. href: 'https://ant.design',
  9. avatar: [
  10. 'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
  11. 'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
  12. ][i % 2],
  13. no: `TradeCode ${i}`,
  14. title: `一个任务名称 ${i}`,
  15. owner: '曲丽丽',
  16. description: '这是一段描述',
  17. callNo: Math.floor(Math.random() * 1000),
  18. status: Math.floor(Math.random() * 10) % 4,
  19. updatedAt: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
  20. createdAt: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
  21. progress: Math.ceil(Math.random() * 100),
  22. });
  23. }
  24. export function getRule(req, res, u) {
  25. let url = u;
  26. if (!url || Object.prototype.toString.call(url) !== '[object String]') {
  27. url = req.url; // eslint-disable-line
  28. }
  29. const params = parse(url, true).query;
  30. let dataSource = [...tableListDataSource];
  31. if (params.sorter) {
  32. const s = params.sorter.split('_');
  33. dataSource = dataSource.sort((prev, next) => {
  34. if (s[1] === 'descend') {
  35. return next[s[0]] - prev[s[0]];
  36. }
  37. return prev[s[0]] - next[s[0]];
  38. });
  39. }
  40. if (params.status) {
  41. const status = params.status.split(',');
  42. let filterDataSource = [];
  43. status.forEach(s => {
  44. filterDataSource = filterDataSource.concat(
  45. [...dataSource].filter(data => parseInt(data.status, 10) === parseInt(s[0], 10))
  46. );
  47. });
  48. dataSource = filterDataSource;
  49. }
  50. if (params.no) {
  51. dataSource = dataSource.filter(data => data.no.indexOf(params.no) > -1);
  52. }
  53. let pageSize = 10;
  54. if (params.pageSize) {
  55. pageSize = params.pageSize * 1;
  56. }
  57. const result = {
  58. list: dataSource,
  59. pagination: {
  60. total: dataSource.length,
  61. pageSize,
  62. current: parseInt(params.currentPage, 10) || 1,
  63. },
  64. };
  65. if (res && res.json) {
  66. res.json(result);
  67. } else {
  68. return result;
  69. }
  70. }
  71. export function postRule(req, res, u, b) {
  72. let url = u;
  73. if (!url || Object.prototype.toString.call(url) !== '[object String]') {
  74. url = req.url; // eslint-disable-line
  75. }
  76. const body = (b && b.body) || req.body;
  77. const { method, no, description } = body;
  78. switch (method) {
  79. /* eslint no-case-declarations:0 */
  80. case 'delete':
  81. tableListDataSource = tableListDataSource.filter(item => no.indexOf(item.no) === -1);
  82. break;
  83. case 'post':
  84. const i = Math.ceil(Math.random() * 10000);
  85. tableListDataSource.unshift({
  86. key: i,
  87. href: 'https://ant.design',
  88. avatar: [
  89. 'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
  90. 'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
  91. ][i % 2],
  92. no: `TradeCode ${i}`,
  93. title: `一个任务名称 ${i}`,
  94. owner: '曲丽丽',
  95. description,
  96. callNo: Math.floor(Math.random() * 1000),
  97. status: Math.floor(Math.random() * 10) % 2,
  98. updatedAt: new Date(),
  99. createdAt: new Date(),
  100. progress: Math.ceil(Math.random() * 100),
  101. });
  102. break;
  103. default:
  104. break;
  105. }
  106. const result = {
  107. list: tableListDataSource,
  108. pagination: {
  109. total: tableListDataSource.length,
  110. },
  111. };
  112. if (res && res.json) {
  113. res.json(result);
  114. } else {
  115. return result;
  116. }
  117. }
  118. export default {
  119. getRule,
  120. postRule,
  121. };