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

68 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. console.log('--onShow')
  38. getData()
  39. })
  40. onReachBottom(() => {
  41. console.log('--onReachBottom')
  42. getMore()
  43. })
  44. return {
  45. queryParams,
  46. list,
  47. total,
  48. getData,
  49. getMore,
  50. }
  51. }