mirror of
https://github.com/ialley-workshop-open/uni-halo.git
synced 2026-11-13 22:11:42 +08:00
refactor: 优化滚动监听逻辑与评论系统,新增对齐alova的meta参数
1. 重构usePageScroll hook,支持自定义默认滚动值并暴露更新方法 2. 全局页面统一接入onPageScroll监听,替换原有内置日志打印 3. 新增评论可见回复数、回复引用、分页回复加载等评论扩展能力 4. 调整全局操作按钮位置与部分UI样式细节 5. 优化请求拦截器,对齐alova参数格式重构query处理逻辑 6. 完善评论弹窗与评论列表组件,实现抖音式折叠回复交互
This commit is contained in:
@@ -4,10 +4,10 @@
|
||||
import relativeTime from 'dayjs/plugin/relativeTime'
|
||||
import { checkAvatarUrl } from '@/utils/url'
|
||||
import { useSettingStore } from '@/store/setting'
|
||||
import type { IComment } from '@/api/types/halo'
|
||||
import type { ICommentReply } from '@/api/types/halo'
|
||||
|
||||
const props = withDefaults(defineProps<{
|
||||
comment : IComment
|
||||
comment : ICommentReply
|
||||
isChild ?: boolean
|
||||
useActions ?: boolean
|
||||
useSolid ?: boolean
|
||||
@@ -15,6 +15,8 @@
|
||||
disallowComment ?: boolean
|
||||
postName ?: string
|
||||
classItem ?: string[]
|
||||
/** 回复引用名映射(quoteReply name -> 被引用人 displayName),命中时显示「回复 @xxx」 */
|
||||
quoteReplyMap ?: Record<string, string>
|
||||
}>(), {
|
||||
isChild: false,
|
||||
useActions: true,
|
||||
@@ -23,11 +25,12 @@
|
||||
disallowComment: false,
|
||||
postName: '',
|
||||
classItem: () => [],
|
||||
quoteReplyMap: () => ({}),
|
||||
})
|
||||
const emit = defineEmits<{
|
||||
(e : 'on-comment', data : { type : string, comment : IComment }) : void
|
||||
(e : 'on-comment', data : { type : string, comment : ICommentReply }) : void
|
||||
(e : 'on-copy', raw : string) : void
|
||||
(e : 'on-detail', comment : IComment) : void
|
||||
(e : 'on-detail', comment : ICommentReply) : void
|
||||
}>()
|
||||
dayjs.extend(relativeTime)
|
||||
dayjs.locale('zh-cn')
|
||||
@@ -37,6 +40,15 @@
|
||||
|
||||
const avatar = computed(() => checkAvatarUrl(props.comment.spec.owner.avatar))
|
||||
|
||||
/** 引用回复标识(被引用人在父级已加载回复映射中可查到时显示) */
|
||||
const quoteReplyText = computed(() => {
|
||||
const quoteName = props.comment.spec.quoteReply
|
||||
if (!quoteName)
|
||||
return ''
|
||||
const displayName = props.quoteReplyMap[quoteName]
|
||||
return displayName ? `回复 @${displayName}` : ''
|
||||
})
|
||||
|
||||
/** 评论时间 */
|
||||
const createTimeText = computed(() => {
|
||||
const time = props.comment.spec.creationTime
|
||||
@@ -77,11 +89,12 @@
|
||||
<view class="text-sm text-gray-500">
|
||||
<text class="text-grey text-xs">{{ comment.spec.owner.displayName }}</text>
|
||||
</view>
|
||||
<view class="mt-0.5 box-border text-sm text-gray-900 leading-5" @click="handleOnDetail"
|
||||
<view v-if="quoteReplyText" class="mt-1 text-xs text-gray-400">{{ quoteReplyText }}</view>
|
||||
<view class="mt-0.5 box-border text-sm text-gray-900 leading-5" @click="handleOnDetail"
|
||||
v-html="comment.spec.raw" />
|
||||
<view class="mt-2 flex items-center gap-x-4">
|
||||
<text class="text-gray-900 text-xs">{{ createTimeText }}</text>
|
||||
<view v-if="useActions" class="actions flex gap-2 font-bold">
|
||||
<view v-if="useActions" class="actions flex gap-2 font-medium">
|
||||
<view v-if="!disallowComment" class="text-xs" @click="handleOnReply">
|
||||
回复
|
||||
</view>
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
<script lang="ts" setup>
|
||||
import { onMounted, onUnmounted, ref } from 'vue'
|
||||
import { getPostCommentList } from '@/api/halo'
|
||||
import { onMounted, onUnmounted, reactive, ref } from 'vue'
|
||||
import { getPostCommentList, getPostCommentReplyList } from '@/api/halo'
|
||||
import { checkAvatarUrl } from '@/utils/url'
|
||||
import type { IComment, ICommentListRes } from '@/api/types/halo'
|
||||
import type { IComment, ICommentListRes, ICommentReply } from '@/api/types/halo'
|
||||
|
||||
const props = withDefaults(defineProps<{
|
||||
disallowComment ?: boolean
|
||||
@@ -16,7 +16,7 @@
|
||||
})
|
||||
|
||||
const emit = defineEmits<{
|
||||
(e : 'on-comment', data : { isComment : boolean, postName : string, title : string }) : void
|
||||
(e : 'on-comment', data : { isComment : boolean, postName : string, title : string, quoteReply ?: string }) : void
|
||||
(e : 'on-comment-detail', data : { postName : string, comment : IComment }) : void
|
||||
(e : 'on-loaded', list : IComment[]) : void
|
||||
}>()
|
||||
@@ -29,12 +29,26 @@
|
||||
name: props.postName,
|
||||
page: 1,
|
||||
size: 50,
|
||||
withReplies: true,
|
||||
replySize: 10,
|
||||
})
|
||||
const result = ref<ICommentListRes | null>(null)
|
||||
const dataList = ref<IComment[]>([])
|
||||
|
||||
/** 回复按需加载每页条数(对齐官方 comment-widget replySize 默认 20) */
|
||||
const REPLIES_PAGE_SIZE = 20
|
||||
|
||||
interface IRepliesState {
|
||||
list : ICommentReply[]
|
||||
total : number
|
||||
page : number
|
||||
hasNext : boolean
|
||||
status : 'loading' | 'success' | 'error'
|
||||
}
|
||||
|
||||
/** 已展开的评论 name 集合 */
|
||||
const expanded = reactive(new Set<string>())
|
||||
/** 各评论已加载回复(展开后缓存,收起再展开不重拉) */
|
||||
const repliesMap = reactive(new Map<string, IRepliesState>())
|
||||
|
||||
async function handleGetData() {
|
||||
loading.value = 'loading'
|
||||
try {
|
||||
@@ -45,6 +59,9 @@
|
||||
item.spec.owner.avatar = checkAvatarUrl(item.spec.owner.avatar ?? 'https://api.dicebear.com/10.x/adventurer-neutral/svg')
|
||||
return item
|
||||
})
|
||||
// 列表刷新后清空展开缓存,保证回复区展示最新数据
|
||||
expanded.clear()
|
||||
repliesMap.clear()
|
||||
loading.value = 'success'
|
||||
emit('on-loaded', dataList.value)
|
||||
}
|
||||
@@ -54,17 +71,97 @@
|
||||
}
|
||||
}
|
||||
|
||||
function handleToComment(data ?: { type : string, comment : IComment }) {
|
||||
/** 回复数(优先可见数,兜底总回复数/内嵌 total) */
|
||||
function getReplyCount(comment : IComment) : number {
|
||||
return comment.status?.visibleReplyCount ?? comment.status?.replyCount ?? comment.replies?.total ?? 0
|
||||
}
|
||||
|
||||
function getRepliesState(comment : IComment) : IRepliesState | undefined {
|
||||
return repliesMap.get(comment.metadata.name)
|
||||
}
|
||||
|
||||
/** 展开/收起某条评论的回复(首次展开时按需拉取) */
|
||||
function toggleReplies(comment : IComment) {
|
||||
const name = comment.metadata.name
|
||||
if (expanded.has(name)) {
|
||||
expanded.delete(name)
|
||||
return
|
||||
}
|
||||
expanded.add(name)
|
||||
if (!repliesMap.has(name)) {
|
||||
loadReplies(comment)
|
||||
}
|
||||
}
|
||||
|
||||
/** 首次展开:拉取第一页回复 */
|
||||
async function loadReplies(comment : IComment) {
|
||||
const name = comment.metadata.name
|
||||
if (!repliesMap.has(name)) {
|
||||
repliesMap.set(name, { list: [], total: 0, page: 0, hasNext: false, status: 'loading' })
|
||||
}
|
||||
const state = repliesMap.get(name)!
|
||||
state.status = 'loading'
|
||||
try {
|
||||
const res = await getPostCommentReplyList(name, { page: 1, size: REPLIES_PAGE_SIZE })
|
||||
const data = res.data
|
||||
state.list = data.items
|
||||
state.total = data.total
|
||||
state.page = data.page
|
||||
state.hasNext = data.hasNext
|
||||
state.status = 'success'
|
||||
}
|
||||
catch (err) {
|
||||
console.error('获取回复失败', err)
|
||||
state.status = 'error'
|
||||
}
|
||||
}
|
||||
|
||||
/** 加载更多回复(追加下一页) */
|
||||
async function loadMoreReplies(comment : IComment) {
|
||||
const state = repliesMap.get(comment.metadata.name)
|
||||
if (!state || state.status === 'loading')
|
||||
return
|
||||
state.status = 'loading'
|
||||
try {
|
||||
const res = await getPostCommentReplyList(comment.metadata.name, { page: state.page + 1, size: REPLIES_PAGE_SIZE })
|
||||
const data = res.data
|
||||
state.list = state.list.concat(data.items)
|
||||
state.page = data.page
|
||||
state.hasNext = data.hasNext
|
||||
state.status = 'success'
|
||||
}
|
||||
catch (err) {
|
||||
console.error('加载更多回复失败', err)
|
||||
state.status = 'error'
|
||||
}
|
||||
}
|
||||
|
||||
/** 已加载回复的 name -> displayName 映射(供「回复 @xxx」引用标识查询) */
|
||||
function buildQuoteReplyMap(comment : IComment) : Record<string, string> {
|
||||
const state = repliesMap.get(comment.metadata.name)
|
||||
if (!state)
|
||||
return {}
|
||||
const map : Record<string, string> = {}
|
||||
state.list.forEach((reply) => {
|
||||
map[reply.metadata.name] = reply.spec.owner.displayName
|
||||
})
|
||||
return map
|
||||
}
|
||||
|
||||
/** 回复/新增评论(data 有值=回复某条评论;parentComment=所属一级评论,回复接口需要其 name) */
|
||||
function handleToComment(data ?: { type : string, comment : ICommentReply }, parentComment ?: IComment) {
|
||||
if (props.disallowComment) {
|
||||
uni.showToast({ icon: 'none', title: '文章已禁止评论!' })
|
||||
return
|
||||
}
|
||||
if (data) {
|
||||
// 回复某条评论
|
||||
if (data && parentComment) {
|
||||
// 回复:postName 必须是一级评论(Comment)的 name;回复二级评论时带 quoteReply 引用其 name
|
||||
const isQuote = data.comment.metadata.name !== parentComment.metadata.name
|
||||
emit('on-comment', {
|
||||
isComment: false,
|
||||
postName: data.comment.metadata.name,
|
||||
postName: parentComment.metadata.name,
|
||||
title: data.comment.spec.owner.displayName,
|
||||
quoteReply: isQuote ? data.comment.metadata.name : undefined,
|
||||
})
|
||||
}
|
||||
else {
|
||||
@@ -161,24 +258,59 @@
|
||||
</view>
|
||||
|
||||
<block v-else>
|
||||
<!-- 一级评论 -->
|
||||
<!-- 一级评论 + 按需展开的回复区 -->
|
||||
<template v-for="comment in dataList" :key="comment.metadata.name">
|
||||
<uh-comment-item :use-content-bg="false" :is-child="false" :comment="comment"
|
||||
:post-name="postName" :disallow-comment="disallowComment" @on-copy="handleCopyContent"
|
||||
@on-comment="handleToComment" @on-detail="handleShowCommentDetail" />
|
||||
@on-comment="(d) => handleToComment(d, comment)" @on-detail="handleShowCommentDetail" />
|
||||
|
||||
<!-- 二级评论 -->
|
||||
<template v-if="comment.replies && comment.replies.items.length !== 0">
|
||||
<uh-comment-item v-for="childComment in comment.replies.items"
|
||||
:key="childComment.metadata.name" :use-content-bg="false" :is-child="true"
|
||||
:comment="childComment" :post-name="postName" :disallow-comment="disallowComment"
|
||||
@on-copy="handleCopyContent" @on-comment="handleToComment"
|
||||
@on-detail="handleShowCommentDetail" />
|
||||
</template>
|
||||
<!-- 回复展开区(默认收起,抖音式) -->
|
||||
<view v-if="getReplyCount(comment) > 0">
|
||||
<!-- 未展开:仅显示回复数 -->
|
||||
<view v-if="!expanded.has(comment.metadata.name)"
|
||||
class="mt-2 ml-10 inline-flex items-center rounded-lg bg-gray-100 px-3 py-1 text-xs text-gray-500"
|
||||
@click="toggleReplies(comment)">
|
||||
共 {{ getReplyCount(comment) }} 条回复
|
||||
</view>
|
||||
|
||||
<!-- 已展开:回复列表 -->
|
||||
<block v-else>
|
||||
<view v-if="getRepliesState(comment)?.status === 'error'"
|
||||
class="mt-2 ml-10 text-xs text-red-400" @click="loadReplies(comment)">
|
||||
回复加载失败,点击重试
|
||||
</view>
|
||||
<view v-else-if="getRepliesState(comment)?.status === 'loading' && getRepliesState(comment)?.list.length === 0"
|
||||
class="mt-2 ml-10 text-xs text-gray-400">
|
||||
回复加载中...
|
||||
</view>
|
||||
<template v-else>
|
||||
<uh-comment-item v-for="childComment in getRepliesState(comment)?.list"
|
||||
:key="childComment.metadata.name" :use-content-bg="false" :is-child="true"
|
||||
:comment="childComment" :post-name="postName"
|
||||
:disallow-comment="disallowComment"
|
||||
:quote-reply-map="buildQuoteReplyMap(comment)"
|
||||
@on-copy="handleCopyContent" @on-comment="(d) => handleToComment(d, comment)"
|
||||
@on-detail="handleShowCommentDetail" />
|
||||
|
||||
<!-- 加载更多(响应 hasNext) -->
|
||||
<view v-if="getRepliesState(comment)?.hasNext"
|
||||
class="mt-2 flex items-center justify-center">
|
||||
<text class="text-xs text-gray-400" @click="loadMoreReplies(comment)">
|
||||
{{ getRepliesState(comment)?.status === 'loading' ? '加载中...' : '加载更多回复' }}
|
||||
</text>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<!-- 收起 -->
|
||||
<view class="mt-2 ml-10 text-xs text-gray-400" @click="toggleReplies(comment)">
|
||||
收起回复
|
||||
</view>
|
||||
</block>
|
||||
</view>
|
||||
</template>
|
||||
</block>
|
||||
</block>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
</template>
|
||||
|
||||
@@ -12,10 +12,13 @@
|
||||
postName : string
|
||||
/** 评论目标 kind(文章 Post / 瞬间 Moment) */
|
||||
subjectKind ?: string
|
||||
/** 回复的回复:被引用回复(CommentReply)的 name,提交时转 quoteReply */
|
||||
quoteReply ?: string
|
||||
}>(), {
|
||||
isComment: false,
|
||||
title: '',
|
||||
subjectKind: 'Post',
|
||||
quoteReply: '',
|
||||
})
|
||||
|
||||
const emit = defineEmits<{
|
||||
@@ -211,6 +214,7 @@
|
||||
email: form.value.email,
|
||||
website: form.value.authorUrl,
|
||||
},
|
||||
quoteReply: props.quoteReply || undefined,
|
||||
captchaCode: config.value?.security?.captcha?.anonymousCommentCaptcha ? form.value.captchaCode : undefined,
|
||||
})
|
||||
uni.showToast({ icon: 'none', title: '回复成功,可能需要审核!' })
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<view class="fixed right-3 bottom-22 z-50 flex flex-col gap-y-3 pb-safe">
|
||||
<view class="fixed right-3 bottom-18 z-50 flex flex-col gap-y-3 pb-safe">
|
||||
<slot></slot>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
@@ -78,21 +78,23 @@
|
||||
<template>
|
||||
<uh-glass-popup v-model="isShow" position="center" :z-index="9999" custom-class="rounded-xl" @close="handleClose">
|
||||
<view v-if="notice" class="box-border w-[80vw] p-4">
|
||||
<!-- 头部:标题 + 关闭 -->
|
||||
<view class="flex items-center justify-between">
|
||||
<view class="flex items-center gap-2">
|
||||
<wd-icon class-prefix="uhemoji2-icon" name="-happy-" size="36rpx" />
|
||||
<text class="text-md font-semibold text-gray-900">
|
||||
最新公告
|
||||
</text>
|
||||
<view v-if="notice.typeDisplayName" class="rounded px-1.5 py-0.5 text-xs" :style="{
|
||||
color: notice.typeColor || '#f83856',
|
||||
backgroundColor: notice.typeColor ? `${notice.typeColor}1a` : '#fdeef1',
|
||||
<view v-if="notice.typeDisplayName" class="rounded px-1.5 py-0.5 text-xs text-primary bg-gray-100"
|
||||
:style="{
|
||||
color: notice.typeColor,
|
||||
backgroundColor: notice.typeColor ? `${notice.typeColor}1a` : '',
|
||||
}">
|
||||
{{ notice.typeDisplayName }}
|
||||
</view>
|
||||
</view>
|
||||
<view class="uh-global-card-glass !bg-white/5 border flex h-6 w-6 rounded-lg items-center justify-center text-gray-500" @click="handleClose">
|
||||
<view
|
||||
class="uh-global-card-glass !bg-white/5 border flex h-6 w-6 rounded-lg items-center justify-center text-gray-500"
|
||||
@click="handleClose">
|
||||
<wd-icon name="close" size="16px" />
|
||||
</view>
|
||||
</view>
|
||||
@@ -101,13 +103,13 @@
|
||||
<view class="mt-4">
|
||||
<image v-if="notice.cover" :src="checkImageUrl(notice.cover)" class="w-full h-34 rounded-lg mb-2">
|
||||
</image>
|
||||
<view class="text-md font-bold leading-snug text-gray-900">
|
||||
<view class="text-sm font-bold leading-snug text-gray-900">
|
||||
{{ notice.title }}
|
||||
</view>
|
||||
<view v-if="notice.summary" class="mt-3 text-sm leading-relaxed text-gray-500">
|
||||
<view v-if="notice.summary" class="mt-3 text-2xs leading-relaxed text-gray-600">
|
||||
{{ notice.summary }}
|
||||
</view>
|
||||
<view v-if="notice.publishTime" class="mt-3 text-xs text-gray-400">
|
||||
<view v-if="notice.publishTime" class="mt-3 text-xs text-gray-600">
|
||||
日期:{{ formatDate(notice.publishTime) }}
|
||||
</view>
|
||||
</view>
|
||||
|
||||
Reference in New Issue
Block a user