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)
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user