猫妈狗爸伴宠师小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
970 B

  1. import { ref, reactive } from 'vue'
  2. export const usePageList = (apiFun, defaultQueryParams) => {
  3. const queryParams = reactive({
  4. pageNo: 1,
  5. pageSize: 10,
  6. ...defaultQueryParams
  7. })
  8. const list = ref([])
  9. const total = ref(0)
  10. const fetchData = async () => {
  11. try {
  12. const res = await apiFun(queryParams)
  13. if (res.code === 200) {
  14. return res.data
  15. }
  16. return { list: [], total: 0 }
  17. } catch (err) {
  18. return { list: [], total: 0 }
  19. }
  20. }
  21. const getData = async () => {
  22. queryParams.pageNo = 1
  23. const { list: _list, total: _total } = await fetchData()
  24. list.value = _list
  25. total.value = _total
  26. }
  27. const getMore = async () => {
  28. if (list.value.length >= total.value) {
  29. return
  30. }
  31. queryParams.pageNo++
  32. const { list: _list } = await fetchData()
  33. list.value = list.value.concat(_list)
  34. }
  35. return {
  36. queryParams,
  37. list,
  38. total,
  39. getData,
  40. getMore,
  41. }
  42. }