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
+60
View File
@@ -0,0 +1,60 @@
/**
* 点赞交互(文章详情 / 瞬间详情 / 瞬间列表共用)
* 已点赞名单本地持久化(刷新/重进后仍防重复);支持单目标(详情页)与多目标(列表页)。
*/
import { ref } from 'vue'
import { submitUpvote } from '@/api/halo'
import { getCache, setCache } from '@/utils/storage'
const UPVOTE_CACHE_KEY = 'uh_upvoted_names'
function readUpvotedNames(): string[] {
const list = getCache<string[]>(UPVOTE_CACHE_KEY)
return Array.isArray(list) ? list : []
}
export function useUpvote(plural: 'posts' | 'moments', getName: () => string | undefined) {
const upvotedNames = ref<string[]>(readUpvotedNames())
/** 判断是否已点赞(可指定 name;缺省用单目标 getName) */
function hasUpvoted(name?: string): boolean {
const n = name ?? getName()
return !!n && upvotedNames.value.includes(n)
}
/** 按 name 点赞(列表多目标场景;成功后持久化 + 回调) */
async function likeByName(name: string, onSuccess?: (name: string) => void) {
if (!name)
return
if (upvotedNames.value.includes(name)) {
uni.showToast({ icon: 'none', title: '已经点过赞啦!' })
return
}
try {
await submitUpvote({ group: 'content.halo.run', plural, name })
uni.showToast({ icon: 'none', title: '点赞成功!' })
upvotedNames.value.push(name)
setCache(UPVOTE_CACHE_KEY, upvotedNames.value)
onSuccess?.(name)
}
catch (err) {
console.error('点赞失败', err)
uni.showToast({ icon: 'none', title: '点赞失败' })
}
}
/** 点赞当前目标(详情页单目标场景) */
async function handleDoLikes(onSuccess?: (name: string) => void) {
const name = getName()
if (!name)
return
await likeByName(name, onSuccess)
}
return {
upvotedNames,
hasUpvoted,
handleDoLikes,
likeByName,
}
}