rule.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { getUrlParams } from './utils';
  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} ${Math.floor(i / 2) + 1}:00:00`),
  17. createdAt: new Date(`2017-07-${Math.floor(i / 2) + 1} ${Math.floor(i / 2) + 1}:00:00`),
  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 = getUrlParams(url);
  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 s = params.status.split(',');
  39. if (s.length === 1) {
  40. dataSource = dataSource.filter(data => parseInt(data.status, 10) === parseInt(s[0], 10));
  41. }
  42. }
  43. if (params.no) {
  44. dataSource = dataSource.filter(data => data.no.indexOf(params.no) > -1);
  45. }
  46. let pageSize = 10;
  47. if (params.pageSize) {
  48. pageSize = params.pageSize * 1;
  49. }
  50. const result = {
  51. list: dataSource,
  52. pagination: {
  53. total: dataSource.length,
  54. pageSize,
  55. current: parseInt(params.currentPage, 10) || 1,
  56. },
  57. };
  58. if (res && res.json) {
  59. res.json(result);
  60. } else {
  61. return result;
  62. }
  63. }
  64. export function postRule(req, res, u, b) {
  65. let url = u;
  66. if (!url || Object.prototype.toString.call(url) !== '[object String]') {
  67. url = req.url; // eslint-disable-line
  68. }
  69. const body = (b && b.body) || req.body;
  70. const { method, no, description } = body;
  71. switch (method) {
  72. /* eslint no-case-declarations:0 */
  73. case 'delete':
  74. tableListDataSource = tableListDataSource.filter(item => no.indexOf(item.no) === -1);
  75. break;
  76. case 'post':
  77. const i = Math.ceil(Math.random() * 10000);
  78. tableListDataSource.unshift({
  79. key: i,
  80. href: 'https://ant.design',
  81. avatar: ['https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png', 'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png'][i % 2],
  82. no: `TradeCode ${i}`,
  83. title: `一个任务名称 ${i}`,
  84. owner: '曲丽丽',
  85. description,
  86. callNo: Math.floor(Math.random() * 1000),
  87. status: Math.floor(Math.random() * 10) % 2,
  88. updatedAt: new Date(),
  89. createdAt: new Date(),
  90. progress: Math.ceil(Math.random() * 100),
  91. });
  92. break;
  93. default:
  94. break;
  95. }
  96. const result = {
  97. list: tableListDataSource,
  98. pagination: {
  99. total: tableListDataSource.length,
  100. },
  101. };
  102. if (res && res.json) {
  103. res.json(result);
  104. } else {
  105. return result;
  106. }
  107. }
  108. export default {
  109. getRule,
  110. postRule,
  111. };