rule.js 3.3 KB

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