1
This commit is contained in:
120
pages/post/post.js
Normal file
120
pages/post/post.js
Normal file
@@ -0,0 +1,120 @@
|
||||
// pages/post/post.js
|
||||
const { forumData } = require('../../utils/data.js')
|
||||
const { showSuccess, showError } = require('../../utils/util.js')
|
||||
const learningTracker = require('../../utils/learningTracker.js')
|
||||
|
||||
// pages/post/post.js
|
||||
const userManager = require('../../utils/userManager.js')
|
||||
|
||||
Page({
|
||||
data: {
|
||||
title: '',
|
||||
content: '',
|
||||
images: [],
|
||||
categories: ['数学', '物理', '计算机', '英语', '其他'],
|
||||
selectedCategory: 0
|
||||
},
|
||||
|
||||
onShow() {
|
||||
// 开始跟踪学习时间
|
||||
learningTracker.onPageShow('forum')
|
||||
},
|
||||
|
||||
onHide() {
|
||||
// 停止跟踪学习时间
|
||||
learningTracker.onPageHide()
|
||||
},
|
||||
|
||||
onUnload() {
|
||||
// 记录学习时长
|
||||
learningTracker.onPageUnload()
|
||||
},
|
||||
|
||||
// 标题输入
|
||||
onTitleInput(e) {
|
||||
this.setData({ title: e.detail.value })
|
||||
},
|
||||
|
||||
// 内容输入
|
||||
onContentInput(e) {
|
||||
this.setData({ content: e.detail.value })
|
||||
},
|
||||
|
||||
// 选择分类
|
||||
onCategoryChange(e) {
|
||||
this.setData({ selectedCategory: parseInt(e.detail.value) })
|
||||
},
|
||||
|
||||
// 选择图片
|
||||
onChooseImage() {
|
||||
const maxCount = 3 - this.data.images.length
|
||||
|
||||
wx.chooseImage({
|
||||
count: maxCount,
|
||||
sizeType: ['compressed'],
|
||||
sourceType: ['album', 'camera'],
|
||||
success: (res) => {
|
||||
const tempFilePaths = res.tempFilePaths
|
||||
const images = this.data.images.concat(tempFilePaths)
|
||||
this.setData({ images })
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
// 删除图片
|
||||
onDeleteImage(e) {
|
||||
const index = e.currentTarget.dataset.index
|
||||
const images = this.data.images
|
||||
images.splice(index, 1)
|
||||
this.setData({ images })
|
||||
},
|
||||
|
||||
// 发布帖子
|
||||
onPublish() {
|
||||
const { title, content, images, categories, selectedCategory } = this.data
|
||||
|
||||
if (!title.trim()) {
|
||||
showError('请输入标题')
|
||||
return
|
||||
}
|
||||
|
||||
if (!content.trim()) {
|
||||
showError('请输入内容')
|
||||
return
|
||||
}
|
||||
|
||||
// 获取用户信息
|
||||
const userInfo = userManager.getUserInfo()
|
||||
const userName = userInfo.nickname || '同学'
|
||||
const userAvatar = userInfo.avatar || '/images/avatar-default.png'
|
||||
|
||||
// 创建新帖子
|
||||
const newPost = {
|
||||
id: Date.now(),
|
||||
title: title.trim(),
|
||||
content: content.trim(),
|
||||
author: userName,
|
||||
avatar: userAvatar,
|
||||
category: categories[selectedCategory],
|
||||
views: 0,
|
||||
likes: 0,
|
||||
comments: 0,
|
||||
time: '刚刚',
|
||||
images: images, // 保存图片数组
|
||||
isLiked: false, // 确保默认未点赞
|
||||
commentList: [] // 初始化评论列表
|
||||
}
|
||||
|
||||
// 保存到本地存储
|
||||
let posts = wx.getStorageSync('forumPosts') || forumData
|
||||
posts.unshift(newPost)
|
||||
wx.setStorageSync('forumPosts', posts)
|
||||
|
||||
showSuccess('发布成功')
|
||||
|
||||
// 返回论坛列表
|
||||
setTimeout(() => {
|
||||
wx.navigateBack()
|
||||
}, 1000)
|
||||
}
|
||||
})
|
||||
3
pages/post/post.json
Normal file
3
pages/post/post.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"navigationBarTitleText": "发布帖子"
|
||||
}
|
||||
82
pages/post/post.wxml
Normal file
82
pages/post/post.wxml
Normal file
@@ -0,0 +1,82 @@
|
||||
<!--pages/post/post.wxml-->
|
||||
<view class="container">
|
||||
<view class="form-section">
|
||||
<!-- 分类选择 -->
|
||||
<view class="form-item">
|
||||
<view class="form-label">选择分类</view>
|
||||
<picker
|
||||
mode="selector"
|
||||
range="{{categories}}"
|
||||
value="{{selectedCategory}}"
|
||||
bindchange="onCategoryChange"
|
||||
>
|
||||
<view class="picker-value">
|
||||
{{categories[selectedCategory]}} ▼
|
||||
</view>
|
||||
</picker>
|
||||
</view>
|
||||
|
||||
<!-- 标题输入 -->
|
||||
<view class="form-item">
|
||||
<view class="form-label">标题</view>
|
||||
<input
|
||||
class="title-input"
|
||||
placeholder="请输入标题(必填)"
|
||||
value="{{title}}"
|
||||
bindinput="onTitleInput"
|
||||
maxlength="50"
|
||||
/>
|
||||
<view class="char-count">{{title.length}}/50</view>
|
||||
</view>
|
||||
|
||||
<!-- 内容输入 -->
|
||||
<view class="form-item">
|
||||
<view class="form-label">内容</view>
|
||||
<textarea
|
||||
class="content-textarea"
|
||||
placeholder="请输入内容(必填)"
|
||||
value="{{content}}"
|
||||
bindinput="onContentInput"
|
||||
maxlength="500"
|
||||
auto-height
|
||||
/>
|
||||
<view class="char-count">{{content.length}}/500</view>
|
||||
</view>
|
||||
|
||||
<!-- 图片上传 -->
|
||||
<view class="form-item">
|
||||
<view class="form-label">
|
||||
<text>添加图片</text>
|
||||
<text class="form-label-tip">(最多3张)</text>
|
||||
</view>
|
||||
<view class="image-upload-section">
|
||||
<!-- 已上传的图片 -->
|
||||
<view class="image-list">
|
||||
<view class="image-item" wx:for="{{images}}" wx:key="index">
|
||||
<image class="uploaded-image" src="{{item}}" mode="aspectFill" />
|
||||
<view class="image-delete" bindtap="onDeleteImage" data-index="{{index}}">
|
||||
<text class="delete-icon">✕</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 上传按钮 -->
|
||||
<view class="image-upload-btn" wx:if="{{images.length < 3}}" bindtap="onChooseImage">
|
||||
<text class="upload-icon">📷</text>
|
||||
<text class="upload-text">添加图片</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 提示信息 -->
|
||||
<view class="tips">
|
||||
<text class="tips-icon">💡</text>
|
||||
<text class="tips-text">请文明发言,共同维护良好的交流环境</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 发布按钮 -->
|
||||
<view class="action-section">
|
||||
<button class="publish-btn" bindtap="onPublish">发布</button>
|
||||
</view>
|
||||
</view>
|
||||
187
pages/post/post.wxss
Normal file
187
pages/post/post.wxss
Normal file
@@ -0,0 +1,187 @@
|
||||
/* pages/post/post.wxss */
|
||||
.container {
|
||||
min-height: 100vh;
|
||||
background-color: #F5F5F5;
|
||||
padding: 30rpx;
|
||||
}
|
||||
|
||||
.form-section {
|
||||
background-color: #ffffff;
|
||||
border-radius: 16rpx;
|
||||
padding: 30rpx;
|
||||
margin-bottom: 30rpx;
|
||||
}
|
||||
|
||||
.form-item {
|
||||
margin-bottom: 40rpx;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.form-item:last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.form-label {
|
||||
font-size: 28rpx;
|
||||
font-weight: bold;
|
||||
color: #333333;
|
||||
margin-bottom: 20rpx;
|
||||
}
|
||||
|
||||
.picker-value {
|
||||
padding: 25rpx;
|
||||
background-color: #F5F5F5;
|
||||
border-radius: 12rpx;
|
||||
font-size: 28rpx;
|
||||
color: #333333;
|
||||
}
|
||||
|
||||
.title-input {
|
||||
width: 100%;
|
||||
padding: 25rpx;
|
||||
background-color: #F5F5F5;
|
||||
border-radius: 12rpx;
|
||||
font-size: 28rpx;
|
||||
}
|
||||
|
||||
.content-textarea {
|
||||
width: 100%;
|
||||
min-height: 300rpx;
|
||||
padding: 25rpx;
|
||||
background-color: #F5F5F5;
|
||||
border-radius: 12rpx;
|
||||
font-size: 28rpx;
|
||||
line-height: 1.8;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.char-count {
|
||||
position: absolute;
|
||||
right: 0;
|
||||
bottom: -35rpx;
|
||||
font-size: 24rpx;
|
||||
color: #999999;
|
||||
}
|
||||
|
||||
/* 图片上传 */
|
||||
.form-label-tip {
|
||||
font-size: 24rpx;
|
||||
color: #999999;
|
||||
font-weight: normal;
|
||||
margin-left: 10rpx;
|
||||
}
|
||||
|
||||
.image-upload-section {
|
||||
margin-top: 20rpx;
|
||||
}
|
||||
|
||||
.image-list {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 20rpx;
|
||||
}
|
||||
|
||||
.image-item {
|
||||
position: relative;
|
||||
width: 200rpx;
|
||||
height: 200rpx;
|
||||
border-radius: 12rpx;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.uploaded-image {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.image-delete {
|
||||
position: absolute;
|
||||
top: -10rpx;
|
||||
right: -10rpx;
|
||||
width: 48rpx;
|
||||
height: 48rpx;
|
||||
background: linear-gradient(135deg, #FF6B6B, #E74C3C);
|
||||
border-radius: 50%;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
box-shadow: 0 4rpx 12rpx rgba(255, 107, 107, 0.4);
|
||||
}
|
||||
|
||||
.delete-icon {
|
||||
color: #ffffff;
|
||||
font-size: 28rpx;
|
||||
font-weight: bold;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.image-upload-btn {
|
||||
width: 200rpx;
|
||||
height: 200rpx;
|
||||
border: 2rpx dashed #D0D0D0;
|
||||
border-radius: 12rpx;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 10rpx;
|
||||
background: linear-gradient(135deg, #F8F9FA, #E9ECEF);
|
||||
transition: all 0.3s ease;
|
||||
}
|
||||
|
||||
.image-upload-btn:active {
|
||||
background: linear-gradient(135deg, #E9ECEF, #DEE2E6);
|
||||
transform: scale(0.95);
|
||||
}
|
||||
|
||||
.upload-icon {
|
||||
font-size: 60rpx;
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
.upload-text {
|
||||
font-size: 24rpx;
|
||||
color: #999999;
|
||||
}
|
||||
|
||||
.tips {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10rpx;
|
||||
padding: 20rpx;
|
||||
background-color: #FFF9E6;
|
||||
border-radius: 12rpx;
|
||||
margin-top: 40rpx;
|
||||
}
|
||||
|
||||
.tips-icon {
|
||||
font-size: 32rpx;
|
||||
}
|
||||
|
||||
.tips-text {
|
||||
flex: 1;
|
||||
font-size: 24rpx;
|
||||
color: #856404;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
/* 操作区 */
|
||||
.action-section {
|
||||
padding: 0 30rpx;
|
||||
}
|
||||
|
||||
.publish-btn {
|
||||
width: 100%;
|
||||
height: 90rpx;
|
||||
line-height: 90rpx;
|
||||
background: linear-gradient(135deg, #50C878, #3CB371);
|
||||
color: #ffffff;
|
||||
border-radius: 45rpx;
|
||||
font-size: 32rpx;
|
||||
font-weight: bold;
|
||||
box-shadow: 0 8rpx 24rpx rgba(80, 200, 120, 0.3);
|
||||
}
|
||||
|
||||
.publish-btn:active {
|
||||
opacity: 0.8;
|
||||
}
|
||||
Reference in New Issue
Block a user