user.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { MockMethod } from 'vite-plugin-mock';
  2. import { resultError, resultSuccess, getRequestToken, requestParams } from '../_util';
  3. export function createFakeUserList() {
  4. return [
  5. {
  6. userId: '1',
  7. username: 'admin',
  8. realName: 'admin123admin',
  9. avatar: '',
  10. desc: 'manager',
  11. password: '123456',
  12. token: 'fakeToken1',
  13. homePath: '/dashboard/analysis',
  14. roles: [
  15. {
  16. roleName: 'Super Admin',
  17. value: 'super',
  18. },
  19. ],
  20. },
  21. {
  22. userId: '2',
  23. username: 'test',
  24. password: '123456',
  25. realName: 'test user',
  26. avatar: '',
  27. desc: 'tester',
  28. token: 'fakeToken2',
  29. homePath: '/dashboard/workbench',
  30. roles: [
  31. {
  32. roleName: 'Tester',
  33. value: 'test',
  34. },
  35. ],
  36. },
  37. ];
  38. }
  39. const fakeCodeList: any = {
  40. '1': ['1000', '3000', '5000'],
  41. '2': ['2000', '4000', '6000'],
  42. };
  43. export default [
  44. // mock user login
  45. {
  46. url: '/basic-api/login',
  47. timeout: 200,
  48. method: 'post',
  49. response: ({ body }) => {
  50. const { username, password } = body;
  51. const checkUser = createFakeUserList().find(
  52. (item) => item.username === username && password === item.password,
  53. );
  54. if (!checkUser) {
  55. return resultError('Incorrect account or password!');
  56. }
  57. const { userId, username: _username, token, realName, desc, roles } = checkUser;
  58. return resultSuccess({
  59. roles,
  60. userId,
  61. username: _username,
  62. token,
  63. realName,
  64. desc,
  65. });
  66. },
  67. },
  68. {
  69. url: '/basic-api/getUserInfo',
  70. method: 'get',
  71. response: (request: requestParams) => {
  72. const token = getRequestToken(request);
  73. if (!token) return resultError('Invalid token');
  74. const checkUser = createFakeUserList().find((item) => item.token === token);
  75. if (!checkUser) {
  76. return resultError('The corresponding user information was not obtained!');
  77. }
  78. return resultSuccess(checkUser);
  79. },
  80. },
  81. {
  82. url: '/basic-api/getPermCode',
  83. timeout: 200,
  84. method: 'get',
  85. response: (request: requestParams) => {
  86. const token = getRequestToken(request);
  87. if (!token) return resultError('Invalid token');
  88. const checkUser = createFakeUserList().find((item) => item.token === token);
  89. if (!checkUser) {
  90. return resultError('Invalid token!');
  91. }
  92. const codeList = fakeCodeList[checkUser.userId];
  93. return resultSuccess(codeList);
  94. },
  95. },
  96. {
  97. url: '/basic-api/logout',
  98. timeout: 200,
  99. method: 'get',
  100. response: (request: requestParams) => {
  101. const token = getRequestToken(request);
  102. if (!token) return resultError('Invalid token');
  103. const checkUser = createFakeUserList().find((item) => item.token === token);
  104. if (!checkUser) {
  105. return resultError('Invalid token!');
  106. }
  107. return resultSuccess(undefined, { message: 'Token has been destroyed' });
  108. },
  109. },
  110. {
  111. url: '/basic-api/testRetry',
  112. statusCode: 405,
  113. method: 'get',
  114. response: () => {
  115. return resultError('Error!');
  116. },
  117. },
  118. ] as MockMethod[];