mirror of
https://github.com/ialley-workshop-open/uni-halo.git
synced 2026-11-13 22:11:42 +08:00
refactor(load-status): 重构数据加载状态管理,统一加载更多逻辑
1. 重构useDataLoadingStatus钩子,新增完整加载状态类型 2. 统一首页和动态页面的加载更多状态管理逻辑 3. 移除冗余的加载状态本地变量,复用钩子内置状态 4. 调整分类页面默认分页大小从3改为10 5. 修复加载状态的默认值处理逻辑
This commit is contained in:
@@ -27,7 +27,7 @@
|
||||
/* ---------------- 状态 ---------------- */
|
||||
const { loadingStatus, loadMoreStatus, updateLoadingStatus, updateLoadMoreStatus, resetLoadingStatus, resetLoadMoreStatus } = useDataLoadingStatus()
|
||||
const queryParams = ref({
|
||||
size: 3,
|
||||
size: 10,
|
||||
page: 1,
|
||||
fieldSelector: ['spec.hideFromList=false'],
|
||||
})
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
import { useAppConfigStore } from '@/store/appConfig'
|
||||
import { useSettingStore } from '@/store/setting'
|
||||
import { checkAvatarUrl, checkImageUrl } from '@/utils/url'
|
||||
import { sleep } from '@/utils/common'
|
||||
import { t } from '@/locale'
|
||||
import { useMaintenanceIntercept } from '@/hooks/useMaintenanceIntercept'
|
||||
import { DataLoadingStatusEnum, useDataLoadingStatus } from '@/hooks/useDataLoadingStatus'
|
||||
@@ -29,13 +30,9 @@
|
||||
const haloConfigs = computed(() => appConfigStore.configs)
|
||||
|
||||
/* ---------------- 状态 ---------------- */
|
||||
const { loadingStatus, updateLoadingStatus } = useDataLoadingStatus()
|
||||
const isLoadMore = ref(false)
|
||||
const loadMoreText = ref(t('common.loading'))
|
||||
const { loadingStatus, loadMoreStatus, updateLoadingStatus, updateLoadMoreStatus, resetLoadMoreStatus } = useDataLoadingStatus()
|
||||
const articleList = ref<IPost[]>([])
|
||||
|
||||
const result = ref<{ hasNext : boolean }>({ hasNext: false })
|
||||
|
||||
const queryParams = ref({
|
||||
size: 10,
|
||||
page: 1,
|
||||
@@ -65,7 +62,7 @@
|
||||
if (recommendMode.value === mode)
|
||||
return
|
||||
recommendMode.value = mode
|
||||
isLoadMore.value = false
|
||||
resetLoadMoreStatus()
|
||||
articleList.value = []
|
||||
queryParams.value.page = 1
|
||||
queryParams.value.sort = recommendSortMap[mode]
|
||||
@@ -104,7 +101,8 @@
|
||||
/** 文章列表 */
|
||||
async function handleGetArticleList() {
|
||||
if (calcAuditModeEnabled.value) {
|
||||
// 审核模式:真实文章按 audit-data posts 过滤(数组顺序即展示顺序)
|
||||
// 审核模式:真实文章按 audit-data posts 过滤(数组顺序即展示顺序),一次拉取不分页
|
||||
resetLoadMoreStatus()
|
||||
const auditPostNames = appConfigStore.auditData.spec?.posts || []
|
||||
try {
|
||||
const res = await getPostList({ page: 1, size: 0, sort: ['spec.publishTime,desc'] })
|
||||
@@ -114,12 +112,19 @@
|
||||
return item
|
||||
})
|
||||
updateLoadingStatus(articleList.value.length === 0 ? DataLoadingStatusEnum.Empty : DataLoadingStatusEnum.Success)
|
||||
loadMoreText.value = t('common.noMore')
|
||||
updateLoadMoreStatus({
|
||||
active: false,
|
||||
status: 'noMore',
|
||||
hasNext: false,
|
||||
})
|
||||
}
|
||||
catch (err) {
|
||||
console.error('获取审核文章失败', err)
|
||||
updateLoadingStatus(DataLoadingStatusEnum.Error)
|
||||
loadMoreText.value = t('common.loadFailed')
|
||||
updateLoadMoreStatus({
|
||||
active: false,
|
||||
status: 'error',
|
||||
})
|
||||
}
|
||||
finally {
|
||||
uni.hideLoading()
|
||||
@@ -128,27 +133,39 @@
|
||||
return
|
||||
}
|
||||
|
||||
if (!isLoadMore.value) {
|
||||
if (!loadMoreStatus.value.active) {
|
||||
updateLoadingStatus(DataLoadingStatusEnum.Loading)
|
||||
}
|
||||
loadMoreText.value = t('common.loading')
|
||||
|
||||
try {
|
||||
const res = await getPostList({ ...toRaw(queryParams.value) })
|
||||
result.value.hasNext = res.data.hasNext
|
||||
articleList.value = (isLoadMore.value
|
||||
articleList.value = (loadMoreStatus.value.active
|
||||
? articleList.value.concat(res.data.items)
|
||||
: res.data.items).map((item) => {
|
||||
item.owner.avatar = checkAvatarUrl(item.owner.avatar)
|
||||
return item
|
||||
})
|
||||
updateLoadingStatus(articleList.value.length === 0 ? DataLoadingStatusEnum.Empty : DataLoadingStatusEnum.Success)
|
||||
loadMoreText.value = res.data.hasNext ? t('common.loadMore') : t('common.noMore')
|
||||
if (!loadMoreStatus.value.active) {
|
||||
await sleep(600)
|
||||
updateLoadingStatus(articleList.value.length === 0 ? DataLoadingStatusEnum.Empty : DataLoadingStatusEnum.Success)
|
||||
}
|
||||
updateLoadMoreStatus({
|
||||
active: false,
|
||||
status: res.data.hasNext ? 'loadMore' : 'noMore',
|
||||
hasNext: res.data.hasNext,
|
||||
})
|
||||
}
|
||||
catch (err) {
|
||||
updateLoadingStatus(DataLoadingStatusEnum.Error)
|
||||
loadMoreText.value = t('common.loadFailed')
|
||||
console.error('获取文章失败', err)
|
||||
if (loadMoreStatus.value.active) {
|
||||
updateLoadMoreStatus({
|
||||
active: false,
|
||||
status: 'error',
|
||||
})
|
||||
}
|
||||
else {
|
||||
updateLoadingStatus(DataLoadingStatusEnum.Error)
|
||||
}
|
||||
}
|
||||
finally {
|
||||
uni.hideLoading()
|
||||
@@ -182,23 +199,29 @@
|
||||
})
|
||||
|
||||
onPullDownRefresh(() => {
|
||||
isLoadMore.value = false
|
||||
resetLoadMoreStatus()
|
||||
queryParams.value.page = 1
|
||||
handleQuery()
|
||||
})
|
||||
|
||||
onReachBottom(() => {
|
||||
// 审核模式:一次拉取不分页,直接提示到底
|
||||
if (calcAuditModeEnabled.value) {
|
||||
uni.showToast({ icon: 'none', title: t('common.noMoreData') })
|
||||
return
|
||||
}
|
||||
if (result.value.hasNext) {
|
||||
queryParams.value.page += 1
|
||||
isLoadMore.value = true
|
||||
handleGetArticleList()
|
||||
// 正在加载时阻止重复请求
|
||||
if (loadMoreStatus.value.active) {
|
||||
return
|
||||
}
|
||||
else {
|
||||
uni.showToast({ icon: 'none', title: t('common.noMoreData') })
|
||||
// 有更多数据时继续加载
|
||||
if (loadMoreStatus.value.hasNext) {
|
||||
queryParams.value.page += 1
|
||||
updateLoadMoreStatus({
|
||||
active: true,
|
||||
status: 'loading',
|
||||
})
|
||||
handleGetArticleList()
|
||||
}
|
||||
})
|
||||
</script>
|
||||
@@ -251,9 +274,7 @@
|
||||
<uh-article-card v-for="(article, index) in articleList" :key="index" from="home" :article="article"
|
||||
:variant="homeListLayout === 'double' ? 'grid' : 'list'" :audit-mode="calcAuditModeEnabled" />
|
||||
</view>
|
||||
<view class="mt-3 box-border pb-5 text-center text-xs text-gray-400">
|
||||
{{ loadMoreText }}
|
||||
</view>
|
||||
<uh-data-loadmore :status="loadMoreStatus.status" :text="loadMoreStatus.text" />
|
||||
</block>
|
||||
</view>
|
||||
<uh-notify-dialog />
|
||||
|
||||
@@ -62,9 +62,8 @@
|
||||
}
|
||||
|
||||
/* ---------------- 状态 ---------------- */
|
||||
const { loadingStatus, updateLoadingStatus } = useDataLoadingStatus()
|
||||
const { loadingStatus, loadMoreStatus, updateLoadingStatus, updateLoadMoreStatus, resetLoadMoreStatus } = useDataLoadingStatus()
|
||||
const queryParams = ref({ size: 10, page: 1 })
|
||||
const hasNext = ref(false)
|
||||
/** 列表卡片 */
|
||||
type MomentCard = IMoment & {
|
||||
images ?: { type ?: string, url : string }[]
|
||||
@@ -77,8 +76,6 @@
|
||||
weekend : string
|
||||
}
|
||||
const dataList = ref<MomentCard[]>([])
|
||||
const isLoadMore = ref(false)
|
||||
const loadMoreText = ref(t('common.loading'))
|
||||
const videoContexts = ref<Record<string, UniApp.VideoContext | undefined>>({})
|
||||
const currentVideoId = ref<string | null>(null)
|
||||
|
||||
@@ -125,6 +122,8 @@
|
||||
/* ---------------- 数据加载 ---------------- */
|
||||
async function handleGetData() {
|
||||
if (calcAuditModeEnabled.value) {
|
||||
// 审核模式:按 audit-data moments 顺序展示,一次拉取不分页
|
||||
resetLoadMoreStatus()
|
||||
const auditMomentNames = appConfigStore.auditData.spec?.moments || []
|
||||
try {
|
||||
const res = await getMomentList({ page: 1, size: 0 })
|
||||
@@ -136,42 +135,61 @@
|
||||
dataList.value = tempItems
|
||||
await sleep(600)
|
||||
updateLoadingStatus(dataList.value.length === 0 ? DataLoadingStatusEnum.Empty : DataLoadingStatusEnum.Success)
|
||||
loadMoreText.value = t('common.noMore')
|
||||
updateLoadMoreStatus({
|
||||
active: false,
|
||||
status: 'noMore',
|
||||
hasNext: false,
|
||||
})
|
||||
uni.stopPullDownRefresh()
|
||||
}
|
||||
catch (err) {
|
||||
console.error(err)
|
||||
updateLoadingStatus(DataLoadingStatusEnum.Error)
|
||||
loadMoreText.value = t('common.loadFailed')
|
||||
updateLoadMoreStatus({
|
||||
active: false,
|
||||
status: 'error',
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if (!isLoadMore.value) {
|
||||
if (!loadMoreStatus.value.active) {
|
||||
updateLoadingStatus(DataLoadingStatusEnum.Loading)
|
||||
}
|
||||
loadMoreText.value = t('common.loading')
|
||||
|
||||
try {
|
||||
const res = await getMomentList({ ...queryParams.value })
|
||||
loadMoreText.value = res.data.hasNext ? t('common.loadMore') : t('common.noMore')
|
||||
hasNext.value = res.data.hasNext
|
||||
|
||||
const tempItems = res.data.items
|
||||
.filter(x => x.spec.visible === 'PUBLIC')
|
||||
.map(mapMomentItem)
|
||||
|
||||
dataList.value = isLoadMore.value
|
||||
dataList.value = loadMoreStatus.value.active
|
||||
? dataList.value.concat(tempItems)
|
||||
: tempItems
|
||||
|
||||
await sleep(600)
|
||||
updateLoadingStatus(dataList.value.length === 0 ? DataLoadingStatusEnum.Empty : DataLoadingStatusEnum.Success)
|
||||
if (!loadMoreStatus.value.active) {
|
||||
await sleep(600)
|
||||
updateLoadingStatus(dataList.value.length === 0 ? DataLoadingStatusEnum.Empty : DataLoadingStatusEnum.Success)
|
||||
}
|
||||
|
||||
updateLoadMoreStatus({
|
||||
active: false,
|
||||
status: res.data.hasNext ? 'loadMore' : 'noMore',
|
||||
hasNext: res.data.hasNext,
|
||||
})
|
||||
}
|
||||
catch (err) {
|
||||
console.error(err)
|
||||
updateLoadingStatus(DataLoadingStatusEnum.Error)
|
||||
loadMoreText.value = t('common.loadFailed')
|
||||
if (loadMoreStatus.value.active) {
|
||||
updateLoadMoreStatus({
|
||||
active: false,
|
||||
status: 'error',
|
||||
})
|
||||
}
|
||||
else {
|
||||
updateLoadingStatus(DataLoadingStatusEnum.Error)
|
||||
}
|
||||
}
|
||||
finally {
|
||||
uni.stopPullDownRefresh()
|
||||
@@ -282,7 +300,7 @@
|
||||
uni.stopPullDownRefresh()
|
||||
return
|
||||
}
|
||||
isLoadMore.value = false
|
||||
resetLoadMoreStatus()
|
||||
queryParams.value.page = 1
|
||||
videoContexts.value = {}
|
||||
currentVideoId.value = null
|
||||
@@ -292,17 +310,23 @@
|
||||
onReachBottom(() => {
|
||||
if (!uniHaloPluginAvailable.value)
|
||||
return
|
||||
// 审核模式:一次拉取不分页,直接提示到底
|
||||
if (calcAuditModeEnabled.value) {
|
||||
uni.showToast({ icon: 'none', title: t('common.noMoreData') })
|
||||
return
|
||||
}
|
||||
if (hasNext.value) {
|
||||
queryParams.value.page += 1
|
||||
isLoadMore.value = true
|
||||
handleGetData()
|
||||
// 正在加载时阻止重复请求
|
||||
if (loadMoreStatus.value.active) {
|
||||
return
|
||||
}
|
||||
else {
|
||||
uni.showToast({ icon: 'none', title: t('common.noMoreData') })
|
||||
// 有更多数据时继续加载
|
||||
if (loadMoreStatus.value.hasNext) {
|
||||
queryParams.value.page += 1
|
||||
updateLoadMoreStatus({
|
||||
active: true,
|
||||
status: 'loading',
|
||||
})
|
||||
handleGetData()
|
||||
}
|
||||
})
|
||||
</script>
|
||||
@@ -411,9 +435,7 @@
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
<view class="load-text pb-5 pt-1 text-center text-xs text-gray-500">
|
||||
{{ loadMoreText }}
|
||||
</view>
|
||||
<uh-data-loadmore :status="loadMoreStatus.status" :text="loadMoreStatus.text" />
|
||||
</view>
|
||||
</template>
|
||||
</view>
|
||||
|
||||
Reference in New Issue
Block a user