Files
panel-uniapp/pages/mine/my_favorites.vue
T
2025-12-03 14:07:17 +08:00

407 lines
10 KiB
Vue

<template>
<view class="content">
<uni-nav-bar leftText=" " :fixed="true" :border="false" leftIcon="back" @clickLeft="leftClick" color="#000000" backgroundColor="#fff" :statusBar="true" title="我的发布"></uni-nav-bar>
<view class="year-selector" @click="showYearPicker">
<text class="year-text">{{selectedYear}}年</text>
<uni-icons type="arrowdown" size="14" color="#666"></uni-icons>
</view>
<scroll-view scroll-y class="scroll-container"
refresher-enabled
:refresher-triggered="refreshing"
@refresherrefresh="onRefresh"
:lower-threshold="100"
@scrolltolower="onLoadMore">
<view class="post-list">
<view v-for="(group, dateKey) in groupedPosts" :key="dateKey" class="date-group">
<view class="date-item">
<view class="date-label">
<text class="date-day">{{group.day}}</text>
<text class="date-month">{{group.month}}月</text>
</view>
<view class="posts-container">
<view v-for="post in group.posts" :key="post.id" class="post-item" @click="viewPost(post)">
<image class="post-image" :src="post.media && post.media[0] ? post.media[0].url_pull : '/static/image/placeholder.png'" mode="aspectFill"></image>
<text class="post-tag">{{post.content || ''}}</text>
</view>
</view>
</view>
</view>
<!-- 加载更多提示 -->
<view v-if="hasMore && postList.length > 0" class="load-more">
<text v-if="loadingMore">加载中...</text>
<text v-else>上拉加载更多</text>
</view>
<view v-else-if="!hasMore && postList.length > 0" class="load-more">
<text>没有更多了</text>
</view>
<view v-if="postList.length === 0 && !loading && !loadingMore" class="empty-view">
<text>暂无发布内容</text>
</view>
</view>
</scroll-view>
<!-- 年份选择器 -->
<u-picker
:show="showYearSelect"
:columns="yearColumns"
:defaultIndex="defaultYearIndex"
keyName="label"
@confirm="onYearConfirm"
@cancel="showYearSelect = false"
title="选择年份">
</u-picker>
<!-- 加载提示 -->
<view v-if="loading" class="loading-mask">
<view class="loading-text">加载中...</view>
</view>
</view>
</template>
<script>
import { request } from '@/api/apiT.js'
export default {
data() {
// 生成年份列表(当前年份前后5年)
const currentYear = new Date().getFullYear();
const years = [];
for (let i = currentYear - 5; i <= currentYear + 5; i++) {
years.push({ label: i.toString(), value: i });
}
return {
selectedYear: currentYear,
showYearSelect: false,
yearColumns: [years],
postList: [],
groupedPosts: {},
loading: false,
refreshing: false,
loadingMore: false, // 上拉加载状态
page: 1, // 当前页码
pageSize: 10, // 每页数量
hasMore: true, // 是否还有更多数据
total: 0, // 总数据量
}
},
computed: {
// 计算当前年份在选择器中的默认索引
defaultYearIndex() {
const currentYear = new Date().getFullYear();
const startYear = currentYear - 5;
return [currentYear - startYear]; // 当前年份在数组中的索引
}
},
onLoad() {
this.loadPostList();
},
methods: {
leftClick() {
// uni.navigateBack()
uni.reLaunch({
url: '/pages/index/mine',// 替换为你要跳转的页面路径
});
},
showYearPicker() {
this.showYearSelect = true;
},
onYearConfirm(e) {
if (e.value && e.value[0]) {
const selectedItem = typeof e.value[0] === 'object' ? e.value[0] : { value: e.value[0], label: e.value[0].toString() };
this.selectedYear = selectedItem.value || parseInt(selectedItem.label);
this.showYearSelect = false;
// 切换年份时重置分页
this.page = 1;
this.hasMore = true;
this.postList = [];
this.groupedPosts = {};
this.loadPostList(false);
}
},
// 加载帖子列表
loadPostList(isLoadMore = false) {
// 防止重复请求
if (isLoadMore) {
if (this.loadingMore || this.loading || this.refreshing || !this.hasMore) {
return;
}
this.loadingMore = true;
this.page += 1;
} else {
if (this.loading && !this.refreshing) {
return;
}
this.loading = true;
this.page = 1;
this.hasMore = true;
if (!isLoadMore && !this.refreshing) {
this.postList = [];
this.groupedPosts = {};
}
}
// 获取当前用户发布的帖子列表
request({
url: 'common/post/list',
method: 'GET',
data: {
// 只获取当前用户发布的帖子,后端应该根据token自动过滤
current: this.page,
size: this.pageSize,
scene:'self',
year:this.selectedYear
}
}).then(res => {
this.loading = false;
this.loadingMore = false;
this.refreshing = false;
if (res.data.code == 200) {
const records = res.data.data.records || [];
const total = res.data.data.total || 0;
this.total = total;
// 过滤出当前年份的数据
const yearFilteredRecords = records.filter(post => {
if (!post.created_at) return false;
const dateStr = post.created_at;
const postYear = new Date(dateStr).getFullYear();
return postYear === this.selectedYear;
});
if (isLoadMore) {
// 上拉加载更多,追加数据并去重
const existingIds = new Set(this.postList.map(item => item.id));
const newRecords = yearFilteredRecords.filter(item => !existingIds.has(item.id));
this.postList = [...this.postList, ...newRecords];
} else {
// 下拉刷新或初次加载,替换数据
this.postList = yearFilteredRecords;
}
// 重新分组
this.groupPostsByDate();
// 判断是否还有更多数据
// 如果当前页返回的原始数据少于pageSize,说明没有更多了
// 或者已经加载到最后一页
if (records.length < this.pageSize) {
this.hasMore = false;
} else {
// 计算理论上最多能加载多少页
const maxPages = Math.ceil(total / this.pageSize);
this.hasMore = this.page < maxPages;
}
}
}).catch(() => {
this.loading = false;
this.loadingMore = false;
this.refreshing = false;
if (isLoadMore) {
this.page -= 1; // 加载失败,页码回退
}
});
},
// 按日期分组帖子
groupPostsByDate() {
const grouped = {};
this.postList.forEach(post => {
const dateStr = post.created_at || post.create_time || post.createdAt;
if (!dateStr) return;
const date = new Date(dateStr);
const month = date.getMonth() + 1; // 月份从0开始,需要+1
const day = date.getDate();
// 生成日期key,格式:MM-DD,用于排序
const dateKey = `${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
if (!grouped[dateKey]) {
grouped[dateKey] = {
day: day,
month: month,
posts: []
};
}
grouped[dateKey].posts.push(post);
});
// 按日期倒序排序(最新的在前)
const sortedKeys = Object.keys(grouped).sort((a, b) => {
// 按完整日期排序(年-月-日)
const dateA = new Date(this.selectedYear, parseInt(a.split('-')[0]) - 1, parseInt(a.split('-')[1]));
const dateB = new Date(this.selectedYear, parseInt(b.split('-')[0]) - 1, parseInt(b.split('-')[1]));
return dateB - dateA; // 倒序:最新的在前
});
// 重新构建分组对象,保持顺序
const sortedGrouped = {};
sortedKeys.forEach(key => {
sortedGrouped[key] = grouped[key];
});
this.groupedPosts = sortedGrouped;
},
// 下拉刷新
onRefresh() {
// 如果正在加载中,不执行刷新
if (this.loading || this.loadingMore) {
this.refreshing = false;
return;
}
this.refreshing = true;
this.page = 1;
this.hasMore = true;
this.loadPostList(false);
},
// 上拉加载更多
onLoadMore() {
if (!this.hasMore || this.loadingMore || this.loading || this.refreshing) {
return;
}
this.loadPostList(true);
},
// 查看帖子详情
viewPost(post) {
// TODO: 跳转到帖子详情页
uni.navigateTo({
url: '/pages/square/detiles?id='+post.id
});
}
}
}
</script>
<style lang="scss">
page,
.content {
height: 100%;
background-color: #fff;
}
.year-selector {
display: flex;
align-items: center;
justify-content: space-between;
padding: 20rpx 30rpx;
background-color: #F5F5F5;
.year-text {
font-size: 28rpx;
color: #333;
}
.arrow-icon {
width: 24rpx;
height: 24rpx;
}
}
.scroll-container {
height: calc(100% - 168rpx); // 减去导航栏和年份选择器高度
}
.post-list {
padding: 20rpx 0;
}
.date-group {
margin-bottom: 40rpx;
}
.date-item {
display: flex;
padding: 0 30rpx;
}
.date-label {
display: flex;
// flex-direction: column;
// align-items: center;
margin-right: 30rpx;
min-width: 120rpx;
.date-day {
font-size: 48rpx;
font-weight: bold;
color: #333;
line-height: 1;
}
.date-month {
font-size: 24rpx;
color: #666;
margin-top: 8rpx;
}
}
.posts-container {
flex: 1;
display: flex;
flex-direction: column;
gap: 20rpx;
}
.post-item {
display: flex;
// align-items: center;
margin-bottom: 30rpx;
.post-image {
width: 200rpx;
height: 200rpx;
background-color: #E5E5E5;
border-radius: 10rpx;
margin-right: 30rpx;
flex-shrink: 0;
}
.post-tag {
font-size: 28rpx;
color: #333;
flex: 1;
}
}
.empty-view {
text-align: center;
padding: 100rpx 0;
color: #999;
font-size: 28rpx;
}
.load-more {
width: 100%;
text-align: center;
padding: 30rpx 0;
font-size: 26rpx;
color: #999;
}
.loading-mask {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.3);
display: flex;
align-items: center;
justify-content: center;
z-index: 999;
.loading-text {
color: #fff;
font-size: 28rpx;
background-color: rgba(0, 0, 0, 0.7);
padding: 20rpx 40rpx;
border-radius: 8rpx;
}
}
</style>