1
0

editTable.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import Mock from 'mockjs'
  2. import { MockMethod } from 'vite-plugin-mock'
  3. const data = Mock.mock({
  4. 'data|10': [
  5. {
  6. id: '@id',
  7. userCode: '@integer(1000, 7060)',
  8. userName: '@cname',
  9. address: '@city(true)',
  10. phone: '@phone',
  11. sex: '@sex'
  12. }
  13. ]
  14. }).data
  15. const mockMethodSeven: MockMethod[] = [
  16. {
  17. url: '/mock/api/edit/table/query',
  18. method: 'get',
  19. timeout: 1000,
  20. response: ({ query }: { query: any }) => ({
  21. code: '0',
  22. data: filterData(query.userName, query.userCode)
  23. })
  24. },
  25. {
  26. url: '/mock/api/edit/table/save',
  27. method: 'post',
  28. timeout: 1000,
  29. response: ({ body }: { body: any }) => {
  30. if (Array.isArray(body)) {
  31. data.splice(0)
  32. body.forEach((item, index) => {
  33. delete item.isEdit
  34. data[index] = { ...item, id: Mock.Random.id() }
  35. })
  36. } else {
  37. delete body.isEdit
  38. if (body.id) {
  39. const index = data.findIndex((f: any) => f.id === body.id)
  40. data[index] = body
  41. return
  42. }
  43. data.unshift({ ...body, id: Mock.Random.id() })
  44. }
  45. }
  46. },
  47. {
  48. url: '/mock/api/edit/table/delete',
  49. method: 'post',
  50. timeout: 1000,
  51. response: ({ body }: { body: any }) => {
  52. const { id } = body
  53. const index = data.findIndex((i: any) => i.id === id)
  54. data.splice(index, 1)
  55. return {
  56. code: '0'
  57. }
  58. }
  59. }
  60. ]
  61. function filterData(userName: string, userCode: string) {
  62. return data.filter((f: any) => f.userName.includes(userName ?? '') && f.userCode.toString().includes(userCode ?? ''))
  63. }
  64. export default mockMethodSeven