rule.js 3.4 KB

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