index.ts 1.9 KB

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