user.ts 2.1 KB

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