user.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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: '超级管理员',
  9. desc: '管理员',
  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: '测试管理员',
  24. desc: '普通管理员',
  25. token: 'fakeToken2',
  26. roles: [
  27. {
  28. roleName: 'Tester',
  29. value: 'test',
  30. },
  31. ],
  32. },
  33. {
  34. userId: '2',
  35. username: 'admin',
  36. password: '123456',
  37. realName: '测试管理员',
  38. desc: '普通管理员',
  39. token: 'fakeToken2',
  40. roles: [
  41. {
  42. roleName: 'Tester',
  43. value: 'test',
  44. },
  45. ],
  46. },
  47. ];
  48. }
  49. const fakeCodeList: any = {
  50. '1': ['1000', '3000', '5000'],
  51. '2': ['2000', '4000', '6000'],
  52. };
  53. export default [
  54. // mock user login
  55. {
  56. url: '/api/login',
  57. timeout: 200,
  58. method: 'post',
  59. response: ({ body }) => {
  60. const { username, password } = body;
  61. const checkUser = createFakeUserList().find(
  62. (item) => item.username === username && password === item.password
  63. );
  64. if (!checkUser) {
  65. return resultError('帐户或密码不正确!');
  66. }
  67. const { userId, username: _username, token, realName, desc, roles } = checkUser;
  68. return resultSuccess({
  69. roles,
  70. userId,
  71. username: _username,
  72. token,
  73. realName,
  74. desc,
  75. });
  76. },
  77. },
  78. {
  79. url: '/api/getUserInfoById',
  80. method: 'get',
  81. response: ({ query }) => {
  82. const { userId } = query;
  83. const checkUser = createFakeUserList().find((item) => item.userId === userId);
  84. if (!checkUser) {
  85. return resultError('The corresponding user information was not obtained!');
  86. }
  87. return resultSuccess(checkUser);
  88. },
  89. },
  90. {
  91. url: '/api/getPermCodeByUserId',
  92. timeout: 200,
  93. method: 'get',
  94. response: ({ query }) => {
  95. const { userId } = query;
  96. if (!userId) {
  97. return resultError('userId is not null!');
  98. }
  99. const codeList = fakeCodeList[userId];
  100. return resultSuccess(codeList);
  101. },
  102. },
  103. ] as MockMethod[];