rule.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. const { parse } = require("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. name: `TradeCode ${i}`,
  14. title: `一个任务名称 ${i}`,
  15. owner: "曲丽丽",
  16. desc: "这是一段描述",
  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. 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(
  46. data => parseInt(data.status, 10) === parseInt(s[0], 10)
  47. )
  48. );
  49. });
  50. dataSource = filterDataSource;
  51. }
  52. if (params.name) {
  53. dataSource = dataSource.filter(data => data.name.indexOf(params.name) > -1);
  54. }
  55. let pageSize = 10;
  56. if (params.pageSize) {
  57. pageSize = params.pageSize * 1;
  58. }
  59. const result = {
  60. list: dataSource,
  61. pagination: {
  62. total: dataSource.length,
  63. pageSize,
  64. current: parseInt(params.currentPage, 10) || 1
  65. }
  66. };
  67. return res.json(result);
  68. }
  69. function postRule(req, res, u, b) {
  70. let url = u;
  71. if (!url || Object.prototype.toString.call(url) !== "[object String]") {
  72. url = req.url; // eslint-disable-line
  73. }
  74. const body = (b && b.body) || req.body;
  75. const { method, name, desc, key } = body;
  76. switch (method) {
  77. /* eslint no-case-declarations:0 */
  78. case "delete":
  79. tableListDataSource = tableListDataSource.filter(
  80. item => key.indexOf(item.key) === -1
  81. );
  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. name: `TradeCode ${i}`,
  93. title: `一个任务名称 ${i}`,
  94. owner: "曲丽丽",
  95. desc,
  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. case "update":
  104. tableListDataSource = tableListDataSource.map(item => {
  105. if (item.key === key) {
  106. Object.assign(item, { desc, name });
  107. return item;
  108. }
  109. return item;
  110. });
  111. break;
  112. default:
  113. break;
  114. }
  115. return getRule(req, res, u);
  116. }
  117. module.exports = {
  118. "GET /api/rule": getRule,
  119. "POST /api/rule": postRule
  120. };