diff --git a/src/components/uh-data-loading/uh-data-loading.vue b/src/components/uh-data-loading/uh-data-loading.vue
index 82fba98..fd2aade 100644
--- a/src/components/uh-data-loading/uh-data-loading.vue
+++ b/src/components/uh-data-loading/uh-data-loading.vue
@@ -20,7 +20,7 @@
loadingStatus: 'loading',
size: 'large',
minHeight: '80vh',
- loadingText: '稍等,正在加载中哦',
+ loadingText: '稍等,正在加载中哦~',
errorText: '哎呀,加载失败了呢~',
emptyText: '啊偶,暂时没有数据呢~',
loadingSubText: '',
diff --git a/src/components/uh-data-loadmore/uh-data-loadmore.vue b/src/components/uh-data-loadmore/uh-data-loadmore.vue
new file mode 100644
index 0000000..b3a7a2b
--- /dev/null
+++ b/src/components/uh-data-loadmore/uh-data-loadmore.vue
@@ -0,0 +1,15 @@
+
+
+
+
+ {{ props.text }}
+
+
\ No newline at end of file
diff --git a/src/hooks/useDataLoading.test.ts b/src/hooks/useDataLoading.test.ts
deleted file mode 100644
index 10145a2..0000000
--- a/src/hooks/useDataLoading.test.ts
+++ /dev/null
@@ -1,84 +0,0 @@
-import { mount } from '@vue/test-utils'
-import { describe, expect, it, vi } from 'vitest'
-import { defineComponent, h } from 'vue'
-import { useDataLoading } from './useDataLoading'
-
-/**
- * 在 Vue 应用上下文中运行 composable。
- * composable 的 ref/computed 只能在 setup() 内使用,
- * withSetup 通过挂载一个临时组件来提供这个上下文。
- */
-function withSetup(composableFn: () => T): T {
- let result!: T
- const Comp = defineComponent({
- setup() {
- result = composableFn()
- return () => h('div')
- },
- })
- const wrapper = mount(Comp)
- wrapper.unmount()
- return result
-}
-
-describe('useDataLoading', () => {
- it('初始状态:status=loading, data=undefined', () => {
- const fetcher = vi.fn().mockResolvedValue('data')
- const { status, data } = withSetup(() => useDataLoading(fetcher))
-
- expect(status.value).toBe('loading')
- expect(data.value).toBeUndefined()
- })
-
- it('run 成功:status 从 loading 流转到 success,data 更新为返回值并触发 onSuccess', async () => {
- const fetcher = vi.fn().mockResolvedValue('result')
- const onSuccess = vi.fn()
- const { status, data, run } = withSetup(() => useDataLoading(fetcher, { onSuccess }))
-
- const promise = run()
- expect(status.value).toBe('loading')
-
- await promise
- expect(status.value).toBe('success')
- expect(data.value).toBe('result')
- expect(onSuccess).toHaveBeenCalledWith('result')
- })
-
- it('run 返回空数组:status 流转到 empty', async () => {
- const fetcher = vi.fn().mockResolvedValue([])
- const { status, run } = withSetup(() => useDataLoading(fetcher))
-
- await run()
- expect(status.value).toBe('empty')
- })
-
- it('isEmpty 自定义判空:返回 true 时 status 为 empty', async () => {
- const fetcher = vi.fn(async (): Promise<{ total: number, items: number[] }> => ({ total: 0, items: [1] }))
- const { status, run } = withSetup(() => useDataLoading(fetcher, { isEmpty: data => data.total === 0 }))
-
- await run()
- expect(status.value).toBe('empty')
- })
-
- it('run 失败:不向外抛出异常,status 流转到 error 并触发 onError', async () => {
- const err = new Error('network error')
- const onError = vi.fn()
- const fetcher = vi.fn().mockRejectedValue(err)
- const { status, data, run } = withSetup(() => useDataLoading(fetcher, { onError }))
-
- await expect(run()).resolves.toBeUndefined()
-
- expect(status.value).toBe('error')
- expect(data.value).toBeUndefined()
- expect(onError).toHaveBeenCalledWith(err)
- })
-
- it('带参数 run:参数透传给 fetcher', async () => {
- const fetcher = vi.fn(async (name: string) => `hi ${name}`)
- const { data, run } = withSetup(() => useDataLoading(fetcher))
-
- await run('world')
- expect(fetcher).toHaveBeenCalledWith('world')
- expect(data.value).toBe('hi world')
- })
-})
diff --git a/src/hooks/useDataLoading.ts b/src/hooks/useDataLoading.ts
index 8fb7520..40d0b39 100644
--- a/src/hooks/useDataLoading.ts
+++ b/src/hooks/useDataLoading.ts
@@ -2,7 +2,7 @@ import type { Ref } from 'vue'
import { ref } from 'vue'
/** 数据加载四态(取值与旧 DataLoadingStatusEnum 一致,二者可互相兼容) */
-export type DataLoadingStatus = 'loading' | 'error' | 'empty' | 'success'
+export type DataLoadingStatus = 'loading' | 'error' | 'empty' | 'success' |'no-network'
export interface IUseDataLoadingOptions {
/** 自定义判空;默认:数组看长度、对象看键数、空值视为空 */
diff --git a/src/hooks/useDataLoadingStatus.ts b/src/hooks/useDataLoadingStatus.ts
index 5505a8f..09c5e04 100644
--- a/src/hooks/useDataLoadingStatus.ts
+++ b/src/hooks/useDataLoadingStatus.ts
@@ -1,27 +1,82 @@
-import type { DataLoadingStatus } from './useDataLoading'
-
export const DataLoadingStatusEnum = {
- Loading: 'loading',
- Error: 'error',
- Empty: 'empty',
- Success: 'success',
- NoNetwork: 'noNetwork',
-} as const
+ Loading: 'loading',
+ Error: 'error',
+ Empty: 'empty',
+ Success: 'success',
+ NoNetwork: 'noNetwork'
+} as const;
-export function useDataLoadingStatus(status?: DataLoadingStatus) {
- const loadingStatus = ref(status ?? DataLoadingStatusEnum.Loading)
+export type DataLoadingStatus = (typeof DataLoadingStatusEnum)[keyof typeof DataLoadingStatusEnum];
- function resetLoadingStatus() {
- loadingStatus.value = DataLoadingStatusEnum.Loading
- }
+const LoadMoreStatusText = {
+ loadMore: '哎嘿,往下还有数据哦~',
+ loading: '稍等,正在加载中哦~',
+ noMore: '呜呜,没有更多数据了~',
+ error: '呜呜,加载失败了哦~',
+ noNetwork: '呜呜,网络好像有点问题哦~'
+} as const;
- function updateLoadingStatus(newStatus: DataLoadingStatus) {
- loadingStatus.value = newStatus
- }
+export type LoadMoreStatus = keyof typeof LoadMoreStatusText
- return {
- loadingStatus,
- resetLoadingStatus,
- updateLoadingStatus,
- }
+export interface LoadMoreStatusOption {
+ // 是否激活加载
+ active?: boolean;
+ status?: LoadMoreStatus;
+ text?: string;
+ hasNext?: boolean;
+}
+
+export function useDataLoadingStatus(defaultStatus?: DataLoadingStatus) {
+ const loadingStatus = ref(defaultStatus ?? DataLoadingStatusEnum.Loading);
+
+ const _loadMoreStatus = ref({
+ active: false,
+ hasNext: false,
+ status: 'loadMore',
+ text: ''
+ });
+
+ const loadMoreStatus = computed(() => {
+ const result = {
+ ..._loadMoreStatus.value
+ };
+ if (!_loadMoreStatus.value.text) {
+ result.text = LoadMoreStatusText[_loadMoreStatus.value.status];
+ }
+ console.log('会触发吗?', _loadMoreStatus.value);
+ console.log('会触发吗 result', result);
+ return result;
+ });
+
+ function resetLoadMoreStatus() {
+ _loadMoreStatus.value.active = false;
+ _loadMoreStatus.value.hasNext = false;
+ _loadMoreStatus.value.status = 'loadMore';
+ _loadMoreStatus.value.text = '';
+ }
+
+ function resetLoadingStatus() {
+ loadingStatus.value = DataLoadingStatusEnum.Loading;
+ resetLoadMoreStatus();
+ }
+
+ function updateLoadingStatus(status: DataLoadingStatus) {
+ loadingStatus.value = status;
+ }
+
+ function updateLoadMoreStatus(status: LoadMoreStatusOption) {
+ _loadMoreStatus.value.active = status.active;
+ _loadMoreStatus.value.status = status.status;
+ _loadMoreStatus.value.hasNext = status.hasNext;
+ _loadMoreStatus.value.text = status.text;
+ }
+
+ return {
+ loadingStatus,
+ loadMoreStatus,
+ resetLoadingStatus,
+ resetLoadMoreStatus,
+ updateLoadingStatus,
+ updateLoadMoreStatus
+ };
}
diff --git a/src/pages-blog/banner-detail/banner-detail.vue b/src/pages-blog/banner-detail/banner-detail.vue
index e6c9f98..e1bebff 100644
--- a/src/pages-blog/banner-detail/banner-detail.vue
+++ b/src/pages-blog/banner-detail/banner-detail.vue
@@ -2,7 +2,7 @@
/**
* 轮播详情页(自定义 Banner 条目点击后进入)
* 通过公开详情接口(getBannerDetail)拉取 content 富文本与外链,类文章详情页
- * 外链平台差异(条件编译):非 APP-PLUS(小程序/H5)提供复制+显示链接,APP-PLUS 提供访问按钮(web-view)
+ * 外链平台差异(条件编译):非 APP-PLUS(小程序/H5)提供复制链接,APP-PLUS 提供访问按钮(web-view)
*/
import { computed, ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
@@ -10,176 +10,146 @@ import { getBannerDetail } from '@/api/uni-halo'
import { checkAvatarUrl, checkThumbnailUrl } from '@/utils/url'
import { formatTime } from '@/utils/formatTime'
import { copyToClipboard } from '@/utils/restrictRead'
+import { sleep } from '@/utils/common'
+import { markdownConfig } from '@/config/markdown'
+import { DataLoadingStatusEnum, useDataLoadingStatus } from '@/hooks/useDataLoadingStatus'
import type { IBannerPublicDetail } from '@/api/types/uni-halo'
definePage({
- style: {
- navigationBarTitleText: '轮播详情',
- },
+ style: {
+ navigationBarTitleText: '轮播详情',
+ navigationStyle: 'custom',
+ },
})
-const loading = ref<'loading' | 'success' | 'error'>('loading')
+const { loadingStatus, updateLoadingStatus } = useDataLoadingStatus()
+const name = ref('')
const detail = ref(null)
const linkCopied = ref(false)
-async function handleGetData(name: string) {
- loading.value = 'loading'
- try {
- const res = await getBannerDetail(name)
- detail.value = res.data || null
- if (detail.value?.title) {
- uni.setNavigationBarTitle({ title: detail.value.title })
- }
- loading.value = 'success'
- }
- catch (err) {
- console.error('获取轮播详情失败', err)
- loading.value = 'error'
- }
-}
-
-function handleRetry() {
- const name = detail.value?.name || ''
- if (name) {
- handleGetData(name)
- }
-}
-
-onLoad((options) => {
- const name = options?.name
- if (name) {
- handleGetData(name)
- }
- else {
- loading.value = 'error'
- }
-})
-
+/** 封面图:无封面时回退默认缩略图 */
const coverUrl = computed(() => checkThumbnailUrl(detail.value?.cover, true))
const authorName = computed(() => detail.value?.authorName || '')
const authorAvatar = computed(() =>
- detail.value?.authorAvatar ? checkAvatarUrl(detail.value.authorAvatar) : '',
+ detail.value?.authorAvatar ? checkAvatarUrl(detail.value.authorAvatar) : '',
)
const dateText = computed(() => {
- if (!detail.value?.date) {
- return ''
- }
- const d = new Date(detail.value.date)
- if (Number.isNaN(d.getTime())) {
- return ''
- }
- return formatTime({ d: detail.value.date, f: 'yyyy-MM-dd' })
+ if (!detail.value?.date) {
+ return ''
+ }
+ const d = new Date(detail.value.date)
+ if (Number.isNaN(d.getTime())) {
+ return ''
+ }
+ return formatTime({ d: detail.value.date, f: 'yyyy-MM-dd' })
+})
+
+const content = computed(() => detail.value?.content || '')
+
+/** 外链存在即展示底部操作(复制/访问) */
+const hasLink = computed(() => !!detail.value?.link)
+
+/** 加载轮播详情(状态机;404/无数据 → 空态,其余错误 → error 态,可重试) */
+async function loadDetail() {
+ updateLoadingStatus(DataLoadingStatusEnum.Loading)
+ if (!name.value) {
+ updateLoadingStatus(DataLoadingStatusEnum.Empty)
+ return
+ }
+ try {
+ const res = await getBannerDetail(name.value)
+ detail.value = res.data || null
+ await sleep(600)
+ updateLoadingStatus(detail.value ? DataLoadingStatusEnum.Success : DataLoadingStatusEnum.Empty)
+ }
+ catch (err) {
+ console.error('轮播详情加载失败', err)
+ const code = (err as { code ?: number }).code
+ updateLoadingStatus(code === 404 ? DataLoadingStatusEnum.Empty : DataLoadingStatusEnum.Error)
+ }
+}
+
+onLoad((options) => {
+ name.value = options?.name || ''
+ loadDetail()
})
/** 非 APP-PLUS:复制链接 */
function handleCopyLink() {
- if (!detail.value?.link) {
- return
- }
- copyToClipboard(detail.value.link, '链接已复制')
- linkCopied.value = true
- setTimeout(() => {
- linkCopied.value = false
- }, 1500)
+ if (!detail.value?.link) {
+ return
+ }
+ copyToClipboard(detail.value.link, '链接已复制')
+ linkCopied.value = true
+ setTimeout(() => {
+ linkCopied.value = false
+ }, 1500)
}
/** APP-PLUS:打开内置 web-view 访问外链 */
function handleOpenLink() {
- if (!detail.value?.link) {
- return
- }
- uni.navigateTo({
- url: `/pages-blog/website/website?data=${JSON.stringify({
- title: detail.value.title || '查看链接',
- url: encodeURIComponent(detail.value.link),
- })}`,
- })
+ if (!detail.value?.link) {
+ return
+ }
+ uni.navigateTo({
+ url: `/pages-blog/website/website?data=${JSON.stringify({
+ title: detail.value.title || '查看链接',
+ url: encodeURIComponent(detail.value.link),
+ })}`,
+ })
}
-
-
-
-
-
+
+
+
-
-
-
-
+
+
-
-
-
- {{ detail.title }}
-
+
+
+
-
-
-
- {{ authorName }}
- {{ dateText }}
-
+
+ {{ detail.title }}
+
-
-
-
-
+
+
+
+ {{ authorName }}
+ {{ dateText }}
+
-
-
-
- 相关链接
-
-
- {{ detail.link }}
-
-
-
-
- {{ linkCopied ? '已复制' : '复制链接' }}
-
-
-
-
-
- 访问链接
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+ {{ linkCopied ? '已复制' : '复制链接' }}
+
+
+
+
+ 访问链接
+
+
+
+
+
+
-
-
diff --git a/src/pages/maintenance/maintenance.vue b/src/pages/maintenance/maintenance.vue
index cca5623..34e9cf3 100644
--- a/src/pages/maintenance/maintenance.vue
+++ b/src/pages/maintenance/maintenance.vue
@@ -329,7 +329,7 @@
{{ line }}
- 为了给你带来更好的体验,站点正在维护升级中
+ 为了给你带来更好的体验,正在维护升级中
别担心,我们很快就回来!
diff --git a/src/pages/tabbar/category/category.vue b/src/pages/tabbar/category/category.vue
index 7fa5f32..5f55bc2 100644
--- a/src/pages/tabbar/category/category.vue
+++ b/src/pages/tabbar/category/category.vue
@@ -25,23 +25,18 @@
const categoryConfig = computed(() => haloConfigs.value.pageConfig?.categoryConfig)
/* ---------------- 状态 ---------------- */
- const { loadingStatus, updateLoadingStatus } = useDataLoadingStatus()
+ const { loadingStatus, loadMoreStatus, updateLoadingStatus, updateLoadMoreStatus, resetLoadingStatus, resetLoadMoreStatus } = useDataLoadingStatus()
const queryParams = ref({
- size: 20,
+ size: 3,
page: 1,
fieldSelector: ['spec.hideFromList=false'],
})
- const hasNext = ref(false)
const dataList = ref([])
- const isLoadMore = ref(false)
- const loadMoreText = ref(t('common.loading'))
function handleResetInit() {
dataList.value = []
queryParams.value.page = 1
- hasNext.value = false
- isLoadMore.value = false
- loadMoreText.value = t('common.loading')
+ resetLoadingStatus()
}
function handleInitPage() {
@@ -51,11 +46,12 @@
/* ---------------- 数据加载 ---------------- */
async function handleGetData() {
- updateLoadingStatus(DataLoadingStatusEnum.Loading)
- // 增加延迟,提升用户体验
- await sleep(800)
- // 审核模式:直接使用审核配置分类快照(categoryDetails)映射 ICategory,免请求
+ if (!loadMoreStatus.value.active) {
+ updateLoadingStatus(DataLoadingStatusEnum.Loading)
+ }
+ // 审核模式
if (calcAuditModeEnabled.value) {
+ resetLoadMoreStatus()
const auditCategoryDetails = appConfigStore.auditData.categoryDetails || []
try {
dataList.value = auditCategoryDetails.map(item => ({
@@ -68,51 +64,60 @@
},
postCount: item.postCount ?? 0,
} as ICategory))
+ await sleep(600)
updateLoadingStatus(dataList.value.length === 0 ? DataLoadingStatusEnum.Empty : DataLoadingStatusEnum.Success)
- loadMoreText.value = t('common.noMore')
- uni.hideLoading()
+ updateLoadMoreStatus({
+ active: false,
+ status: 'noMore',
+ hasNext: false
+ })
uni.stopPullDownRefresh()
}
catch (err) {
console.error(err)
updateLoadingStatus(DataLoadingStatusEnum.Error)
- loadMoreText.value = t('common.loadFailed')
}
return
}
- if (!isLoadMore.value) {
- updateLoadingStatus(DataLoadingStatusEnum.Loading)
- }
- loadMoreText.value = t('common.loading')
-
try {
const res = await getCategoryList({ ...queryParams.value })
- loadMoreText.value = res.data.hasNext ? t('common.loadMore') : t('common.noMore')
- hasNext.value = res.data.hasNext
-
const tempItems = res.data.items.map(item => ({
...item,
postCount: item.postCount ?? 0,
spec: { ...item.spec, cover: checkThumbnailUrl(item.spec.cover, true) },
}))
- dataList.value = isLoadMore.value
+ dataList.value = loadMoreStatus.value.active
? dataList.value.concat(tempItems)
: tempItems
- 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 {
- setTimeout(() => {
- uni.hideLoading()
- uni.stopPullDownRefresh()
- }, 500)
+ uni.stopPullDownRefresh()
}
}
@@ -135,25 +140,26 @@
})
onReachBottom(() => {
- if (calcAuditModeEnabled.value) {
- uni.showToast({ icon: 'none', title: t('common.noMoreData') })
- return
+ // 如果正在请求,需要阻止继续发起请求
+ if (loadMoreStatus.value.active && loadMoreStatus.value.loading) {
+ return;
}
- if (hasNext.value) {
+ // 有更多数据时,继续加载数据
+ if (loadMoreStatus.value.hasNext) {
queryParams.value.page += 1
- isLoadMore.value = true
+ updateLoadMoreStatus({
+ active: true,
+ status: 'loading'
+ })
handleGetData()
- }
- else {
- uni.showToast({ icon: 'none', title: t('common.noMoreData') })
- }
+ }
})
-
+
@@ -174,9 +180,7 @@
-
- {{ loadMoreText }}
-
+
\ No newline at end of file