index.mock.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { SUCCESS_CODE } from '@/constants'
  2. const timeout = 1000
  3. const List: {
  4. username: string
  5. password: string
  6. role: string
  7. roleId: string
  8. permissions: string | string[]
  9. }[] = [
  10. {
  11. username: 'admin',
  12. password: 'admin',
  13. role: 'admin',
  14. roleId: '1',
  15. permissions: ['*.*.*']
  16. },
  17. {
  18. username: 'test',
  19. password: 'test',
  20. role: 'test',
  21. roleId: '2',
  22. permissions: ['example:dialog:create', 'example:dialog:delete']
  23. }
  24. ]
  25. export default [
  26. // 列表接口
  27. {
  28. url: '/mock/user/list',
  29. method: 'get',
  30. response: ({ query }) => {
  31. const { username, pageIndex, pageSize } = query
  32. const mockList = List.filter((item) => {
  33. if (username && item.username.indexOf(username) < 0) return false
  34. return true
  35. })
  36. const pageList = mockList.filter(
  37. (_, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1)
  38. )
  39. return {
  40. code: SUCCESS_CODE,
  41. data: {
  42. total: mockList.length,
  43. list: pageList
  44. }
  45. }
  46. }
  47. },
  48. // 登录接口
  49. {
  50. url: '/mock/user/login',
  51. method: 'post',
  52. timeout,
  53. response: ({ body }) => {
  54. const data = body
  55. let hasUser = false
  56. for (const user of List) {
  57. if (user.username === data.username && user.password === data.password) {
  58. hasUser = true
  59. return {
  60. code: SUCCESS_CODE,
  61. data: user
  62. }
  63. }
  64. }
  65. if (!hasUser) {
  66. return {
  67. code: 500,
  68. message: '账号或密码错误'
  69. }
  70. }
  71. }
  72. },
  73. // 退出接口
  74. {
  75. url: '/mock/user/loginOut',
  76. method: 'get',
  77. timeout,
  78. response: () => {
  79. return {
  80. code: SUCCESS_CODE,
  81. data: null
  82. }
  83. }
  84. }
  85. ]