341 lines
10 KiB
Vue
341 lines
10 KiB
Vue
<template>
|
|
<view class="family-page" :class="`feed-state--${feedState}`">
|
|
<ModulePageBackground module="family" />
|
|
<view class="family-page__header"
|
|
><PageHeader
|
|
title="家族动态"
|
|
custom-back
|
|
:action="hasValidContext ? '发布' : ''"
|
|
@back="requestBack"
|
|
@action="toPublish"
|
|
/></view>
|
|
<view class="feed-content">
|
|
<view class="feed-heading"
|
|
><text>家族圈</text><text>家宴、通知与共同记忆</text></view
|
|
>
|
|
<AppLoading
|
|
v-if="feedState === 'loading'"
|
|
text="正在读取家族动态"
|
|
description="请稍候,正在整理家族近况。"
|
|
/>
|
|
<view v-else-if="feedState === 'list'" class="feed-list">
|
|
<view
|
|
v-for="item in feeds"
|
|
:key="item.id"
|
|
class="feed-card"
|
|
@click="openFeed(item)"
|
|
>
|
|
<text class="feed-card__publisher">{{ item.publisher }}</text>
|
|
<text class="feed-card__content">{{ feedPreview(item.content) }}</text>
|
|
<FamilyFeedMedia :files="item.mediaFiles" />
|
|
<text class="feed-card__meta">{{ feedTypeLabel(item.type) }} · {{ formatMinuteTimestamp(item.time) }}</text>
|
|
<text v-if="item.recommendationReason" class="feed-card__reason">{{ item.recommendationReason }}</text>
|
|
</view>
|
|
<button
|
|
v-if="hasMoreFeeds || loadMoreState === 'loading' || loadMoreState === 'error'"
|
|
class="feed-more"
|
|
:disabled="loadMoreState === 'loading'"
|
|
@click="loadMoreFeeds"
|
|
>{{ loadMoreLabel }}</button>
|
|
</view>
|
|
<view v-else class="feed-state-card"
|
|
><view class="feed-state-card__copy"
|
|
><text>{{ stateCopy.title }}</text
|
|
><text>{{ stateCopy.copy }}</text></view
|
|
></view
|
|
>
|
|
<view class="feed-action" @click="handlePrimaryAction"
|
|
><text>{{ stateCopy.action }}</text></view
|
|
>
|
|
</view>
|
|
</view>
|
|
</template>
|
|
|
|
<script setup>
|
|
import { computed, ref } from "vue";
|
|
import { onBackPress, onLoad, onShow, onUnload } from "@dcloudio/uni-app";
|
|
import AppLoading from "@/components/AppLoading.vue";
|
|
import FamilyFeedMedia from "@/components/family/FeedMedia.vue";
|
|
import ModulePageBackground from "@/components/ModulePageBackground.vue";
|
|
import PageHeader from "@/components/PageHeader.vue";
|
|
import {
|
|
createRequestController,
|
|
isRequestCancelled
|
|
} from "@/services/api/request-controller.js";
|
|
import { familyFeedApi } from "@/services/api/family-feed-service.js";
|
|
import { formatMinuteTimestamp } from "@/utils/display-time.js";
|
|
import { goBack, handleBackPress, openPage } from "@/utils/navigation/gateway.js";
|
|
|
|
const genealogyId = ref("");
|
|
const hasValidContext = ref(false);
|
|
const feedState = ref("loading");
|
|
const feeds = ref([]);
|
|
const FEED_PAGE_SIZE = 20;
|
|
const totalFeedCount = ref(0);
|
|
const currentFeedPage = ref(1);
|
|
const loadMoreState = ref("idle");
|
|
const hasMoreFeeds = computed(
|
|
() => feedState.value === "list" && feeds.value.length < totalFeedCount.value,
|
|
);
|
|
const loadMoreLabel = computed(() => {
|
|
if (loadMoreState.value === "loading") return "正在加载";
|
|
if (loadMoreState.value === "error") return "加载失败,重新加载";
|
|
return "继续加载";
|
|
});
|
|
let active = true;
|
|
let skipInitialShowRefresh = true;
|
|
const feedListRequestController = createRequestController();
|
|
const recommendationRequestController = createRequestController();
|
|
const feedTypeLabel = (type) =>
|
|
String(type || "")
|
|
.trim()
|
|
.toLowerCase() === "text"
|
|
? "文字动态"
|
|
: "家族动态";
|
|
const feedPreview = (content) => {
|
|
const text = String(content || "").trim();
|
|
return text.length > 96 ? `${text.slice(0, 96).trimEnd()}…` : text;
|
|
};
|
|
const stateCopy = computed(() =>
|
|
!hasValidContext.value
|
|
? {
|
|
title: "请先选择有效家谱",
|
|
copy: "家族动态必须归属明确家谱。",
|
|
action: "返回我的家谱",
|
|
}
|
|
: feedState.value === "empty"
|
|
? {
|
|
title: "还没有家族动态",
|
|
copy: "发布第一条动态,记录家族此刻。",
|
|
action: "发布家族动态",
|
|
}
|
|
: feedState.value === "error"
|
|
? {
|
|
title: "动态暂时无法读取",
|
|
copy: "请稍后重新读取。",
|
|
action: "重新读取",
|
|
}
|
|
: { title: "", copy: "", action: "发布家族动态" },
|
|
);
|
|
const loadFeeds = async () => {
|
|
if (!hasValidContext.value) return;
|
|
feedState.value = "loading";
|
|
loadMoreState.value = "idle";
|
|
currentFeedPage.value = 1;
|
|
feedListRequestController.abort();
|
|
recommendationRequestController.abort();
|
|
try {
|
|
const [pageResult, recommendationResult] = await Promise.allSettled([
|
|
familyFeedApi.getFeedPage(
|
|
genealogyId.value,
|
|
{ pageNum: 1, pageSize: FEED_PAGE_SIZE },
|
|
{ requestController: feedListRequestController },
|
|
),
|
|
familyFeedApi.getFeedRecommendations(genealogyId.value, {
|
|
requestController: recommendationRequestController,
|
|
}),
|
|
]);
|
|
if (pageResult.status === "rejected") throw pageResult.reason;
|
|
if (!active) return;
|
|
const recommendations =
|
|
recommendationResult.status === "fulfilled"
|
|
? recommendationResult.value
|
|
: [];
|
|
const reasons = new Map(
|
|
recommendations.map((item) => [String(item.id), item.recommendationReason]),
|
|
);
|
|
feeds.value = pageResult.value.rows.map((item) => ({
|
|
...item,
|
|
recommendationReason: reasons.get(String(item.id)) || "",
|
|
}));
|
|
totalFeedCount.value = pageResult.value.total;
|
|
loadMoreState.value =
|
|
feeds.value.length < totalFeedCount.value ? "idle" : "done";
|
|
feedState.value = feeds.value.length ? "list" : "empty";
|
|
} catch (error) {
|
|
if (!active || isRequestCancelled(error)) return;
|
|
feedState.value = "error";
|
|
}
|
|
};
|
|
const loadMoreFeeds = async () => {
|
|
if (!hasMoreFeeds.value || loadMoreState.value === "loading") return;
|
|
loadMoreState.value = "loading";
|
|
try {
|
|
const nextPage = currentFeedPage.value + 1;
|
|
const page = await familyFeedApi.getFeedPage(
|
|
genealogyId.value,
|
|
{ pageNum: nextPage, pageSize: FEED_PAGE_SIZE },
|
|
{ requestController: feedListRequestController },
|
|
);
|
|
if (!active) return;
|
|
const knownIds = new Set(feeds.value.map((item) => String(item.id)));
|
|
feeds.value = feeds.value.concat(
|
|
page.rows.filter((item) => !knownIds.has(String(item.id))),
|
|
);
|
|
currentFeedPage.value = nextPage;
|
|
totalFeedCount.value = page.total;
|
|
loadMoreState.value =
|
|
feeds.value.length < totalFeedCount.value ? "idle" : "done";
|
|
} catch (error) {
|
|
if (!active || isRequestCancelled(error)) return;
|
|
loadMoreState.value = "error";
|
|
}
|
|
};
|
|
onLoad((query) => {
|
|
genealogyId.value = String(query?.genealogyId || "");
|
|
hasValidContext.value = /^[1-9]\d*$/.test(genealogyId.value);
|
|
if (hasValidContext.value) void loadFeeds();
|
|
else feedState.value = "error";
|
|
});
|
|
onShow(() => {
|
|
if (skipInitialShowRefresh) {
|
|
skipInitialShowRefresh = false;
|
|
return;
|
|
}
|
|
if (hasValidContext.value) void loadFeeds();
|
|
});
|
|
onUnload(() => {
|
|
active = false;
|
|
feedListRequestController.abort();
|
|
recommendationRequestController.abort();
|
|
});
|
|
const toPublish = () =>
|
|
hasValidContext.value
|
|
? openPage("F02", { genealogyId: genealogyId.value, mode: "create" }, "F12")
|
|
: goBack();
|
|
const openFeed = (item) =>
|
|
openPage("F03", { genealogyId: genealogyId.value, feedId: item.id }, "F12");
|
|
const handlePrimaryAction = () => {
|
|
if (!hasValidContext.value) return goBack();
|
|
return feedState.value === "error" ? loadFeeds() : toPublish();
|
|
};
|
|
const requestBack = () => goBack();
|
|
onBackPress((event) => handleBackPress(event, requestBack));
|
|
</script>
|
|
|
|
<style scoped lang="scss">
|
|
@import "../../styles/adaptive-frame-profiles.scss";
|
|
.family-page {
|
|
display: flex;
|
|
min-height: 100vh;
|
|
flex-direction: column;
|
|
background: $paper;
|
|
}
|
|
.family-page__header,
|
|
.feed-content {
|
|
z-index: 1;
|
|
}
|
|
.feed-content {
|
|
flex: 1;
|
|
padding: 24rpx 24rpx calc(48rpx + env(safe-area-inset-bottom));
|
|
}
|
|
.feed-heading text {
|
|
display: block;
|
|
}
|
|
.feed-heading text:first-child {
|
|
color: $ink;
|
|
font-family: STKaiti, KaiTi, serif;
|
|
font-size: clamp(17px, 31rpx, 22px);
|
|
font-weight: 700;
|
|
}
|
|
.feed-heading text:last-child {
|
|
margin-top: 6rpx;
|
|
color: $ink-muted;
|
|
font-size: clamp(14px, 23rpx, 17px);
|
|
}
|
|
.feed-list {
|
|
margin-top: 22rpx;
|
|
}
|
|
.feed-card,
|
|
.feed-state-card {
|
|
@include adaptive-family-letter;
|
|
box-sizing: border-box;
|
|
margin-top: 16rpx;
|
|
padding: 30rpx;
|
|
background-color: rgba($paper, 0.82);
|
|
}
|
|
.feed-card text,
|
|
.feed-state-card text {
|
|
display: block;
|
|
}
|
|
.feed-card__publisher {
|
|
color: $brand-red;
|
|
font-size: clamp(14px, 22rpx, 17px);
|
|
}
|
|
.feed-card__content {
|
|
display: -webkit-box;
|
|
margin-top: 12rpx;
|
|
overflow: hidden;
|
|
color: $ink;
|
|
font-size: clamp(16px, 28rpx, 20px);
|
|
line-height: 1.55;
|
|
overflow-wrap: anywhere;
|
|
-webkit-box-orient: vertical;
|
|
-webkit-line-clamp: 3;
|
|
}
|
|
.feed-card__meta {
|
|
margin-top: 12rpx;
|
|
color: $ink-muted;
|
|
font-size: clamp(13px, 21rpx, 16px);
|
|
}
|
|
.feed-card__reason {
|
|
margin-top: 8rpx;
|
|
color: #9a6555;
|
|
font-size: clamp(12px, 20rpx, 15px);
|
|
}
|
|
.feed-more {
|
|
@include adaptive-scroll-button(secondary);
|
|
display: block;
|
|
width: 420rpx;
|
|
max-width: 100%;
|
|
min-height: 80rpx;
|
|
margin: 22rpx auto 0;
|
|
padding: 0 24rpx;
|
|
border: 0;
|
|
color: $ink;
|
|
font-size: clamp(14px, 22rpx, 17px);
|
|
font-weight: 700;
|
|
line-height: 80rpx;
|
|
}
|
|
.feed-more::after {
|
|
border: 0;
|
|
}
|
|
.feed-more[disabled] {
|
|
opacity: 0.62;
|
|
}
|
|
.feed-state-card {
|
|
min-height: 230rpx;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
text-align: center;
|
|
}
|
|
.feed-state-card text:first-child {
|
|
color: $ink;
|
|
font-size: clamp(17px, 31rpx, 22px);
|
|
font-weight: 700;
|
|
}
|
|
.feed-state-card text:last-child {
|
|
margin-top: 13rpx;
|
|
color: $ink-muted;
|
|
font-size: clamp(14px, 23rpx, 17px);
|
|
line-height: 1.45;
|
|
}
|
|
.feed-action {
|
|
@include adaptive-scroll-button(primary);
|
|
width: 514rpx;
|
|
max-width: 100%;
|
|
min-height: 80rpx;
|
|
margin: 19rpx auto;
|
|
}
|
|
.feed-action text {
|
|
display: flex;
|
|
min-height: 80rpx;
|
|
align-items: center;
|
|
justify-content: center;
|
|
color: #fff9ed;
|
|
font-size: clamp(15px, 24rpx, 18px);
|
|
font-weight: 700;
|
|
}
|
|
</style>
|