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

feat: 新增收藏功能与多项体验优化

- 新增本地收藏store与工具函数,支持文章/瞬间收藏持久化
- 重构插件可用性检查hook,统一管理依赖插件状态
- 替换旧数据加载hook为新的状态管理方案
- 优化首页布局与加载逻辑,调整公告组件样式
- 为瞬间详情页添加点赞、评论与收藏功能
- 新增文本处理工具,支持HTML/Markdown转纯文本与摘要截断
- 修复评论弹窗适配瞬间类型的参数问题
This commit is contained in:
小莫唐尼
2026-09-04 17:17:18 +08:00
parent 629b1f9219
commit 62b604644c
28 changed files with 1548 additions and 1387 deletions
@@ -5,8 +5,10 @@ import { getPostByName, getPostCommentReplyList, postTrackersCounter, submitUpvo
import { createVerificationCode, requestRestrictReadCheck } from '@/api/uni-halo'
import { formatTime } from '@/utils/formatTime'
import { useAppConfigStore } from '@/store/appConfig'
import { useFavoritesStore } from '@/store/favorites'
import { useSettingStore } from '@/store/setting'
import { checkAvatarUrl, checkImageUrl } from '@/utils/url'
import { buildPostFavoriteItem } from '@/utils/favorite'
import { checkPostRestrictRead, copyToClipboard, getRestrictReadTypeName, getShowableContent } from '@/utils/restrictRead'
import { getDomainOnly } from '@/utils/urlParams'
import { markdownConfig } from '@/config/markdown'
@@ -23,6 +25,7 @@ definePage({
})
const appConfigStore = useAppConfigStore()
const favoritesStore = useFavoritesStore()
const settingStore = useSettingStore()
const { loadingStatus, updateLoadingStatus } = useDataLoadingStatus()
@@ -217,6 +220,22 @@ async function handleDoLikes() {
}
}
/* ---------------- 收藏 ---------------- */
/** 当前文章是否已收藏 */
function hasFavorited(): boolean {
const name = result.value?.metadata.name
return !!name && favoritesStore.isFavorite('post', name)
}
/** 切换收藏(收藏/取消),收藏时按当前文章内容生成快照入库 */
function handleTogglePostFavorite() {
const post = result.value
if (!post)
return
const favorited = favoritesStore.toggle(buildPostFavoriteItem(post))
uni.showToast({ icon: 'none', title: favorited ? '收藏成功' : '已取消收藏' })
}
/* ---------------- 受限阅读 ---------------- */
function readMore() {
const annotations = result.value?.metadata?.annotations
@@ -613,10 +632,11 @@ const globalAppSettings = computed(() => settingStore.settings)
</view>
<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"
@click="handleToComment()"
@click="handleTogglePostFavorite"
>
<wd-icon class-prefix="uhemoji-icon" name="-smile-" size="36rpx" />
<text class="shrink-0 text-sm text-gray-900 font-semibold">收藏</text>
<text class="shrink-0 text-sm text-gray-900 font-semibold"
:style="hasFavorited() ? { color: '#ffb300' } : ''">{{ hasFavorited() ? '已收藏' : '收藏' }}</text>
</view>
</view>
</view>
+229 -253
View File
@@ -1,280 +1,256 @@
<script lang="ts" setup>
/**
* 数据看板页(源自旧项目 pagesA/data-visual,新建复刻)
* 标签统计/分类统计(环形图)、文章发布趋势(热度图)、评论活跃用户(柱状图)、热门文章 Top10(柱状图)
*/
import { ref } from 'vue'
import { onPullDownRefresh } from '@dcloudio/uni-app'
import { getChartData } from '@/api/uni-halo'
import { usePluginAvailable } from '@/utils/plugin'
import type { IDataStatistics } from '@/api/uni-halo'
import { ref } from 'vue'
import { onPullDownRefresh } from '@dcloudio/uni-app'
import { getChartData } from '@/api/uni-halo'
import type { IDataStatistics } from '@/api/uni-halo'
definePage({
style: {
navigationBarTitleText: '数据看板',
enablePullDownRefresh: true,
},
})
definePage({
style: {
navigationBarTitleText: '数据看板',
enablePullDownRefresh: true,
},
})
/** 依赖插件(plugin-data-statistics) */
const uniHaloPluginId = 'plugin-data-statistics'
const uniHaloPluginAvailable = ref(true)
/** 依赖插件(plugin-data-statistics) */
const uniHaloPluginId = 'plugin-data-statistics'
const { available: uniHaloPluginAvailable, check: checkPluginAvailable } = usePluginAvailable(uniHaloPluginId)
/* ---------------- 状态 ---------------- */
const loading = ref<'loading' | 'success' | 'error'>('loading')
const statistics = ref<IDataStatistics>({
tags: [],
categories: [],
articles: [],
comments: [],
top10Articles: [],
})
/* ---------------- 状态 ---------------- */
const loading = ref<'loading' | 'success' | 'error'>('loading')
const statistics = ref<IDataStatistics>({
tags: [],
categories: [],
articles: [],
comments: [],
top10Articles: [],
})
/* ---------------- 图表配置 ---------------- */
const chartColors = ['#3B82F6', '#10B981', '#F59E0B', '#EF4444', '#8B5CF6', '#EC4899', '#14B8A6', '#F97316', '#ea7ccc', '#0EA5E9']
/* ---------------- 图表配置 ---------------- */
const chartColors = ['#3B82F6', '#10B981', '#F59E0B', '#EF4444', '#8B5CF6', '#EC4899', '#14B8A6', '#F97316', '#ea7ccc', '#0EA5E9']
/** 标签统计(环形图) */
const tagChart = ref({
isExpand: true,
type: 'ring',
data: { series: [{ data: [] as { name: string, value: number }[] }] },
})
/** 标签统计(环形图) */
const tagChart = ref({
isExpand: true,
type: 'ring',
data: { series: [{ data: [] as { name : string, value : number }[] }] },
})
/** 分类统计(柱状图) */
const categoryChart = ref({
isExpand: true,
type: 'column',
data: { categories: [] as string[], series: [{ name: '分类', data: [] as number[] }] },
})
/** 分类统计(柱状图) */
const categoryChart = ref({
isExpand: true,
type: 'column',
data: { categories: [] as string[], series: [{ name: '分类', data: [] as number[] }] },
})
/** 文章发布趋势(热度图) */
const trandArticleChart = ref({
isExpand: true,
type: 'hotmap',
data: [] as { date: string, count: number }[],
})
/** 文章发布趋势(热度图) */
const trandArticleChart = ref({
isExpand: true,
type: 'hotmap',
data: [] as { date : string, count : number }[],
})
/** 评论活跃用户(柱状图) */
const userCommentsChart = ref({
isExpand: true,
type: 'column',
data: { categories: [] as string[], series: [{ name: '评论', data: [] as number[] }] },
})
/** 评论活跃用户(柱状图) */
const userCommentsChart = ref({
isExpand: true,
type: 'column',
data: { categories: [] as string[], series: [{ name: '评论', data: [] as number[] }] },
})
/** 热门文章 Top10(柱状图) */
const top10ArticlesChart = ref({
isExpand: true,
type: 'column',
data: { categories: [] as string[], series: [{ name: '访问量', data: [] as number[] }] },
})
/** 热门文章 Top10(柱状图) */
const top10ArticlesChart = ref({
isExpand: true,
type: 'column',
data: { categories: [] as string[], series: [{ name: '访问量', data: [] as number[] }] },
})
/* ---------------- 数据处理 ---------------- */
function handleTagChart() {
const data = [...statistics.value.tags].sort((a, b) => b.count - a.count)
tagChart.value.data = {
series: [
{
data: data.map(item => ({ name: item.name, value: item.count })),
},
],
}
}
/* ---------------- 数据处理 ---------------- */
function handleTagChart() {
const data = [...statistics.value.tags].sort((a, b) => b.count - a.count)
tagChart.value.data = {
series: [
{
data: data.map(item => ({ name: item.name, value: item.count })),
},
],
}
}
function handleCategoriesChart() {
const data = [...statistics.value.categories].sort((a, b) => b.total - a.total)
categoryChart.value.data = {
categories: data.map(item => item.name),
series: [{ name: '分类', data: data.map(item => item.total) }],
}
}
function handleCategoriesChart() {
const data = [...statistics.value.categories].sort((a, b) => b.total - a.total)
categoryChart.value.data = {
categories: data.map(item => item.name),
series: [{ name: '分类', data: data.map(item => item.total) }],
}
}
function handleTrendArticlesChart() {
trandArticleChart.value.data = statistics.value.articles.map(item => ({
date: item.date,
count: item.count,
}))
}
function handleTrendArticlesChart() {
trandArticleChart.value.data = statistics.value.articles.map(item => ({
date: item.date,
count: item.count,
}))
}
function handleUserCommentsChart() {
const data = [...statistics.value.comments].sort((a, b) => b.count - a.count).slice(0, 10)
userCommentsChart.value.data = {
categories: data.map(item => item.username),
series: [{ name: '评论', data: data.map(item => item.count) }],
}
}
function handleUserCommentsChart() {
const data = [...statistics.value.comments].sort((a, b) => b.count - a.count).slice(0, 10)
userCommentsChart.value.data = {
categories: data.map(item => item.username),
series: [{ name: '评论', data: data.map(item => item.count) }],
}
}
function handleTop10ArticlesChart() {
const data = [...statistics.value.top10Articles].sort((a, b) => b.views - a.views).slice(0, 10)
top10ArticlesChart.value.data = {
categories: data.map(item => item.name),
series: [{ name: '访问量', data: data.map(item => item.views) }],
}
}
function handleTop10ArticlesChart() {
const data = [...statistics.value.top10Articles].sort((a, b) => b.views - a.views).slice(0, 10)
top10ArticlesChart.value.data = {
categories: data.map(item => item.name),
series: [{ name: '访问量', data: data.map(item => item.views) }],
}
}
/* ---------------- 数据加载 ---------------- */
async function handleGetData() {
uni.showLoading({ mask: true, title: '加载中...' })
loading.value = 'loading'
try {
const res = await getChartData()
statistics.value = res.data
handleTagChart()
handleCategoriesChart()
handleTrendArticlesChart()
handleUserCommentsChart()
handleTop10ArticlesChart()
loading.value = 'success'
}
catch (err) {
console.error(err)
loading.value = 'error'
}
finally {
setTimeout(() => {
uni.hideLoading()
uni.stopPullDownRefresh()
}, 100)
}
}
/* ---------------- 数据加载 ---------------- */
async function handleGetData() {
uni.showLoading({ mask: true, title: '加载中...' })
loading.value = 'loading'
try {
const res = await getChartData()
statistics.value = res.data
handleTagChart()
handleCategoriesChart()
handleTrendArticlesChart()
handleUserCommentsChart()
handleTop10ArticlesChart()
loading.value = 'success'
}
catch (err) {
console.error(err)
loading.value = 'error'
}
finally {
setTimeout(() => {
uni.hideLoading()
uni.stopPullDownRefresh()
}, 100)
}
}
/* ---------------- 生命周期 ---------------- */
async function init() {
uniHaloPluginAvailable.value = await usePluginAvailable(uniHaloPluginId)
if (!uniHaloPluginAvailable.value) {
uni.stopPullDownRefresh()
return
}
handleGetData()
}
/* ---------------- 生命周期 ---------------- */
async function init() {
await checkPluginAvailable()
if (!uniHaloPluginAvailable.value) {
uni.stopPullDownRefresh()
return
}
handleGetData()
}
onPullDownRefresh(() => {
if (!uniHaloPluginAvailable.value) {
uni.stopPullDownRefresh()
return
}
handleGetData()
})
onPullDownRefresh(() => {
if (!uniHaloPluginAvailable.value) {
uni.stopPullDownRefresh()
return
}
handleGetData()
})
init()
init()
</script>
<template>
<view class="app-page box-border min-h-screen w-screen p-6 text-[#353437]" style="background-color: #fafafd;">
<uh-plugin-unavailable
v-if="!uniHaloPluginAvailable"
:plugin-id="uniHaloPluginId"
error-text="阿偶,检测到当前插件没有安装或者启用,无法使用功能哦,请联系管理员"
@on-refresh="handleGetData"
/>
<template v-else>
<!-- 加载/错误占位 -->
<view v-if="loading !== 'success'">
<uh-data-loading :loading-status="loading" @refresh="handleGetData" />
</view>
<view class="bg-page box-border min-h-screen w-screen p-3">
<uh-plugin-unavailable v-if="!uniHaloPluginAvailable" :plugin-id="uniHaloPluginId"
error-text="阿偶,检测到当前插件没有安装或者启用,无法使用功能哦,请联系管理员" @on-refresh="handleGetData" />
<template v-else>
<uh-data-loading v-if="loading !== 'success'" :loading-status="loading" @refresh="handleGetData" />
<!-- 内容区域 -->
<view v-else class="content flex flex-col gap-6">
<!-- 标签统计 -->
<view class="card box-border rounded-xl bg-white/95 p-6" style="box-shadow: 0 0 12rpx rgb(226 232 240 / 35%);">
<view class="card-head flex items-center justify-between" @click="tagChart.isExpand = !tagChart.isExpand">
<view class="card-head-title flex items-baseline gap-2">
<text class="card-head-text relative box-border pl-6 text-[30rpx] font-bold">标签统计</text>
<text class="card-head-subtext text-[26rpx] text-[#6b7280] font-normal">(全部标签的文章数量占比)</text>
</view>
<wd-icon :name="tagChart.isExpand ? 'arrow-up' : 'arrow-down'" size="16px" color="#909399" />
</view>
<view v-show="tagChart.isExpand" class="card-body mt-6 box-border w-full overflow-hidden border-2 border-[#e9eef3] rounded-xl bg-[#fcfdfe] p-3">
<qiun-data-charts
type="ring"
:chart-data="tagChart.data"
:opts="{ color: chartColors, padding: [5, 5, 5, 5], dataLabel: false, legend: { show: false }, extra: { ring: { ringWidth: 36, offsetAngle: -90, border: true, borderWidth: 1, borderColor: '#FFFFFF' } } }"
/>
</view>
</view>
<!-- 内容区域 -->
<view v-else class="content flex flex-col gap-3">
<!-- 标签统计 -->
<view class="uh-global-card-glass uh-shadow-xs box-border rounded-xl p-4">
<uh-section-title>
标签统计
<template #right>
<view class="flex items-center gap-x-2">
<text class="text-xs text-gray-500">(全部标签的文章数量占比)</text>
<wd-icon :name="tagChart.isExpand ? 'up' : 'down'" size="16px" color="#909399"
@click="tagChart.isExpand = !tagChart.isExpand" />
</view>
</template>
</uh-section-title>
<view v-show="tagChart.isExpand" class="box-border w-full mt-3">
<qiun-data-charts type="ring" :chart-data="tagChart.data"
:opts="{ color: chartColors, padding: [5, 5, 5, 5], dataLabel: false, legend: { show: false }, extra: { ring: { ringWidth: 36, offsetAngle: -90, border: true, borderWidth: 1, borderColor: '#FFFFFF' } } }" />
</view>
</view>
<!-- 分类统计 -->
<view class="card box-border rounded-xl bg-white/95 p-6" style="box-shadow: 0 0 12rpx rgb(226 232 240 / 35%);">
<view class="card-head flex items-center justify-between" @click="categoryChart.isExpand = !categoryChart.isExpand">
<view class="card-head-title flex items-baseline gap-2">
<text class="card-head-text relative box-border pl-6 text-[30rpx] font-bold">分类统计</text>
<text class="card-head-subtext text-[26rpx] text-[#6b7280] font-normal">(全部分类的文章数量占比)</text>
</view>
<wd-icon :name="categoryChart.isExpand ? 'arrow-up' : 'arrow-down'" size="16px" color="#909399" />
</view>
<view v-show="categoryChart.isExpand" class="card-body mt-6 box-border w-full overflow-hidden border-2 border-[#e9eef3] rounded-xl bg-[#fcfdfe] p-3">
<qiun-data-charts
type="column"
:chart-data="categoryChart.data"
:opts="{ color: chartColors, padding: [20, 15, 10, 15], legend: { show: false }, xAxis: { disableGrid: true, fontSize: 10, itemCount: 6 }, yAxis: { gridType: 'dash', dashLength: 4 }, extra: { column: { type: 'group', width: 22, linearType: 'custom', seriesGap: 5, barBorderCircle: true, customColor: ['#F59E0B'] } } }"
/>
</view>
</view>
<!-- 分类统计 -->
<view class="uh-global-card-glass uh-shadow-xs box-border rounded-xl p-4">
<uh-section-title>
分类统计
<template #right>
<view class="flex items-center gap-x-2">
<text class="text-xs text-gray-500">(全部分类的文章数量占比)</text>
<wd-icon :name="categoryChart.isExpand ? 'up' : 'down'" size="16px" color="#909399"
@click="categoryChart.isExpand = !categoryChart.isExpand" />
</view>
</template>
</uh-section-title>
<view v-show="categoryChart.isExpand" class="box-border w-full mt-3">
<qiun-data-charts type="column" :chart-data="categoryChart.data"
:opts="{ color: chartColors, padding: [20, 15, 10, 15], legend: { show: false }, xAxis: { disableGrid: true, fontSize: 10, itemCount: 6 }, yAxis: { gridType: 'dash', dashLength: 4 }, extra: { column: { type: 'group', width: 22, linearType: 'custom', seriesGap: 5, barBorderCircle: true, customColor: ['#F59E0B'] } } }" />
</view>
</view>
<!-- 文章发布趋势 -->
<view class="card box-border rounded-xl bg-white/95 p-6" style="box-shadow: 0 0 12rpx rgb(226 232 240 / 35%);">
<view class="card-head flex items-center justify-between" @click="trandArticleChart.isExpand = !trandArticleChart.isExpand">
<view class="card-head-title flex items-baseline gap-2">
<text class="card-head-text relative box-border pl-6 text-[30rpx] font-bold">文章发布趋势</text>
<text class="card-head-subtext text-[26rpx] text-[#6b7280] font-normal">(按日期统计文章发布数量)</text>
</view>
<wd-icon :name="trandArticleChart.isExpand ? 'arrow-up' : 'arrow-down'" size="16px" color="#909399" />
</view>
<view v-show="trandArticleChart.isExpand" class="card-body mt-6 box-border w-full overflow-hidden border-2 border-[#e9eef3] rounded-xl bg-[#fcfdfe] p-3">
<uh-heatmap :chart-data="trandArticleChart.data" />
</view>
</view>
<!-- 文章发布趋势 -->
<view class="uh-global-card-glass uh-shadow-xs box-border rounded-xl p-4">
<uh-section-title>
文章发布趋势
<template #right>
<view class="flex items-center gap-x-2">
<text class="text-xs text-gray-500">(按日期统计文章发布数量)</text>
<wd-icon :name="trandArticleChart.isExpand ? 'up' : 'down'" size="16px" color="#909399"
@click="trandArticleChart.isExpand = !trandArticleChart.isExpand" />
</view>
</template>
</uh-section-title>
<view v-show="trandArticleChart.isExpand" class="box-border w-full mt-3">
<uh-heatmap :chart-data="trandArticleChart.data" />
</view>
</view>
<!-- 评论活跃用户 -->
<view class="card box-border rounded-xl bg-white/95 p-6" style="box-shadow: 0 0 12rpx rgb(226 232 240 / 35%);">
<view class="card-head flex items-center justify-between" @click="userCommentsChart.isExpand = !userCommentsChart.isExpand">
<view class="card-head-title flex items-baseline gap-2">
<text class="card-head-text relative box-border pl-6 text-[30rpx] font-bold">评论活跃用户</text>
<text class="card-head-subtext text-[26rpx] text-[#6b7280] font-normal">(按评论作者统计评论数量)</text>
</view>
<wd-icon :name="userCommentsChart.isExpand ? 'arrow-up' : 'arrow-down'" size="16px" color="#909399" />
</view>
<view v-show="userCommentsChart.isExpand" class="card-body mt-6 box-border w-full overflow-hidden border-2 border-[#e9eef3] rounded-xl bg-[#fcfdfe] p-3">
<qiun-data-charts
type="column"
:chart-data="userCommentsChart.data"
:opts="{ color: chartColors, padding: [20, 15, 10, 10], legend: { show: false }, xAxis: { disableGrid: true, fontSize: 10, itemCount: 5 }, yAxis: { gridType: 'dash', dashLength: 4 }, extra: { column: { type: 'group', width: 22, linearType: 'custom', seriesGap: 5, barBorderCircle: true, customColor: ['#F59E0B'] } } }"
/>
</view>
</view>
<!-- 评论活跃用户 -->
<view class="uh-global-card-glass uh-shadow-xs box-border rounded-xl p-4">
<uh-section-title>
评论活跃用户
<template #right>
<view class="flex items-center gap-x-2">
<text class="text-xs text-gray-500">(按评论作者统计评论数量)</text>
<wd-icon :name="userCommentsChart.isExpand ? 'up' : 'down'" size="16px" color="#909399"
@click="userCommentsChart.isExpand = !userCommentsChart.isExpand" />
</view>
</template>
</uh-section-title>
<view v-show="userCommentsChart.isExpand" class="box-border w-full mt-3">
<qiun-data-charts type="column" :chart-data="userCommentsChart.data"
:opts="{ color: chartColors, padding: [20, 15, 10, 10], legend: { show: false }, xAxis: { disableGrid: true, fontSize: 10, itemCount: 5 }, yAxis: { gridType: 'dash', dashLength: 4 }, extra: { column: { type: 'group', width: 22, linearType: 'custom', seriesGap: 5, barBorderCircle: true, customColor: ['#F59E0B'] } } }" />
</view>
</view>
<!-- 热门文章 Top10 -->
<view class="card box-border rounded-xl bg-white/95 p-6" style="box-shadow: 0 0 12rpx rgb(226 232 240 / 35%);">
<view class="card-head">
<view class="card-head-title flex items-baseline gap-2">
<text class="card-head-text relative box-border pl-6 text-[30rpx] font-bold">热门文章前10</text>
<text class="card-head-subtext text-[26rpx] text-[#6b7280] font-normal">(按访问量排序的热门文章)</text>
</view>
</view>
<view class="card-body mt-6 box-border w-full overflow-hidden border-2 border-[#e9eef3] rounded-xl bg-[#fcfdfe] p-3">
<qiun-data-charts
type="column"
:chart-data="top10ArticlesChart.data"
:opts="{ color: chartColors, padding: [20, 15, 10, 10], legend: { show: false }, xAxis: { disableGrid: true, fontSize: 10, itemCount: 5 }, yAxis: { gridType: 'dash', dashLength: 4 }, extra: { column: { type: 'group', width: 22, linearType: 'custom', seriesGap: 5, barBorderCircle: true, customColor: ['#F59E0B'] } } }"
/>
</view>
</view>
</view>
</template>
</view>
</template>
<style scoped lang="scss">
.card-head-text {
&::before {
content: '';
position: absolute;
left: 0;
top: 50%;
transform: translateY(-50%);
width: 8rpx;
height: 70%;
background-color: #03a9f4;
border-radius: 12rpx;
}
}
</style>
<!-- 热门文章 Top10 -->
<view class="uh-global-card-glass uh-shadow-xs box-border rounded-xl p-4">
<uh-section-title>
热门文章前10
<template #right>
<view class="flex items-center gap-x-2">
<text class="text-xs text-gray-500">(按访问量排序的热门文章)</text>
<wd-icon :name="top10ArticlesChart.isExpand ? 'up' : 'down'" size="16px" color="#909399"
@click="top10ArticlesChart.isExpand = !top10ArticlesChart.isExpand" />
</view>
</template>
</uh-section-title>
<view v-show="top10ArticlesChart.isExpand" class="box-border w-full mt-3">
<qiun-data-charts type="column" :chart-data="top10ArticlesChart.data"
:opts="{ color: chartColors, padding: [20, 15, 10, 10], legend: { show: false }, xAxis: { disableGrid: true, fontSize: 10, itemCount: 5 }, yAxis: { gridType: 'dash', dashLength: 4 }, extra: { column: { type: 'group', width: 22, linearType: 'custom', seriesGap: 5, barBorderCircle: true, customColor: ['#F59E0B'] } } }" />
</view>
</view>
</view>
</template>
</view>
</template>
+196
View File
@@ -0,0 +1,196 @@
<script lang="ts" setup>
/**
* 收藏页(纯本地,文章 × 瞬间)
* 功能:双 Tab(仿图库顶部吸顶胶囊 chip)本地收藏列表 + 跳详情 + 删除 + 状态舞台
* 数据:useFavoritesStore(persist),快照自包含,无需网络加载
* 状态:useDataLoadingStatus + uh-data-loading(与 tabbar 页同构);本地同步数据,状态直接推导
*/
import { computed, ref, watchEffect } from 'vue'
import { formatTime } from '@/utils/formatTime'
import { useFavoritesStore } from '@/store/favorites'
import { DataLoadingStatusEnum, useDataLoadingStatus } from '@/hooks/useDataLoadingStatus'
import type { FavoriteKind, IFavoriteItem } from '@/utils/favorite'
definePage({
style: {
navigationBarTitleText: '我的收藏',
backgroundColor: '#f6f3ee',
},
})
const favoritesStore = useFavoritesStore()
/* ---------------- Tab(文章/瞬间) ---------------- */
const activeKind = ref<FavoriteKind>('post')
const tabList = computed(() => [
{ key: 'post' as FavoriteKind, label: '文章', count: favoritesStore.counts.post },
{ key: 'moment' as FavoriteKind, label: '瞬间', count: favoritesStore.counts.moment },
])
const currentItems = computed<IFavoriteItem[]>(() =>
activeKind.value === 'post' ? favoritesStore.postItems : favoritesStore.momentItems,
)
function handleSwitchTab(kind: FavoriteKind) {
activeKind.value = kind
}
/* ---------------- 加载状态机(与 tabbar 页同构) ---------------- */
const { loadingStatus, updateLoadingStatus } = useDataLoadingStatus()
/** 本地数据同步可得:状态由当前 Tab 列表直接推导(空→Empty;loading/error 分支为将来异步数据源预留) */
watchEffect(() => {
updateLoadingStatus(
currentItems.value.length === 0 ? DataLoadingStatusEnum.Empty : DataLoadingStatusEnum.Success,
)
})
/** uh-data-loading「刷新试试」:空收藏无可刷新来源,引导回首页发现可收藏内容 */
function handleExplore() {
uni.switchTab({ url: '/pages/tabbar/home/home' })
}
/* ---------------- 收藏时间 ---------------- */
function formatCollectTime(time: string): string {
return formatTime({ d: time, f: 'yyyy-MM-dd' })
}
/* ---------------- 交互 ---------------- */
/** 跳转对应类型详情(原内容被删由详情页空态兜底) */
function handleToDetail(item: IFavoriteItem) {
const base = item.kind === 'post'
? '/pages-blog/article-detail/article-detail'
: '/pages-blog/moment-detail/moment-detail'
uni.navigateTo({
url: `${base}?name=${item.id}`,
animationType: 'slide-in-right',
})
}
/** 取消收藏(直接删除 + 轻提示;详情页可随时重新收藏,不弹二次确认) */
function handleRemove(item: IFavoriteItem) {
favoritesStore.remove(item.kind, item.id)
uni.showToast({ icon: 'none', title: '已取消收藏' })
}
/* ---------------- 空态文案 ---------------- */
const emptyText = computed(() => (activeKind.value === 'post' ? '还没有收藏文章' : '还没有收藏瞬间'))
</script>
<template>
<view class="box-border min-h-screen w-screen bg-page pb-10">
<!-- 顶部类型 Tab(与图库页同款:吸顶玻璃胶囊 chip) -->
<wd-sticky>
<scroll-view scroll-x class="w-full whitespace-nowrap">
<view class="flex gap-2 px-3 pb-1 pt-3">
<view
v-for="tab in tabList" :key="tab.key"
class="uh-global-card-glass uh-shadow-xs inline-block border rounded-2xl px-5 py-1.5 text-sm"
:class="tab.key === activeKind ? 'bg-primary font-bold' : 'text-gray-500'"
@click="handleSwitchTab(tab.key)"
>
{{ tab.label }}
<text v-if="tab.count > 0">({{ tab.count }})</text>
</view>
</view>
</scroll-view>
</wd-sticky>
<!-- 状态舞台 + 列表 -->
<view class="flex flex-col gap-3 px-3 pt-2">
<!-- 空态(当前 Tab 无收藏):uh-data-loading 统一渲染,视觉与 tabbar 页一致 -->
<uh-data-loading
v-if="loadingStatus !== 'success'" :loading-status="loadingStatus" min-height="55vh"
:empty-text="emptyText" empty-sub-text="在文章或瞬间的详情页点亮星标,内容会出现在这里"
@refresh="handleExplore"
/>
<!-- 成功态:当前 Tab 列表 -->
<template v-else>
<!-- 文章卡 -->
<template v-if="activeKind === 'post'">
<view
v-for="item in currentItems" :key="item.id"
class="uh-shadow-xs overflow-hidden rounded-[24rpx] bg-white" @click="handleToDetail(item)"
>
<view class="flex gap-3 p-4 pb-3">
<image
v-if="item.cover" :src="item.cover" mode="aspectFill"
class="h-[128rpx] w-[176rpx] shrink-0 rounded-lg"
/>
<view class="min-w-0 flex-1">
<view class="truncate text-sm text-gray-900 font-bold">
{{ item.title || '未命名' }}
</view>
<view v-if="item.content" class="clamp-2 mt-1 text-xs text-gray-500 leading-relaxed">
{{ item.content }}
</view>
</view>
</view>
<!-- 元信息 + 底部操作(详情/删除) -->
<view class="flex items-center justify-between px-4 pb-3">
<view class="min-w-0 flex flex-1 items-center gap-1.5 text-xs text-gray-400">
<image
v-if="item.owner.avatar" :src="item.owner.avatar" mode="aspectFill"
class="h-[36rpx] w-[36rpx] shrink-0 rounded-full"
/>
<text class="max-w-[220rpx] truncate">{{ item.owner.displayName }}</text>
<text class="shrink-0">· 收藏于 {{ formatCollectTime(item.createTime) }}</text>
</view>
<view class="flex shrink-0 items-center gap-1">
<text class="px-2 py-1 text-xs text-primary" @click.stop="handleToDetail(item)">详情</text>
<text class="px-2 py-1 text-xs text-gray-400" @click.stop="handleRemove(item)">删除</text>
</view>
</view>
</view>
</template>
<!-- 瞬间卡 -->
<template v-else>
<view
v-for="item in currentItems" :key="item.id"
class="uh-shadow-xs overflow-hidden rounded-[24rpx] bg-white" @click="handleToDetail(item)"
>
<view class="p-4 pb-3">
<view class="clamp-3 text-sm text-gray-800 leading-relaxed">
{{ item.content || '(暂无内容)' }}
</view>
</view>
<view class="flex items-center justify-between px-4 pb-3">
<view class="min-w-0 flex flex-1 items-center gap-1.5 text-xs text-gray-400">
<image
v-if="item.owner.avatar" :src="item.owner.avatar" mode="aspectFill"
class="h-[36rpx] w-[36rpx] shrink-0 rounded-full"
/>
<text class="max-w-[220rpx] truncate">{{ item.owner.displayName }}</text>
<text class="shrink-0">· 收藏于 {{ formatCollectTime(item.createTime) }}</text>
</view>
<view class="flex shrink-0 items-center gap-1">
<text class="px-2 py-1 text-xs text-primary" @click.stop="handleToDetail(item)">详情</text>
<text class="px-2 py-1 text-xs text-gray-400" @click.stop="handleRemove(item)">删除</text>
</view>
</view>
</view>
</template>
</template>
</view>
</view>
</template>
<style scoped lang="scss">
/* 多行截断(原子类无 line-clamp,scoped 补充) */
.clamp-2 {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
overflow: hidden;
}
.clamp-3 {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}
</style>
+6 -6
View File
@@ -12,7 +12,7 @@ import { getMiniProgramLinkGroupedList } from '@/api/uni-halo'
import { useAppConfigStore } from '@/store/appConfig'
import { useSettingStore } from '@/store/setting'
import { checkAvatarUrl, checkImageUrl } from '@/utils/url'
import { NeedPluginIds, usePluginAvailable } from '@/utils/plugin'
import { NeedPluginIds } from '@/hooks/usePluginAvailable'
import type { ILink, ILinkGroup } from '@/api/types/halo'
import type { IMiniProgramLink, IMiniProgramLinkGroupVo } from '@/api/types/uni-halo'
@@ -32,10 +32,10 @@ const globalAppSettings = computed(() => settingStore.settings)
/* ---------------- 依赖插件 ---------------- */
/** 站点 tab:plugin-links */
const sitePluginId = NeedPluginIds.PluginLinks
const sitePluginAvailable = ref(true)
const { available: sitePluginAvailable, check: checkSitePluginAvailable } = usePluginAvailable(sitePluginId)
/** 小程序 tab:plugin-uni-halo */
const miniPluginId = NeedPluginIds.PluginUniHalo
const miniPluginAvailable = ref(true)
const { available: miniPluginAvailable, check: checkMiniPluginAvailable } = usePluginAvailable(miniPluginId)
/* ---------------- tabs ---------------- */
const activeTabIndex = ref(0)
@@ -268,9 +268,9 @@ function handleSaveMiniProgramCode(link: IMiniProgramLink) {
/* ---------------- 生命周期 ---------------- */
onLoad(async () => {
;[sitePluginAvailable.value, miniPluginAvailable.value] = await Promise.all([
usePluginAvailable(sitePluginId),
usePluginAvailable(miniPluginId),
await Promise.all([
checkSitePluginAvailable(),
checkMiniPluginAvailable(),
])
if (sitePluginAvailable.value)
handleGetLinkGroupData()
+121 -1
View File
@@ -7,9 +7,11 @@
*/
import { computed, ref } from 'vue'
import { onLoad, onPullDownRefresh, onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'
import { getMomentByName } from '@/api/halo'
import { getMomentByName, submitUpvote } from '@/api/halo'
import { useAppConfigStore } from '@/store/appConfig'
import { useFavoritesStore } from '@/store/favorites'
import { checkAvatarUrl, checkThumbnailUrl } from '@/utils/url'
import { buildMomentFavoriteItem } from '@/utils/favorite'
import { generateUUID } from '@/utils/uuid'
import { formatTime as formatTimeUtil } from '@/utils/formatTime'
import { randomTagColor } from '@/utils/random'
@@ -27,6 +29,7 @@ definePage({
})
const appConfigStore = useAppConfigStore()
const favoritesStore = useFavoritesStore()
const haloConfigs = computed(() => appConfigStore.configs)
const bloggerInfo = computed(() => {
@@ -122,6 +125,83 @@ const calcMastheadMeta = computed(() => {
return time ? formatTimeUtil({ d: time, f: 'yyyy年 · 星期w' }) : ''
})
/* ---------------- 收藏 ---------------- */
/** 当前瞬间是否已收藏(悬浮胶囊高亮) */
const momentFavorited = computed(() => {
const name = moment.value?.metadata.name
return !!name && favoritesStore.isFavorite('moment', name)
})
/** 切换收藏(收藏/取消),收藏时按当前详情内容生成快照入库 */
function handleToggleMomentFavorite() {
const card = moment.value
if (!card)
return
const favorited = favoritesStore.toggle(buildMomentFavoriteItem(card))
uni.showToast({ icon: 'none', title: favorited ? '收藏成功' : '已取消收藏' })
}
/* ---------------- 点赞 ---------------- */
const upvotedNames = ref<string[]>([])
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) {
current.stats.upvote = (current.stats.upvote || 0) + 1
}
}
catch (err) {
console.error('点赞失败', err)
uni.showToast({ icon: 'none', title: '点赞失败' })
}
}
/* ---------------- 评论 ---------------- */
const commentModal = ref({
show: false,
isComment: false,
postName: '',
title: '',
})
function handleToComment() {
const current = moment.value
if (!current)
return
if (!current.spec.allowComment) {
uni.showToast({ icon: 'none', title: '瞬间已开启禁止评论!' })
return
}
commentModal.value = {
show: true,
isComment: true,
postName: current.metadata.name,
title: '新增评论',
}
}
function handleOnCommentModalClose(data: { refresh: boolean, isSubmit: boolean }) {
commentModal.value.show = false
}
/* ---------------- 视频互斥 ---------------- */
function createVideoContexts(videos: { id?: string }[]) {
stopAllVideos()
@@ -329,9 +409,49 @@ onShareTimeline(() => ({
<view v-else class="h-7" />
</view>
<!-- 悬浮操作(与文章详情一致:点赞/评论/收藏) -->
<view 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"
>
<!-- 点赞 -->
<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"
>
<wd-icon class-prefix="uhemoji-icon" name="-kiss-" size="36rpx" />
<text class="shrink-0 text-sm text-gray-900 font-semibold">点赞</text>
</view>
<!-- 评论 -->
<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"
@click="handleToComment()"
>
<wd-icon class-prefix="uhemoji-icon" name="-thinking" size="36rpx" />
<text class="shrink-0 text-sm text-gray-900 font-semibold">评论</text>
</view>
<!-- 收藏 -->
<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"
@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>
</view>
</view>
</view>
<!-- 回顶 -->
<view class="to-top-btn uh-shadow-xs fixed bottom-[100rpx] right-6 z-6 h-[72rpx] w-[72rpx] flex items-center justify-center rounded-full bg-white" @click="handleToTopPage()">
<wd-icon name="arrow-up" size="20px" color="#6b7280" />
</view>
<!-- 评论弹窗 -->
<uh-comment-modal
v-if="commentModal.show" :show="commentModal.show" :is-comment="commentModal.isComment"
:title="commentModal.title" :post-name="commentModal.postName" subject-kind="Moment"
@on-close="handleOnCommentModalClose"
/>
</view>
</template>
+2 -3
View File
@@ -7,7 +7,6 @@ import { computed, ref } from 'vue'
import { onLoad, onPullDownRefresh } from '@dcloudio/uni-app'
import { getPostListByKeyword } from '@/api/halo'
import { useAppConfigStore } from '@/store/appConfig'
import { usePluginAvailable } from '@/utils/plugin'
import { markdownConfig } from '@/config/markdown'
import { formatTime as formatTimeUtil } from '@/utils/formatTime'
import { debounce } from '@/utils/debounce'
@@ -24,7 +23,7 @@ const calcAuditModeEnabled = computed(() => appConfigStore.auditModeEnabled)
/** 依赖插件(plugin-search-widget) */
const uniHaloPluginId = 'plugin-search-widget'
const uniHaloPluginAvailable = ref(true)
const { available: uniHaloPluginAvailable, check: checkPluginAvailable } = usePluginAvailable(uniHaloPluginId)
/* ---------------- 状态 ---------------- */
const loading = ref<'loading' | 'success' | 'error'>('loading')
@@ -122,7 +121,7 @@ function handleToTopPage(duration = 500) {
/* ---------------- 生命周期 ---------------- */
onLoad(async () => {
uniHaloPluginAvailable.value = await usePluginAvailable(uniHaloPluginId)
await checkPluginAvailable()
if (!uniHaloPluginAvailable.value) {
uni.stopPullDownRefresh()
return
+2 -3
View File
@@ -7,7 +7,6 @@ import { computed, ref } from 'vue'
import { onLoad, onPullDownRefresh, onReachBottom } from '@dcloudio/uni-app'
import { getVoteList } from '@/api/uni-halo'
import { useAppConfigStore } from '@/store/appConfig'
import { usePluginAvailable } from '@/utils/plugin'
import type { IVoteItem } from '@/api/types/uni-halo'
definePage({
@@ -22,7 +21,7 @@ const calcAuditModeEnabled = computed(() => appConfigStore.auditModeEnabled)
/** 依赖插件(plugin-vote) */
const uniHaloPluginId = 'plugin-vote'
const uniHaloPluginAvailable = ref(true)
const { available: uniHaloPluginAvailable, check: checkPluginAvailable } = usePluginAvailable(uniHaloPluginId)
/* ---------------- 状态 ---------------- */
const loading = ref<'loading' | 'success' | 'error'>('loading')
@@ -84,7 +83,7 @@ function handleToTopPage(duration = 500) {
/* ---------------- 生命周期 ---------------- */
onLoad(async () => {
uniHaloPluginAvailable.value = await usePluginAvailable(uniHaloPluginId)
await checkPluginAvailable()
if (!uniHaloPluginAvailable.value) {
uni.stopPullDownRefresh()
return