userMana.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * @Author: your name
  3. * @Date: 2021-07-26 15:20:30
  4. * @LastEditTime: 2021-07-27 10:04:54
  5. * @LastEditors: Please set LastEditors
  6. * @Description: In User Settings Edit
  7. * @FilePath: /TracerMethodology_PC/mock/userMana.js
  8. */
  9. // eslint-disable-next-line import/no-extraneous-dependencies
  10. import moment from 'moment';
  11. import { parse } from 'url'; // mock tableListDataSource
  12. const genList = (current, pageSize) => {
  13. const tableListDataSource = [];
  14. for (let i = 0; i < pageSize; i += 1) {
  15. const index = (current - 1) * 10 + i;
  16. tableListDataSource.push({
  17. key:index,
  18. name:`用户${i}`,
  19. account:`账户${i}`,
  20. hospitalStatus:0,
  21. id:index,
  22. password:'123'
  23. });
  24. }
  25. // console.log({tableListDataSource});
  26. tableListDataSource.reverse();
  27. return tableListDataSource;
  28. };
  29. let tableListDataSource = genList(1, 100);
  30. function getUser(req, res, u) {
  31. let realUrl = u;
  32. if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') {
  33. realUrl = req.url;
  34. }
  35. const { current = 1, pageSize = 10 } = req.query;
  36. const params = parse(realUrl, true).query;
  37. let dataSource = [...tableListDataSource].slice((current - 1) * pageSize, current * pageSize);
  38. if (params.sorter) {
  39. const sorter = JSON.parse(params.sorter);
  40. dataSource = dataSource.sort((prev, next) => {
  41. let sortNumber = 0;
  42. Object.keys(sorter).forEach((key) => {
  43. if (sorter[key] === 'descend') {
  44. if (prev[key] - next[key] > 0) {
  45. sortNumber += -1;
  46. } else {
  47. sortNumber += 1;
  48. }
  49. return;
  50. }
  51. if (prev[key] - next[key] > 0) {
  52. sortNumber += 1;
  53. } else {
  54. sortNumber += -1;
  55. }
  56. });
  57. return sortNumber;
  58. });
  59. }
  60. if (params.filter) {
  61. const filter = JSON.parse(params.filter);
  62. if (Object.keys(filter).length > 0) {
  63. dataSource = dataSource.filter((item) => {
  64. return Object.keys(filter).some((key) => {
  65. if (!filter[key]) {
  66. return true;
  67. }
  68. if (filter[key].includes(`${item[key]}`)) {
  69. return true;
  70. }
  71. return false;
  72. });
  73. });
  74. }
  75. }
  76. if (params.name) {
  77. dataSource = dataSource.filter((data) => data?.name?.includes(params.name || ''));
  78. }
  79. const result = {
  80. data: {
  81. list:dataSource,
  82. totalCount:tableListDataSource.length,
  83. pageSize:pageSize,
  84. },
  85. status:200,
  86. success: true,
  87. };
  88. return res.json(result);
  89. }
  90. function postUser(req, res, u, b) {
  91. let realUrl = u;
  92. if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') {
  93. realUrl = req.url;
  94. }
  95. // const body = (b && b.body) || req.body;
  96. const { method,body} = req;
  97. const {name,account, hospitalStatus,password } = body;
  98. switch (method) {
  99. /* eslint no-case-declarations:0 */
  100. case 'delete':
  101. tableListDataSource = tableListDataSource.filter((item) => key.indexOf(item.key) === -1);
  102. break;
  103. case 'POST':
  104. (() => {
  105. const i = Math.ceil(Math.random() * 10000);
  106. const newRule = {
  107. key: tableListDataSource.length,
  108. avatar: [
  109. 'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
  110. 'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
  111. ][i % 2],
  112. name,
  113. account,
  114. hospitalStatus,
  115. password,
  116. createdAt: moment().format('YYYY-MM-DD'),
  117. };
  118. tableListDataSource.unshift(newRule);
  119. return res.json(newRule);
  120. })();
  121. return;
  122. case 'update':
  123. (() => {
  124. let newRule = {};
  125. tableListDataSource = tableListDataSource.map((item) => {
  126. if (item.key === key) {
  127. newRule = { ...item, desc, name };
  128. return { ...item, desc, name };
  129. }
  130. return item;
  131. });
  132. return res.json(newRule);
  133. })();
  134. return;
  135. default:
  136. break;
  137. }
  138. const result = {
  139. list: tableListDataSource,
  140. pagination: {
  141. total: tableListDataSource.length,
  142. },
  143. };
  144. res.json(result);
  145. }
  146. export default {
  147. 'GET /api/costAccount/user/list': getUser,
  148. 'POST /api/costAccount/user/save': postUser,
  149. };