user.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { MockMethod } from 'vite-plugin-mock';
  2. import { resultError, resultSuccess } from '../_util';
  3. function createFakeUserList() {
  4. return [
  5. {
  6. userId: '1',
  7. username: 'vben',
  8. realName: 'Vben',
  9. desc: 'manager',
  10. password: '123456',
  11. token: 'fakeToken1',
  12. role: {
  13. roleName: 'Super Admin',
  14. value: 'super',
  15. },
  16. },
  17. {
  18. userId: '2',
  19. username: 'test',
  20. password: '123456',
  21. realName: 'test user',
  22. desc: 'tester',
  23. token: 'fakeToken2',
  24. role: {
  25. roleName: 'Tester',
  26. value: 'test',
  27. },
  28. },
  29. ];
  30. }
  31. const fakeCodeList: any = {
  32. '1': ['1000', '3000', '5000'],
  33. '2': ['2000', '4000', '6000'],
  34. };
  35. export default [
  36. // mock user login
  37. {
  38. url: '/api/login',
  39. timeout: 1000,
  40. method: 'post',
  41. response: ({ body }) => {
  42. const { username, password } = body;
  43. const checkUser = createFakeUserList().find(
  44. (item) => item.username === username && password === item.password
  45. );
  46. if (!checkUser) {
  47. return resultError('Incorrect account or password!');
  48. }
  49. const { userId, username: _username, token, realName, desc, role } = checkUser;
  50. return resultSuccess({
  51. role,
  52. userId,
  53. username: _username,
  54. token,
  55. realName,
  56. desc,
  57. });
  58. },
  59. },
  60. {
  61. url: '/api/getUserInfoById',
  62. timeout: 200,
  63. method: 'get',
  64. response: ({ query }) => {
  65. const { userId } = query;
  66. const checkUser = createFakeUserList().find((item) => item.userId === userId);
  67. if (!checkUser) {
  68. return resultError('The corresponding user information was not obtained!');
  69. }
  70. return resultSuccess(checkUser);
  71. },
  72. },
  73. {
  74. url: '/api/getPermCodeByUserId',
  75. timeout: 200,
  76. method: 'get',
  77. response: ({ query }) => {
  78. const { userId } = query;
  79. if (!userId) {
  80. return resultError('userId is not null!');
  81. }
  82. const codeList = fakeCodeList[userId];
  83. return resultSuccess(codeList);
  84. },
  85. },
  86. ] as MockMethod[];