user.ts 2.2 KB

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