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

import { ref, reactive } from 'vue'
import { onShow, onReachBottom } from '@dcloudio/uni-app'
export const usePageList = (apiFun, defaultQueryParams) => {
const queryParams = reactive({
pageNo: 1,
pageSize: 10,
...defaultQueryParams
})
const list = ref([])
const total = ref(0)
const fetchData = async () => {
try {
const res = await apiFun(queryParams)
if (res.code === 200) {
return res.data
}
return { list: [], total: 0 }
} catch (err) {
return { list: [], total: 0 }
}
}
const getData = async () => {
queryParams.pageNo = 1
const { list: _list, total: _total } = await fetchData()
list.value = _list
total.value = _total
}
const getMore = async () => {
if (list.value.length >= total.value) {
return
}
queryParams.pageNo++
const { list: _list } = await fetchData()
list.value = list.value.concat(_list)
}
onShow(() => {
console.log('--onShow')
getData()
})
onReachBottom(() => {
console.log('--onReachBottom')
getMore()
})
return {
queryParams,
list,
total,
getData,
getMore,
}
}