Files
panel-uniapp/pages/mine/my_favorites.vue
T
2025-12-26 11:52:22 +08:00

644 lines
16 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="search">
<view class="search-input-wrapper">
<view class="search-input-box">
<view v-if="searchTag" class="search-tag">
<text>{{searchTag}}</text>
<uni-icons type="closeempty" size="14" color="#666" @click="removeSearchTag"></uni-icons>
</view>
<input
class="search-input"
:placeholder="searchTag ? '' : '请输入搜索内容'"
v-model="searchKeyword"
@confirm="handleSearch"
@input="handleSearchInput"
/>
<uni-icons type="search" size="18" color="#909399" @click="handleSearch"></uni-icons>
</view>
</view>
<view class="sort-options">
<view
class="sort-item"
:class="{active: sortField === 'browse'}"
@click="toggleSort('browse')"
>
<text>观看量</text>
<view class="">
<u-icon name="arrow-up-fill" size="8" :color="sortField === 'browse'&&sortOrder === 'asc' ? '#206ff4' : '#666'"></u-icon>
<u-icon name="arrow-down-fill" size="8" :color="sortField === 'browse'&&sortOrder === 'desc' ? '#206ff4' : '#666'" ></u-icon>
</view>
</view>
<view
class="sort-item"
:class="{active: sortField === 'favorite'}"
@click="toggleSort('favorite')"
>
<text>收藏量</text>
<view class="">
<u-icon name="arrow-up-fill" size="8" :color="sortField === 'favorite'&&sortOrder === 'asc' ? '#206ff4' : '#666'"></u-icon>
<u-icon name="arrow-down-fill" size="8" :color="sortField === 'favorite'&&sortOrder === 'desc' ? '#206ff4' : '#666'" ></u-icon>
</view>
</view>
</view>
</view>
<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">
<image @click="viewPost(post)" class="post-image" :src="post.media && post.media[0] ? post.media[0].url_pull : '/static/image/placeholder.png'" mode="aspectFill"></image>
<view class="" style="flex: 1;display: flex;">
<view @click="viewPost(post)" class="post-tag">{{ formatContent(post.content) }}</view>
<view class="" style="width: 80rpx;text-align: right;">
<uni-icons @click="delpost(post.id)" type="trash" color="#999" size="20"></uni-icons>
</view>
</view>
</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, // 总数据量
searchKeyword: '',
searchTag: '', // 搜索标签(如 #包豪斯)
sortField: '', // 排序字段:''(时间) 或 'browse'(观看量) 或 'favorite'(收藏量)
sortOrder: 'desc', // 排序方向:'asc' 升序 或 'desc' 降序
}
},
computed: {
// 计算当前年份在选择器中的默认索引
defaultYearIndex() {
const currentYear = new Date().getFullYear();
const startYear = currentYear - 5;
return [currentYear - startYear]; // 当前年份在数组中的索引
}
},
onLoad() {
this.loadPostList();
},
methods: {
formatContent(str) {
if (!str) return '';
return str.length > 30 ? str.slice(0, 30) + '...' : str;
},
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);
}
},
//删除帖子
delpost(item){
const that = this;
uni.showModal({
title: '提示',
content: '确定要删除当前帖子吗?',
success: function (res) {
if (res.confirm) {
request({
url:'common/post/destroy',
method:'POST',
data:{
id:item
}
}).then(res => {
if(res.data.code === 200){
// 从帖子列表中移除
const index = that.postList.findIndex(post => post.id === item);
if(index > -1){
that.postList.splice(index, 1);
// 重新分组
that.groupPostsByDate();
// 更新总数
that.total = Math.max(0, that.total - 1);
}
uni.showToast({
title:res.data.message || '删除成功',
icon: 'none'
});
} else {
uni.showToast({
title:res.data.message || '删除失败',
icon: 'none'
});
}
})
}
},
})
},
// 处理搜索输入
handleSearchInput(e) {
const value = e.detail.value;
// 检测是否输入了标签格式(#开头)
if(value.startsWith('#')){
this.searchTag = value;
this.searchKeyword = '';
} else {
this.searchKeyword = value;
if(this.searchTag){
this.searchTag = '';
}
}
},
// 处理搜索
handleSearch() {
if(this.searchKeyword && this.searchKeyword.startsWith('#')){
this.searchTag = this.searchKeyword;
this.searchKeyword = '';
}
// 重新加载数据
this.page = 1;
this.hasMore = true;
this.postList = [];
this.groupedPosts = {};
this.loadPostList(false);
},
// 移除搜索标签
removeSearchTag() {
this.searchTag = '';
this.searchKeyword = '';
// 重新加载数据
this.page = 1;
this.hasMore = true;
this.postList = [];
this.groupedPosts = {};
this.loadPostList(false);
},
// 切换排序
toggleSort(field) {
if(this.sortField === field){
// 如果点击的是当前排序字段,切换排序方向
this.sortOrder = this.sortOrder === 'asc' ? 'desc' : 'asc';
} else {
// 如果点击的是新字段,设置为降序
this.sortField = field;
this.sortOrder = 'desc';
}
// 重新加载数据
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 = {};
}
}
// 构建请求参数
const params = {
// 只获取当前用户发布的帖子,后端应该根据token自动过滤
current: this.page,
size: this.pageSize,
scene:'self',
year:this.selectedYear
};
// 添加搜索参数
if(this.searchTag){
params.keyword = this.searchTag.replace('#', '');
} else if(this.searchKeyword){
params.keyword = this.searchKeyword;
}
// 添加排序参数
if(this.sortField){
params.sort_by = this.sortField;
params.order = this.sortOrder;
} else {
// 默认按时间排序
params.sort_by = '';
params.order = 'desc';
}
// 获取当前用户发布的帖子列表
request({
url: 'common/post/list',
method: 'GET',
data: params
}).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;
}
// 下拉刷新时重置为默认时间排序
if(this.sortField !== ''){
this.sortField = '';
this.sortOrder = 'desc';
}
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;
}
.search{
padding: 20rpx;
display: flex;
align-items: center;
gap: 20rpx;
background: #fff;
}
.search-input-wrapper{
flex: 1;
}
.search-input-box{
display: flex;
align-items: center;
background: #F5F6FA;
border-radius: 10rpx;
padding: 15rpx 24rpx;
gap: 12rpx;
}
.search-tag{
display: flex;
align-items: center;
background: #fff;
border-radius: 20rpx;
padding: 4rpx 12rpx;
gap: 8rpx;
font-size: 24rpx;
color: #333;
margin-right: 8rpx;
flex-shrink: 0;
}
.search-tag text{
font-size: 24rpx;
color: #333;
}
.search-input{
flex: 1;
font-size: 28rpx;
color: #333;
background: transparent;
border: none;
}
.sort-options{
display: flex;
align-items: center;
gap: 24rpx;
flex-shrink: 0;
}
.sort-item{
display: flex;
align-items: center;
gap: 6rpx;
font-size: 26rpx;
color: #666;
cursor: pointer;
transition: color 0.3s;
}
.sort-item.active{
color: #206ff4;
font-weight: 500;
}
.sort-item text{
font-size: 26rpx;
}
.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>