1
0
mirror of https://github.com/ialley-workshop-open/uni-halo.git synced 2026-11-13 22:11:42 +08:00

refactor: 优化代码结构与功能,修复细节问题

1.  新增首页精选分类配置类型定义与逻辑,支持自定义首页分类展示顺序
2.  移除冗余的qs导入与注释代码,简化请求参数处理
3.  修复瞬间详情页悬浮操作的渲染问题与收藏按钮样式
4.  优化文章卡片的头像处理与分类跳转逻辑
5.  重构数据加载组件,新增尺寸配置与样式优化
6.  为分类文章页添加排序切换功能,支持默认/置顶/最新/最旧排序
7.  优化首页分类模块的加载状态与数据获取逻辑,添加延迟模拟
8.  统一API文件的导入格式与代码风格
This commit is contained in:
小莫唐尼
2026-09-09 20:41:00 +08:00
parent 4c623be4c0
commit eb44980484
8 changed files with 307 additions and 252 deletions
@@ -3,7 +3,7 @@ import { ref } from 'vue'
import { onLoad, onPullDownRefresh, onReachBottom, onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'
import { getCategoryPostList } from '@/api/halo'
import { DataLoadingStatusEnum, useDataLoadingStatus } from '@/hooks/useDataLoadingStatus'
import type { IPost } from '@/api/types/halo'
import type { IPost, IPostListReq } from '@/api/types/halo'
definePage({
style: {
@@ -23,6 +23,24 @@ const dataList = ref<IPost[]>([])
const isLoadMore = ref(false)
const loadMoreText = ref('')
/* ---------------- 排序切换(sort 参数由接口透传,见 IPostListReq.sort) ---------------- */
const sortOptions: { key: string; label: string; sort?: string[] }[] = [
{ key: 'default', label: '默认' },
{ key: 'pinned', label: '按置顶', sort: ['spec.pinned,desc'] },
{ key: 'latest', label: '按最新', sort: ['metadata.creationTimestamp,desc'] },
{ key: 'oldest', label: '按最旧', sort: ['metadata.creationTimestamp,asc'] },
]
const activeSort = ref('default')
function handleSortChange(key: string) {
if (activeSort.value === key)
return
activeSort.value = key
isLoadMore.value = false
queryParams.value.page = 0
handleGetData()
}
async function handleGetData() {
if (!isLoadMore.value) {
updateLoadingStatus(DataLoadingStatusEnum.Loading)
@@ -30,7 +48,15 @@ async function handleGetData() {
loadMoreText.value = '加载中...'
try {
const res = await getCategoryPostList(name.value, { ...queryParams.value })
const reqParams: Record<string, unknown> = { ...queryParams.value }
const currentSort = sortOptions.find(opt => opt.key === activeSort.value)?.sort
if (currentSort) {
reqParams.sort = currentSort
}
else {
delete reqParams.sort
}
const res = await getCategoryPostList(name.value, reqParams as IPostListReq)
navbarTitle.value = `${pageTitle.value} (共${res.data.total}篇)`
hasNext.value = res.data.hasNext
dataList.value = isLoadMore.value
@@ -62,16 +88,6 @@ function handleToArticleDetail(article: IPost) {
})
}
function handleToTopPage(duration = 500) {
uni.pageScrollTo({
scrollTop: 0,
duration,
fail: (err) => {
console.error('回顶失败', err)
},
})
}
onLoad((options) => {
name.value = options?.name || ''
pageTitle.value = options?.title || '分类详情'
@@ -112,6 +128,19 @@ onShareTimeline(() => ({
<!-- 自定义导航 -->
<uh-navbar :default-title="navbarTitle" title-color="text-gray-900" />
<!-- 排序切换:默认 / 按置顶 / 按最新 / 按最旧 -->
<view class="box-border flex items-center gap-2 px-3 py-2">
<view
v-for="opt in sortOptions"
:key="opt.key"
class="rounded-full px-3 py-1 text-xs"
:class="activeSort === opt.key ? 'bg-secondary font-bold' : 'uh-global-card-glass shadow-none border text-gray-500'"
@click="handleSortChange(opt.key)"
>
{{ opt.label }}
</view>
</view>
<!-- 加载/错误/空占位(状态机) -->
<view v-if="loadingStatus !== 'success'">
<uh-data-loading
@@ -122,18 +151,16 @@ onShareTimeline(() => ({
</view>
<block v-else>
<uh-article-card
v-for="(article, index) in dataList"
:key="index"
:article="article"
@on-click="handleToArticleDetail"
/>
<view class="load-text py-5 text-center text-[24rpx] text-[#999]">
{{ loadMoreText }}
</view>
<view class="to-top-btn fixed bottom-[100rpx] right-6 z-6 h-[72rpx] w-[72rpx] flex items-center justify-center rounded-full bg-white shadow-sm" @click="handleToTopPage()">
<wd-icon name="arrow-up" size="20px" color="#03a9f4" />
<view class="box-border flex flex-col gap-y-3 p-3">
<uh-article-card
v-for="(article, index) in dataList"
:key="index"
:article="article"
@on-click="handleToArticleDetail"
/>
<view class="load-text py-5 text-center text-[24rpx] text-[#999]">
{{ loadMoreText }}
</view>
</view>
</block>
</view>
@@ -362,7 +362,7 @@
kind="Moment" :disallow-comment="!moment.spec.allowComment" @on-comment="handleOnComment" />
</view>
<!-- 悬浮操作(与文章详情一致:点赞/评论/收藏) -->
<view class="fixed bottom-8 left-1/2 z-10 flex items-center justify-center pb-safe -translate-x-1/2">
<view v-if="moment" class="fixed bottom-8 left-1/2 z-10 flex items-center justify-center pb-safe -translate-x-1/2">
<view
class="uh-global-card-glass box-border flex items-center justify-center gap-2 border rounded-full p-1 text-primary">
<!-- 点赞 -->
@@ -384,8 +384,8 @@
class="uh-global-card-glass box-border h-[72rpx] flex flex-1 items-center justify-center gap-x-1 border rounded-full px-4 shadow-none"
@click="handleToggleMomentFavorite">
<wd-icon class-prefix="uhemoji-icon" name="-smile-" size="36rpx" />
<text class="shrink-0 text-sm text-gray-900 font-semibold"
:style="momentFavorited ? { color: '#ffb300' } : ''">{{ momentFavorited ? '已收藏' : '收藏' }}</text>
<text class="shrink-0 text-sm font-semibold"
:class="momentFavorited ? 'text-primary' : 'text-gray-900'">{{ momentFavorited ? '已收藏' : '收藏' }}</text>
</view>
</view>
</view>