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

refactor: 抽离点赞hook并统一点赞逻辑,优化评论列表组件

1. 新增useUpvote点赞hook,统一文章、瞬间的点赞逻辑与本地缓存
2. 重构文章详情、瞬间详情页的点赞代码,移除重复实现
3. 为评论列表组件添加kind参数支持动态类型,新增refresh暴露方法与全局刷新监听
4. 优化瞬间列表页的点赞、评论交互与样式
This commit is contained in:
小莫唐尼
2026-09-09 18:40:14 +08:00
parent d528d214b5
commit b793920097
5 changed files with 192 additions and 69 deletions
@@ -1,7 +1,8 @@
<script lang="ts" setup>
import { computed, ref } from 'vue'
import { onLoad, onPullDownRefresh, onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'
import { getPostByName, getPostCommentReplyList, postTrackersCounter, submitUpvote } from '@/api/halo'
import { getPostByName, getPostCommentReplyList, postTrackersCounter } from '@/api/halo'
import { useUpvote } from '@/hooks/useUpvote'
import { createVerificationCode, requestRestrictReadCheck } from '@/api/uni-halo'
import { formatTime } from '@/utils/formatTime'
import { useAppConfigStore } from '@/store/appConfig'
@@ -188,36 +189,14 @@
}
/* ---------------- 点赞 ---------------- */
const upvotedNames = ref<string[]>([])
const { hasUpvoted, handleDoLikes } = useUpvote('posts', () => result.value?.metadata.name)
function hasUpvoted() : boolean {
return upvotedNames.value.includes(result.value?.metadata.name || '')
}
async function handleDoLikes() {
if (!result.value) {
return
}
if (hasUpvoted()) {
uni.showToast({ icon: 'none', title: '已经点过赞啦!' })
return
}
try {
await submitUpvote({
group: 'content.halo.run',
plural: 'posts',
name: result.value.metadata.name,
})
uni.showToast({ icon: 'none', title: '点赞成功!' })
upvotedNames.value.push(result.value.metadata.name)
if (result.value.stats) {
function handleDoLikesClick() {
handleDoLikes((name) => {
if (result.value?.stats) {
result.value.stats.upvote = (result.value.stats.upvote || 0) + 1
}
}
catch (err) {
console.error('点赞失败', err)
uni.showToast({ icon: 'none', title: '点赞失败' })
}
})
}
/* ---------------- 收藏 ---------------- */
@@ -358,6 +337,13 @@
commentModal.value.show = false
}
/** 评论列表加载完成:同步评论计数(统计区展示) */
function handleCommentLoaded(list : IComment[]) {
if (result.value?.stats) {
result.value.stats.comment = list.length
}
}
function handleOnShowCommentDetail(data : { postName : string, comment : IComment }) {
commentDetail.value = {
show: true,
@@ -595,8 +581,8 @@
<!-- 评论区域 -->
<uh-comment-list v-if="calcIsShowComment && result" :disallow-comment="!result.spec.allowComment"
:post-name="result.metadata.name" :post="result" @on-comment="handleOnComment"
@on-comment-detail="handleOnShowCommentDetail" />
:post-name="result.metadata.name" :post="result" @on-comment="handleOnComment"
@on-comment-detail="handleOnShowCommentDetail" @on-loaded="handleCommentLoaded" />
</view>
</view>
@@ -607,7 +593,7 @@
class="uh-global-card-glass box-border flex items-center justify-center gap-2 border rounded-full p-1 text-primary">
<view
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"
:class="{ active: hasUpvoted() }" @click="handleDoLikes">
:class="{ active: hasUpvoted() }" @click="handleDoLikesClick">
<wd-icon class-prefix="uhemoji-icon" name="-kiss-" size="36rpx" />
<text class="shrink-0 text-sm text-gray-900 font-semibold">点赞</text>
</view>
+31 -29
View File
@@ -1,7 +1,8 @@
<script lang="ts" setup>
import { computed, ref } from 'vue'
import { onLoad, onPullDownRefresh, onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'
import { getMomentByName, submitUpvote } from '@/api/halo'
import { getMomentByName } from '@/api/halo'
import { useUpvote } from '@/hooks/useUpvote'
import { useAppConfigStore } from '@/store/appConfig'
import { useFavoritesStore } from '@/store/favorites'
import { checkAvatarUrl, checkThumbnailUrl } from '@/utils/url'
@@ -138,35 +139,15 @@
}
/* ---------------- 点赞 ---------------- */
const upvotedNames = ref<string[]>([])
const { hasUpvoted, handleDoLikes } = useUpvote('moments', () => moment.value?.metadata.name)
function hasUpvoted() : boolean {
return upvotedNames.value.includes(moment.value?.metadata.name || '')
}
async function handleDoLikes() {
const current = moment.value
if (!current) { return }
if (hasUpvoted()) {
uni.showToast({ icon: 'none', title: '已经点过赞啦!' })
return
}
try {
await submitUpvote({
group: 'content.halo.run',
plural: 'moments',
name: current.metadata.name,
})
uni.showToast({ icon: 'none', title: '点赞成功!' })
upvotedNames.value.push(current.metadata.name)
if (current.stats) {
function handleDoLikesClick() {
handleDoLikes((name) => {
const current = moment.value
if (current?.stats) {
current.stats.upvote = (current.stats.upvote || 0) + 1
}
}
catch (err) {
console.error('点赞失败', err)
uni.showToast({ icon: 'none', title: '点赞失败' })
}
})
}
/* ---------------- 评论 ---------------- */
@@ -176,6 +157,8 @@
postName: '',
title: '',
})
/** 评论列表组件实例(评论成功后刷新) */
const commentListRef = ref<{ refresh : () => void } | null>(null)
function handleToComment() {
const current = moment.value
@@ -193,8 +176,23 @@
}
}
/** 评论列表触发(回复某条评论/新增) */
function handleOnComment(data : { isComment : boolean, postName : string, title : string }) {
commentModal.value = {
show: true,
isComment: data.isComment,
postName: data.postName,
title: data.title,
}
}
function handleOnCommentModalClose(data : { refresh : boolean, isSubmit : boolean }) {
commentModal.value.show = false
if (data.isSubmit) {
// 评论成功后刷新评论列表与计数
commentListRef.value?.refresh()
loadMoment()
}
}
/* ---------------- 视频互斥 ---------------- */
@@ -358,6 +356,10 @@
</view>
</view>
</view>
<!-- 评论列表(瞬间评论,kind=Moment) -->
<uh-comment-list v-if="moment" ref="commentListRef" :post-name="moment.metadata.name" :post="moment"
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">
@@ -366,12 +368,12 @@
<!-- 点赞 -->
<view
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"
:class="{ active: hasUpvoted() }" @click="handleDoLikes">
:class="{ active: hasUpvoted() }" @click="handleDoLikesClick">
<wd-icon class-prefix="uhemoji-icon" name="-kiss-" size="36rpx" />
<text class="shrink-0 text-sm text-gray-900 font-semibold">点赞</text>
</view>
<!-- 评论 -->
<view
<view v-if="moment.spec.allowComment"
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="handleToComment()">
<wd-icon class-prefix="uhemoji-icon" name="-thinking" size="36rpx" />