This commit is contained in:
ChuXun
2025-10-19 20:28:31 +08:00
parent c81f8a8b03
commit eaab9a762a
100 changed files with 23416 additions and 0 deletions

191
pages/forum/forum.js Normal file
View File

@@ -0,0 +1,191 @@
// pages/forum/forum.js
const {forumData} = require('../../utils/data.js')
const learningTracker = require('../../utils/learningTracker.js')
Page({
data: {
posts: [],
categories: ['全部', '数学', '物理', '计算机', '英语', '其他'],
selectedCategory: '全部'
},
onLoad() {
this.loadPosts()
},
onShow() {
// 开始跟踪学习时间
learningTracker.onPageShow('forum')
// 每次显示时重新加载,以获取最新发布的帖子
this.loadPosts()
// 更新自定义TabBar选中状态
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
this.getTabBar().setData({
selected: 2
})
}
},
onHide() {
// 停止跟踪学习时间
learningTracker.onPageHide()
},
onUnload() {
// 记录学习时长
learningTracker.onPageUnload()
},
// 加载帖子
loadPosts() {
try {
let posts = wx.getStorageSync('forumPosts') || forumData
// 同步评论数量
const allComments = wx.getStorageSync('forumComments') || {}
// 获取收藏的帖子列表
const favoritePosts = wx.getStorageSync('favoritePosts') || []
posts = posts.map(post => {
const comments = allComments[post.id] || []
post.comments = comments.length
// 检查是否已收藏
post.isFavorite = favoritePosts.some(fav => fav.id === post.id)
return post
})
// 保存更新后的帖子数据
wx.setStorageSync('forumPosts', posts)
// 按时间排序(最新的在前)
posts.sort((a, b) => b.id - a.id)
this.setData({ posts })
this.filterPosts()
} catch (error) {
console.error('加载帖子失败:', error)
wx.showToast({
title: '数据加载失败',
icon: 'none'
})
}
},
// 分类筛选
onCategoryChange(e) {
const category = e.currentTarget.dataset.category
this.setData({ selectedCategory: category })
this.filterPosts()
},
// 筛选帖子
filterPosts() {
const { selectedCategory } = this.data
let allPosts = wx.getStorageSync('forumPosts') || forumData
// 同步评论数量和收藏状态
const allComments = wx.getStorageSync('forumComments') || {}
const favoritePosts = wx.getStorageSync('favoritePosts') || []
allPosts = allPosts.map(post => {
const comments = allComments[post.id] || []
post.comments = comments.length
// 检查是否已收藏
post.isFavorite = favoritePosts.some(fav => fav.id === post.id)
return post
})
if (selectedCategory === '全部') {
this.setData({ posts: allPosts })
} else {
const filtered = allPosts.filter(post => post.category === selectedCategory)
this.setData({ posts: filtered })
}
},
// 查看帖子详情
onPostDetail(e) {
const { id } = e.currentTarget.dataset
wx.navigateTo({
url: `/pages/forum-detail/forum-detail?id=${id}`
})
},
// 发布新帖子
onNewPost() {
wx.navigateTo({
url: '/pages/post/post'
})
},
// 点赞
onLike(e) {
const { id } = e.currentTarget.dataset
let posts = wx.getStorageSync('forumPosts') || forumData
posts = posts.map(post => {
if (post.id === id) {
post.isLiked = !post.isLiked
post.likes = post.isLiked ? post.likes + 1 : post.likes - 1
}
return post
})
wx.setStorageSync('forumPosts', posts)
this.loadPosts()
},
// 预览图片
onPreviewImage(e) {
const { url, urls } = e.currentTarget.dataset
wx.previewImage({
current: url, // 当前显示图片的链接
urls: urls // 需要预览的图片链接列表
})
},
// 收藏/取消收藏
onFavorite(e) {
const { id } = e.currentTarget.dataset
let favoritePosts = wx.getStorageSync('favoritePosts') || []
let posts = wx.getStorageSync('forumPosts') || forumData
// 找到当前帖子
const currentPost = posts.find(post => post.id === id)
if (!currentPost) return
// 检查是否已收藏
const index = favoritePosts.findIndex(fav => fav.id === id)
if (index > -1) {
// 已收藏,取消收藏
favoritePosts.splice(index, 1)
wx.showToast({
title: '取消收藏',
icon: 'success',
duration: 1500
})
} else {
// 未收藏,添加收藏
favoritePosts.push({
id: currentPost.id,
title: currentPost.title,
category: currentPost.category,
time: new Date().toLocaleString()
})
wx.showToast({
title: '收藏成功',
icon: 'success',
duration: 1500
})
}
// 保存收藏列表
wx.setStorageSync('favoritePosts', favoritePosts)
// 重新加载帖子列表
this.loadPosts()
}
})

3
pages/forum/forum.json Normal file
View File

@@ -0,0 +1,3 @@
{
"navigationBarTitleText": "学科论坛"
}

99
pages/forum/forum.wxml Normal file
View File

@@ -0,0 +1,99 @@
<!--pages/forum/forum.wxml-->
<view class="container">
<!-- 分类标签 -->
<scroll-view class="category-scroll" scroll-x>
<view class="category-list">
<view
class="category-item {{selectedCategory === item ? 'active' : ''}}"
wx:for="{{categories}}"
wx:key="index"
data-category="{{item}}"
bindtap="onCategoryChange"
>
{{item}}
</view>
</view>
</scroll-view>
<!-- 帖子列表 -->
<view class="post-list">
<view
class="post-card"
wx:for="{{posts}}"
wx:key="id"
bindtap="onPostDetail"
data-id="{{item.id}}"
>
<!-- 帖子头部 -->
<view class="post-header">
<image class="avatar" src="{{item.avatar || '/images/avatar-default.png'}}" mode="aspectFill"></image>
<view class="author-info">
<view class="author-name">{{item.author}}</view>
<view class="post-time">{{item.time}}</view>
</view>
<view class="category-tag">{{item.category}}</view>
</view>
<!-- 帖子内容 -->
<view class="post-content">
<view class="post-title">{{item.title}}</view>
<view class="post-text">{{item.content}}</view>
<!-- 图片 -->
<view class="post-images" wx:if="{{item.images && item.images.length > 0}}">
<image
class="post-image"
wx:for="{{item.images}}"
wx:key="index"
wx:for-item="img"
src="{{img}}"
mode="aspectFill"
catchtap="onPreviewImage"
data-url="{{img}}"
data-urls="{{item.images}}"
></image>
</view>
</view>
<!-- 帖子底部 -->
<view class="post-footer">
<view class="footer-left">
<view class="stat-item">
<text class="stat-icon">👀</text>
<text class="stat-text">{{item.views}}</text>
</view>
<view
class="stat-item like-item {{item.isLiked ? 'liked' : ''}}"
catchtap="onLike"
data-id="{{item.id}}"
>
<text class="stat-icon">{{item.isLiked ? '❤️' : '🤍'}}</text>
<text class="stat-text">{{item.likes}}</text>
</view>
<view class="stat-item">
<text class="stat-icon">💬</text>
<text class="stat-text">{{item.comments}}</text>
</view>
</view>
<view
class="favorite-btn {{item.isFavorite ? 'favorited' : ''}}"
catchtap="onFavorite"
data-id="{{item.id}}"
>
<text class="favorite-icon">{{item.isFavorite ? '⭐' : '☆'}}</text>
</view>
</view>
</view>
<!-- 空状态 -->
<view class="empty-state" wx:if="{{posts.length === 0}}">
<text class="empty-icon">📝</text>
<text class="empty-text">暂无帖子,快来发布第一条吧</text>
</view>
</view>
<!-- 发布按钮 -->
<view class="fab" bindtap="onNewPost">
<text class="fab-icon">✏️</text>
</view>
</view>

338
pages/forum/forum.wxss Normal file
View File

@@ -0,0 +1,338 @@
/* pages/forum/forum.wxss */
.container {
padding-bottom: 30rpx;
background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
min-height: 100vh;
animation: fadeIn 0.5s ease-out;
/* 底部留出TabBar的空间 */
padding-bottom: calc(env(safe-area-inset-bottom) + 150rpx);
}
/* 分类标签 */
.category-scroll {
white-space: nowrap;
background: linear-gradient(135deg, #ffffff 0%, #fafbfc 100%);
padding: 24rpx 0;
position: sticky;
top: 0;
z-index: 100;
box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
backdrop-filter: blur(10rpx);
animation: slideInDown 0.6s cubic-bezier(0.4, 0, 0.2, 1);
}
.category-list {
display: inline-flex;
padding: 0 30rpx;
gap: 20rpx;
}
.category-item {
display: inline-block;
padding: 14rpx 32rpx;
background: linear-gradient(135deg, #f5f5f5 0%, #e8e8e8 100%);
color: #666666;
border-radius: 32rpx;
font-size: 26rpx;
font-weight: 500;
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.06);
}
.category-item:active {
transform: scale(0.95);
}
.category-item.active {
background: linear-gradient(135deg, #50C878 0%, #3CB371 100%);
color: #ffffff;
box-shadow: 0 4rpx 12rpx rgba(80, 200, 120, 0.3);
transform: scale(1.05);
}
/* 帖子列表 */
.post-list {
padding: 24rpx 30rpx;
}
.post-card {
background: linear-gradient(135deg, #ffffff 0%, #fafbfc 100%);
border-radius: 20rpx;
padding: 35rpx;
margin-bottom: 24rpx;
box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.08);
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
animation: slideInUp 0.6s ease-out;
position: relative;
overflow: hidden;
}
.post-card::before {
content: '';
position: absolute;
left: 0;
top: 0;
bottom: 0;
width: 6rpx;
background: linear-gradient(to bottom, #50C878, #3CB371);
transform: scaleY(0);
transition: transform 0.3s ease;
}
.post-card:active {
transform: translateY(-4rpx);
box-shadow: 0 12rpx 32rpx rgba(0, 0, 0, 0.12);
}
.post-card:active::before {
transform: scaleY(1);
}
.post-card:nth-child(1) { animation-delay: 0.1s; }
.post-card:nth-child(2) { animation-delay: 0.15s; }
.post-card:nth-child(3) { animation-delay: 0.2s; }
.post-card:nth-child(4) { animation-delay: 0.25s; }
.post-card:nth-child(5) { animation-delay: 0.3s; }
/* 帖子头部 */
.post-header {
display: flex;
align-items: center;
margin-bottom: 24rpx;
}
.avatar {
width: 80rpx;
height: 80rpx;
border-radius: 50%;
margin-right: 18rpx;
background: linear-gradient(135deg, #e0e0e0 0%, #c8c8c8 100%);
box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
transition: all 0.3s ease;
}
.avatar:active {
transform: scale(1.1);
}
.author-info {
flex: 1;
}
.author-name {
font-size: 30rpx;
font-weight: bold;
color: #333333;
margin-bottom: 8rpx;
}
.post-time {
font-size: 24rpx;
color: #999999;
opacity: 0.8;
}
.category-tag {
padding: 10rpx 22rpx;
background: linear-gradient(135deg, #E8F8F0 0%, #D5F2E3 100%);
color: #50C878;
border-radius: 24rpx;
font-size: 22rpx;
font-weight: 600;
box-shadow: 0 2rpx 6rpx rgba(80, 200, 120, 0.2);
}
/* 帖子内容 */
.post-content {
margin-bottom: 24rpx;
}
.post-title {
font-size: 34rpx;
font-weight: bold;
color: #333333;
margin-bottom: 18rpx;
line-height: 1.6;
position: relative;
}
.post-text {
font-size: 28rpx;
color: #666666;
line-height: 1.8;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
}
.post-images {
display: flex;
gap: 12rpx;
margin-top: 18rpx;
flex-wrap: wrap;
}
.post-image {
width: 200rpx;
height: 200rpx;
border-radius: 12rpx;
background: linear-gradient(135deg, #f5f5f5 0%, #e8e8e8 100%);
box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
transition: all 0.3s ease;
}
.post-image:active {
transform: scale(0.95);
}
/* 帖子底部 */
.post-footer {
display: flex;
align-items: center;
justify-content: space-between;
padding-top: 24rpx;
border-top: 2rpx solid rgba(0, 0, 0, 0.05);
}
.footer-left {
display: flex;
align-items: center;
gap: 45rpx;
flex: 1;
}
.stat-item {
display: flex;
align-items: center;
gap: 10rpx;
font-size: 24rpx;
color: #999999;
padding: 8rpx 16rpx;
border-radius: 20rpx;
transition: all 0.3s ease;
}
.stat-item:active {
background-color: rgba(0, 0, 0, 0.03);
transform: scale(1.1);
}
.stat-icon {
font-size: 32rpx;
transition: all 0.3s ease;
}
.like-item.liked .stat-text {
color: #FF6B6B;
font-weight: bold;
}
.like-item.liked .stat-icon {
animation: pulse 0.6s ease;
}
/* 收藏按钮 */
.favorite-btn {
display: flex;
align-items: center;
justify-content: center;
width: 68rpx;
height: 68rpx;
border-radius: 50%;
background: linear-gradient(135deg, #f5f5f5 0%, #e8e8e8 100%);
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.08);
}
.favorite-btn:active {
transform: scale(0.9) rotate(72deg);
box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.12);
}
.favorite-btn.favorited {
background: linear-gradient(135deg, #FFD700 0%, #FFA500 100%);
box-shadow: 0 4rpx 12rpx rgba(255, 215, 0, 0.4);
}
.favorite-icon {
font-size: 38rpx;
transition: all 0.3s ease;
color: #999999;
}
.favorite-btn.favorited .favorite-icon {
color: #ffffff;
animation: bounce 0.6s ease;
}
/* 收藏动画 */
@keyframes bounce {
0%, 100% {
transform: scale(1);
}
25% {
transform: scale(1.3);
}
50% {
transform: scale(0.9);
}
75% {
transform: scale(1.1);
}
}
/* 空状态 */
.empty-state {
display: flex;
flex-direction: column;
align-items: center;
padding: 120rpx 60rpx;
animation: fadeIn 0.8s ease-out;
}
.empty-icon {
font-size: 120rpx;
margin-bottom: 24rpx;
opacity: 0.4;
animation: pulse 2s ease-in-out infinite;
}
.empty-text {
font-size: 28rpx;
color: #999999;
}
/* 发布按钮 */
.fab {
position: fixed;
right: 30rpx;
bottom: calc(env(safe-area-inset-bottom) + 120rpx);
width: 110rpx;
height: 110rpx;
border-radius: 50%;
background: linear-gradient(135deg, #50C878 0%, #3CB371 100%);
box-shadow: 0 12rpx 32rpx rgba(80, 200, 120, 0.5);
display: flex;
align-items: center;
justify-content: center;
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
animation: scaleIn 0.6s cubic-bezier(0.4, 0, 0.2, 1) 0.5s both;
z-index: 999;
}
.fab:active {
transform: scale(0.88) rotate(90deg);
box-shadow: 0 6rpx 16rpx rgba(80, 200, 120, 0.5);
}
.fab-icon {
font-size: 52rpx;
color: #ffffff;
transition: all 0.3s ease;
}
.fab:active .fab-icon {
transform: rotate(-90deg);
}