user.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 Admin',
  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: 200,
  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. method: 'get',
  63. response: ({ query }) => {
  64. const { userId } = query;
  65. const checkUser = createFakeUserList().find((item) => item.userId === userId);
  66. if (!checkUser) {
  67. return resultError('The corresponding user information was not obtained!');
  68. }
  69. return resultSuccess(checkUser);
  70. },
  71. },
  72. {
  73. url: '/api/getPermCodeByUserId',
  74. timeout: 200,
  75. method: 'get',
  76. response: ({ query }) => {
  77. const { userId } = query;
  78. if (!userId) {
  79. return resultError('userId is not null!');
  80. }
  81. const codeList = fakeCodeList[userId];
  82. return resultSuccess(codeList);
  83. },
  84. },
  85. ] as MockMethod[];