猫妈狗爸伴宠师小程序前端代码
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.

72 lines
1.2 KiB

  1. import { ref, reactive } from 'vue'
  2. import { onShow, onReachBottom } from '@dcloudio/uni-app'
  3. export const usePageList = (apiFun, defaultQueryParams) => {
  4. const queryParams = reactive({
  5. pageNo: 1,
  6. pageSize: 10,
  7. ...defaultQueryParams
  8. })
  9. const list = ref([])
  10. const total = ref(0)
  11. const fetchData = async () => {
  12. try {
  13. const res = await apiFun(queryParams)
  14. if (res.code === 200) {
  15. return res.data
  16. }
  17. return { list: [], total: 0 }
  18. } catch (err) {
  19. return { list: [], total: 0 }
  20. }
  21. }
  22. const getData = async () => {
  23. queryParams.pageNo = 1
  24. const { list: _list, total: _total } = await fetchData()
  25. list.value = _list
  26. total.value = _total
  27. }
  28. const getMore = async () => {
  29. if (list.value.length >= total.value) {
  30. return
  31. }
  32. queryParams.pageNo++
  33. const { list: _list } = await fetchData()
  34. list.value = list.value.concat(_list)
  35. }
  36. onShow(() => {
  37. // todo fetch
  38. console.log('--onShow')
  39. return
  40. getData()
  41. })
  42. onReachBottom(() => {
  43. // todo fetch
  44. console.log('--onReachBottom')
  45. return
  46. getMore()
  47. })
  48. return {
  49. queryParams,
  50. list,
  51. total,
  52. getData,
  53. getMore,
  54. }
  55. }