Files
yshop-pro-uniapp/hooks/usePage.js

98 lines
1.8 KiB
JavaScript
Raw Normal View History

2023-10-11 11:27:47 +08:00
// mouse.js
import { ref, onMounted, onUnmounted } from 'vue'
import { onReady, onReachBottom } from '@dcloudio/uni-app'
export const usePage = getPage => {
// 页码,默认为1
const page = ref(1)
// 页大小,默认为10
const limit = ref(10)
// 关键字
const keyword = ref('')
// 类别
const type = ref('')
// 分类ID
const sid = ref('')
// 是否新品,不为空的字符串即可
const news = ref('')
// 是否积分兑换商品
const isIntegral = ref('')
// 到底了
const loadend = ref(false)
// 加载中
const loading = ref(false)
2023-11-14 17:21:03 +08:00
// 是否有数据
const listEmpty = ref(false)
2023-10-11 11:27:47 +08:00
const dataList = ref([])
const handleGetDataList = async () => {
if (loading.value || loadend.value) return
loading.value = true
const products = await getPage({
page: page.value,
limit: limit.value,
keyword: keyword.value,
type: type.value,
sid: sid.value,
news: news.value,
isIntegral: isIntegral.value,
})
2023-11-14 17:21:03 +08:00
listEmpty.value = false
2023-10-11 11:27:47 +08:00
if (products) {
if (products.length <= 0) {
2023-11-14 17:21:03 +08:00
if(page.value === 1){
listEmpty.value = true
} else {
loadend.value = true
}
2023-10-11 11:27:47 +08:00
}
dataList.value = dataList.value.concat(products)
}
loading.value = false
}
const handleRefresh = () => {
loadend.value = false
loading.value = false
2023-11-14 17:21:03 +08:00
page.value = 1
2023-10-11 11:27:47 +08:00
dataList.value = []
handleGetDataList()
}
onReady(() => {
// handleGetDataList()
})
onReachBottom(() => {
2023-11-14 17:21:03 +08:00
if (loading.value || loadend.value) return
2023-10-11 11:27:47 +08:00
page.value += 1
2023-11-14 17:21:03 +08:00
handleGetDataList()
2023-10-11 11:27:47 +08:00
})
// 通过返回值暴露所管理的状态
return {
type,
dataList,
page,
limit,
keyword,
loading,
loadend,
2023-11-14 17:21:03 +08:00
listEmpty,
news,
sid,
2023-10-11 11:27:47 +08:00
refresh: handleRefresh,
}
}