list.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { ref } from 'vue'
  2. import { getCurrentInstance } from 'vue'
  3. function useList(url, beforeData = null, httpType) {
  4. const {
  5. appContext: {
  6. config: { globalProperties: global }
  7. }
  8. } = getCurrentInstance()
  9. const empty = ref(false)
  10. const loading = ref(false)
  11. const finished = ref(false)
  12. const page = ref(0)
  13. const totalElements = ref(0)
  14. const size = ref(20)
  15. const list = ref([])
  16. const getData = (isRefresh = false, isFinished = false) => {
  17. if (isRefresh) {
  18. page.value = 0
  19. list.value = []
  20. }
  21. loading.value = true
  22. finished.value = false
  23. empty.value = false
  24. let data = { page: page.value, size: size.value, sort: 'createdAt,desc' }
  25. if (beforeData) {
  26. data = {
  27. ...data,
  28. ...beforeData()
  29. }
  30. }
  31. if (httpType === 'get') {
  32. return global.$http
  33. .get(url, data, { body: 'json' })
  34. .then(res => {
  35. if (res.first) {
  36. list.value = []
  37. }
  38. list.value = [...list.value, ...res.content]
  39. empty.value = res.empty
  40. loading.value = false
  41. finished.value = isFinished || res.last
  42. if (!finished.value) {
  43. page.value = page.value + 1
  44. }
  45. totalElements.value = Number(res.totalElements)
  46. })
  47. .catch(e => {
  48. console.log(e)
  49. })
  50. } else {
  51. return global.$http
  52. .post(url, data, { body: 'json' })
  53. .then(res => {
  54. if (res.first) {
  55. list.value = []
  56. }
  57. list.value = [...list.value, ...res.content]
  58. empty.value = res.empty
  59. loading.value = false
  60. finished.value = isFinished || res.last
  61. if (!finished.value) {
  62. page.value = page.value + 1
  63. }
  64. totalElements.value = Number(res.totalElements)
  65. })
  66. .catch(e => {
  67. console.log(e)
  68. })
  69. }
  70. }
  71. return {
  72. empty,
  73. loading,
  74. finished,
  75. page,
  76. totalElements,
  77. size,
  78. list,
  79. getData
  80. }
  81. }
  82. export default useList