8 changed files with 0 additions and 1441 deletions
@ -1,263 +0,0 @@ |
|||
const API = require('../../utils/api.js'); |
|||
|
|||
Page({ |
|||
data: { |
|||
content: '', |
|||
images: [], |
|||
selectedTopic: null, |
|||
showTopicModal: false, |
|||
hotTopics: [] |
|||
}, |
|||
|
|||
onLoad() { |
|||
this.loadHotTopics(); |
|||
}, |
|||
|
|||
loadHotTopics() { |
|||
API.getHotTopics().then(res => { |
|||
if (res.data && res.data.length > 0) { |
|||
this.setData({ |
|||
hotTopics: res.data |
|||
}); |
|||
} else { |
|||
// 使用默认热门话题
|
|||
this.setData({ |
|||
hotTopics: [ |
|||
{ id: 1, name: '今天你吃蛋了么?', count: 123 }, |
|||
{ id: 2, name: '日常分享', count: 456 }, |
|||
{ id: 3, name: '鸡蛋行情', count: 789 }, |
|||
{ id: 4, name: '养殖经验', count: 321 }, |
|||
{ id: 5, name: '美食分享', count: 654 } |
|||
] |
|||
}); |
|||
} |
|||
}).catch(err => { |
|||
console.error('加载热门话题失败:', err); |
|||
// 出错时使用默认热门话题
|
|||
this.setData({ |
|||
hotTopics: [ |
|||
{ id: 1, name: '今天你吃蛋了么?', count: 123 }, |
|||
{ id: 2, name: '日常分享', count: 456 }, |
|||
{ id: 3, name: '鸡蛋行情', count: 789 }, |
|||
{ id: 4, name: '养殖经验', count: 321 }, |
|||
{ id: 5, name: '美食分享', count: 654 } |
|||
] |
|||
}); |
|||
}); |
|||
}, |
|||
|
|||
onContentChange(e) { |
|||
this.setData({ |
|||
content: e.detail.value |
|||
}); |
|||
}, |
|||
|
|||
chooseImage() { |
|||
if (this.data.images.length >= 5) { |
|||
wx.showToast({ |
|||
title: '最多只能上传5张图片', |
|||
icon: 'none' |
|||
}); |
|||
return; |
|||
} |
|||
|
|||
wx.chooseImage({ |
|||
count: 5 - this.data.images.length, |
|||
sizeType: ['compressed'], |
|||
sourceType: ['album', 'camera'], |
|||
success: (res) => { |
|||
this.setData({ |
|||
images: [...this.data.images, ...res.tempFilePaths] |
|||
}); |
|||
} |
|||
}); |
|||
}, |
|||
|
|||
deleteImage(e) { |
|||
const index = e.currentTarget.dataset.index; |
|||
const images = this.data.images.filter((_, i) => i !== index); |
|||
this.setData({ |
|||
images |
|||
}); |
|||
}, |
|||
|
|||
showTopicPicker() { |
|||
this.setData({ |
|||
showTopicModal: true |
|||
}); |
|||
}, |
|||
|
|||
hideTopicPicker() { |
|||
this.setData({ |
|||
showTopicModal: false |
|||
}); |
|||
}, |
|||
|
|||
selectTopic(e) { |
|||
const topic = e.currentTarget.dataset.topic; |
|||
this.setData({ |
|||
selectedTopic: topic, |
|||
showTopicModal: false |
|||
}); |
|||
}, |
|||
|
|||
stopPropagation() { |
|||
// 阻止事件冒泡
|
|||
}, |
|||
|
|||
cancel() { |
|||
wx.navigateBack(); |
|||
}, |
|||
|
|||
submit() { |
|||
console.log('点击了发布按钮,当前数据:', { |
|||
content: this.data.content, |
|||
images: this.data.images, |
|||
selectedTopic: this.data.selectedTopic |
|||
}); |
|||
|
|||
if (!this.data.content.trim() && !this.data.selectedTopic) { |
|||
console.log('验证失败:文本内容和话题都为空'); |
|||
return; |
|||
} |
|||
|
|||
console.log('验证通过,开始发布'); |
|||
|
|||
wx.showLoading({ |
|||
title: '发布中...' |
|||
}); |
|||
|
|||
// 先上传图片,获取永久 URL
|
|||
this.uploadImages(this.data.images) |
|||
.then(uploadedImages => { |
|||
console.log('图片上传完成,上传的图片数量:', uploadedImages.length); |
|||
|
|||
// 获取用户ID - 从多个来源获取,确保可靠性
|
|||
let userId = wx.getStorageSync('userId'); |
|||
|
|||
// 如果没有userId,尝试从userInfo中获取
|
|||
if (!userId) { |
|||
const userInfo = wx.getStorageSync('userInfo'); |
|||
userId = userInfo?.userId || userInfo?.id; |
|||
} |
|||
|
|||
// 再次检查userId
|
|||
if (!userId) { |
|||
console.error('获取userId失败,本地存储中没有userId'); |
|||
throw new Error('用户未登录,请重新登录'); |
|||
} |
|||
|
|||
console.log('获取到的userId:', userId); |
|||
|
|||
// 获取用户信息
|
|||
let phoneNumber = null; |
|||
let avatarUrl = null; |
|||
const userInfo = wx.getStorageSync('userInfo'); |
|||
|
|||
if (userInfo) { |
|||
if (userInfo?.phoneNumber) { |
|||
phoneNumber = userInfo.phoneNumber; |
|||
} |
|||
if (userInfo?.avatarUrl) { |
|||
avatarUrl = userInfo.avatarUrl; |
|||
} |
|||
} else { |
|||
phoneNumber = wx.getStorageSync('phoneNumber'); |
|||
avatarUrl = wx.getStorageSync('avatarUrl'); |
|||
} |
|||
|
|||
console.log('获取到的用户电话号码:', phoneNumber); |
|||
console.log('获取到的用户头像URL:', avatarUrl); |
|||
|
|||
const postData = { |
|||
user_id: userId, |
|||
phone: phoneNumber, |
|||
avatar_url: avatarUrl, |
|||
content: this.data.content, |
|||
images: uploadedImages, |
|||
topic: this.data.selectedTopic?.name || this.data.selectedTopic |
|||
}; |
|||
console.log('准备发送的发布数据:', postData); |
|||
|
|||
return API.createPost(postData); |
|||
}) |
|||
.then(res => { |
|||
console.log('发布成功,服务器返回:', res); |
|||
wx.hideLoading(); |
|||
wx.showToast({ |
|||
title: '发布成功', |
|||
icon: 'success' |
|||
}); |
|||
setTimeout(() => { |
|||
wx.navigateBack(); |
|||
}, 1000); |
|||
}) |
|||
.catch(err => { |
|||
console.error('发布动态失败:', err); |
|||
wx.hideLoading(); |
|||
wx.showToast({ |
|||
title: err.message || '发布失败,请重试', |
|||
icon: 'none' |
|||
}); |
|||
}); |
|||
}, |
|||
|
|||
// 上传图片获取永久 URL
|
|||
uploadImages(tempImagePaths) { |
|||
return new Promise((resolve, reject) => { |
|||
if (!tempImagePaths || tempImagePaths.length === 0) { |
|||
resolve([]); |
|||
return; |
|||
} |
|||
|
|||
const uploadedImages = []; |
|||
let uploadedCount = 0; |
|||
let hasError = false; |
|||
|
|||
tempImagePaths.forEach((tempPath, index) => { |
|||
wx.uploadFile({ |
|||
url: API.BASE_URL + '/api/eggbar/upload', |
|||
filePath: tempPath, |
|||
name: 'image', |
|||
formData: { |
|||
index: index, |
|||
total: tempImagePaths.length |
|||
}, |
|||
success: (res) => { |
|||
if (res.statusCode === 200) { |
|||
try { |
|||
const data = JSON.parse(res.data); |
|||
if (data.success && data.imageUrl) { |
|||
uploadedImages.push(data.imageUrl); |
|||
} else { |
|||
console.error('上传图片失败:', data.message || '未知错误'); |
|||
hasError = true; |
|||
} |
|||
} catch (e) { |
|||
console.error('解析上传响应失败:', e); |
|||
hasError = true; |
|||
} |
|||
} else { |
|||
console.error('上传图片失败,HTTP状态码:', res.statusCode); |
|||
hasError = true; |
|||
} |
|||
}, |
|||
fail: (err) => { |
|||
console.error('上传图片失败:', err); |
|||
hasError = true; |
|||
}, |
|||
complete: () => { |
|||
uploadedCount++; |
|||
if (uploadedCount === tempImagePaths.length) { |
|||
if (hasError && uploadedImages.length === 0) { |
|||
reject(new Error('所有图片上传失败,请重试')); |
|||
} else { |
|||
resolve(uploadedImages); |
|||
} |
|||
} |
|||
} |
|||
}); |
|||
}); |
|||
}); |
|||
} |
|||
}); |
|||
@ -1,9 +0,0 @@ |
|||
{ |
|||
"usingComponents": {}, |
|||
"enablePullDownRefresh": false, |
|||
"backgroundTextStyle": "dark", |
|||
"backgroundColor": "#f8f8f8", |
|||
"navigationBarBackgroundColor": "#ffffff", |
|||
"navigationBarTitleText": "发布动态", |
|||
"navigationBarTextStyle": "black" |
|||
} |
|||
@ -1,68 +0,0 @@ |
|||
<view> |
|||
<view class="header"> |
|||
<text class="title">发布动态</text> |
|||
</view> |
|||
|
|||
<view class="form"> |
|||
<view class="form-item"> |
|||
<textarea |
|||
class="textarea" |
|||
placeholder="分享你的想法..." |
|||
placeholder-class="placeholder" |
|||
value="{{content}}" |
|||
bindinput="onContentChange" |
|||
maxlength="500" |
|||
></textarea> |
|||
<text class="counter">{{content.length}}/500</text> |
|||
</view> |
|||
|
|||
<view class="form-item"> |
|||
<view class="label">添加图片</view> |
|||
<view class="image-uploader"> |
|||
<view class="upload-area" bindtap="chooseImage"> |
|||
<text class="upload-icon">+</text> |
|||
<text class="upload-text">选择图片</text> |
|||
</view> |
|||
<view class="image-list"> |
|||
<view class="image-item" wx:for="{{images}}" wx:key="index"> |
|||
<image class="image" src="{{item}}" mode="aspectFill"></image> |
|||
<view class="image-delete" bindtap="deleteImage" data-index="{{index}}"> |
|||
<text>×</text> |
|||
</view> |
|||
</view> |
|||
</view> |
|||
</view> |
|||
</view> |
|||
|
|||
<view class="form-item"> |
|||
<view class="label">选择话题</view> |
|||
<view class="topic-picker" bindtap="showTopicPicker"> |
|||
<text class="topic-text">{{selectedTopic ? '#' + selectedTopic : '选择话题'}}</text> |
|||
<text class="topic-arrow">▼</text> |
|||
</view> |
|||
</view> |
|||
</view> |
|||
|
|||
<view class="footer"> |
|||
<button class="cancel-btn" bindtap="cancel">取消</button> |
|||
<button class="submit-btn" bindtap="submit" disabled="{{!content.trim() && !selectedTopic}}"> |
|||
发布 |
|||
</button> |
|||
</view> |
|||
|
|||
<!-- 话题选择器弹窗 --> |
|||
<view wx:if="{{showTopicModal}}" class="modal-overlay" bindtap="hideTopicPicker"> |
|||
<view class="modal-container" catchtap="stopPropagation"> |
|||
<view class="modal-header"> |
|||
<text class="modal-title">选择话题</text> |
|||
<text class="modal-close" bindtap="hideTopicPicker">×</text> |
|||
</view> |
|||
<view class="modal-content"> |
|||
<view class="topic-item" wx:for="{{hotTopics}}" wx:key="id" bindtap="selectTopic" data-topic="{{item.name}}"> |
|||
<text class="topic-name">#{{item.name}}</text> |
|||
<text class="topic-count">{{item.count}}人讨论</text> |
|||
</view> |
|||
</view> |
|||
</view> |
|||
</view> |
|||
</view> |
|||
@ -1,281 +0,0 @@ |
|||
page { |
|||
background-color: #f5f5f5; |
|||
height: 100vh; |
|||
} |
|||
|
|||
.container { |
|||
min-height: 100vh; |
|||
padding: 20rpx; |
|||
} |
|||
|
|||
.header { |
|||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); |
|||
border-radius: 20rpx; |
|||
padding: 30rpx; |
|||
margin-bottom: 20rpx; |
|||
box-shadow: 0 4rpx 20rpx rgba(102, 126, 234, 0.3); |
|||
} |
|||
|
|||
.title { |
|||
font-size: 36rpx; |
|||
font-weight: bold; |
|||
color: #ffffff; |
|||
} |
|||
|
|||
.form { |
|||
background: #ffffff; |
|||
border-radius: 20rpx; |
|||
padding: 30rpx; |
|||
margin-bottom: 20rpx; |
|||
box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08); |
|||
} |
|||
|
|||
.form-item { |
|||
margin-bottom: 30rpx; |
|||
} |
|||
|
|||
.label { |
|||
font-size: 28rpx; |
|||
font-weight: 600; |
|||
color: #333333; |
|||
margin-bottom: 15rpx; |
|||
} |
|||
|
|||
.textarea { |
|||
width: 100%; |
|||
min-height: 200rpx; |
|||
font-size: 28rpx; |
|||
color: #333333; |
|||
border: 2rpx solid #f0f0f0; |
|||
border-radius: 12rpx; |
|||
padding: 20rpx; |
|||
box-sizing: border-box; |
|||
resize: none; |
|||
line-height: 1.6; |
|||
} |
|||
|
|||
.placeholder { |
|||
color: #999999; |
|||
} |
|||
|
|||
.counter { |
|||
display: block; |
|||
text-align: right; |
|||
font-size: 24rpx; |
|||
color: #999999; |
|||
margin-top: 10rpx; |
|||
} |
|||
|
|||
.image-uploader { |
|||
margin-top: 10rpx; |
|||
} |
|||
|
|||
.upload-area { |
|||
width: 120rpx; |
|||
height: 120rpx; |
|||
border: 2rpx dashed #e0e0e0; |
|||
border-radius: 12rpx; |
|||
display: flex; |
|||
flex-direction: column; |
|||
align-items: center; |
|||
justify-content: center; |
|||
background: #fafafa; |
|||
margin-bottom: 15rpx; |
|||
transition: all 0.3s ease; |
|||
} |
|||
|
|||
.upload-area:active { |
|||
background: #f0f0f0; |
|||
transform: scale(0.98); |
|||
} |
|||
|
|||
.upload-icon { |
|||
font-size: 48rpx; |
|||
color: #999999; |
|||
margin-bottom: 10rpx; |
|||
} |
|||
|
|||
.upload-text { |
|||
font-size: 24rpx; |
|||
color: #999999; |
|||
} |
|||
|
|||
.image-list { |
|||
display: flex; |
|||
gap: 15rpx; |
|||
flex-wrap: wrap; |
|||
margin-top: 15rpx; |
|||
} |
|||
|
|||
.image-item { |
|||
position: relative; |
|||
width: 150rpx; |
|||
height: 150rpx; |
|||
border-radius: 12rpx; |
|||
overflow: hidden; |
|||
} |
|||
|
|||
.image { |
|||
width: 100%; |
|||
height: 100%; |
|||
background: #f0f0f0; |
|||
} |
|||
|
|||
.image-delete { |
|||
position: absolute; |
|||
top: 5rpx; |
|||
right: 5rpx; |
|||
width: 40rpx; |
|||
height: 40rpx; |
|||
background: rgba(0, 0, 0, 0.6); |
|||
border-radius: 50%; |
|||
display: flex; |
|||
align-items: center; |
|||
justify-content: center; |
|||
color: #ffffff; |
|||
font-size: 32rpx; |
|||
font-weight: bold; |
|||
transition: all 0.3s ease; |
|||
} |
|||
|
|||
.image-delete:active { |
|||
background: rgba(0, 0, 0, 0.8); |
|||
transform: scale(0.9); |
|||
} |
|||
|
|||
.topic-picker { |
|||
display: flex; |
|||
justify-content: space-between; |
|||
align-items: center; |
|||
padding: 20rpx; |
|||
border: 2rpx solid #f0f0f0; |
|||
border-radius: 12rpx; |
|||
background: #fafafa; |
|||
transition: all 0.3s ease; |
|||
} |
|||
|
|||
.topic-picker:active { |
|||
background: #f0f0f0; |
|||
} |
|||
|
|||
.topic-text { |
|||
font-size: 28rpx; |
|||
color: #333333; |
|||
} |
|||
|
|||
.topic-arrow { |
|||
font-size: 20rpx; |
|||
color: #999999; |
|||
} |
|||
|
|||
.footer { |
|||
position: fixed; |
|||
bottom: 0; |
|||
left: 0; |
|||
right: 0; |
|||
display: flex; |
|||
padding: 20rpx; |
|||
background: #ffffff; |
|||
border-top: 1rpx solid #f0f0f0; |
|||
box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05); |
|||
box-sizing: border-box; |
|||
} |
|||
|
|||
.cancel-btn { |
|||
flex: 1; |
|||
height: 80rpx; |
|||
margin-right: 15rpx; |
|||
background: #f5f5f5; |
|||
color: #333333; |
|||
border: none; |
|||
border-radius: 40rpx; |
|||
font-size: 28rpx; |
|||
font-weight: 600; |
|||
} |
|||
|
|||
.submit-btn { |
|||
flex: 1; |
|||
height: 80rpx; |
|||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); |
|||
color: #ffffff; |
|||
border: none; |
|||
border-radius: 40rpx; |
|||
font-size: 28rpx; |
|||
font-weight: 600; |
|||
} |
|||
|
|||
.submit-btn:disabled { |
|||
background: #e0e0e0; |
|||
color: #999999; |
|||
} |
|||
|
|||
.modal-overlay { |
|||
position: fixed; |
|||
top: 0; |
|||
left: 0; |
|||
right: 0; |
|||
bottom: 0; |
|||
background: rgba(0, 0, 0, 0.5); |
|||
display: flex; |
|||
align-items: center; |
|||
justify-content: center; |
|||
z-index: 9999; |
|||
} |
|||
|
|||
.modal-container { |
|||
width: 80%; |
|||
max-height: 70vh; |
|||
background: #ffffff; |
|||
border-radius: 20rpx; |
|||
overflow: hidden; |
|||
} |
|||
|
|||
.modal-header { |
|||
display: flex; |
|||
justify-content: space-between; |
|||
align-items: center; |
|||
padding: 20rpx; |
|||
border-bottom: 1rpx solid #f0f0f0; |
|||
} |
|||
|
|||
.modal-title { |
|||
font-size: 32rpx; |
|||
font-weight: bold; |
|||
color: #333333; |
|||
} |
|||
|
|||
.modal-close { |
|||
font-size: 36rpx; |
|||
color: #999999; |
|||
padding: 10rpx; |
|||
} |
|||
|
|||
.modal-content { |
|||
padding: 20rpx; |
|||
max-height: 50vh; |
|||
overflow-y: auto; |
|||
} |
|||
|
|||
.topic-item { |
|||
padding: 20rpx; |
|||
border-bottom: 1rpx solid #f0f0f0; |
|||
display: flex; |
|||
justify-content: space-between; |
|||
align-items: center; |
|||
transition: all 0.3s ease; |
|||
} |
|||
|
|||
.topic-item:active { |
|||
background: #f0f7ff; |
|||
} |
|||
|
|||
.topic-name { |
|||
font-size: 28rpx; |
|||
color: #333333; |
|||
font-weight: 500; |
|||
} |
|||
|
|||
.topic-count { |
|||
font-size: 24rpx; |
|||
color: #999999; |
|||
} |
|||
@ -1,442 +0,0 @@ |
|||
const API = require('../../utils/api.js'); |
|||
|
|||
Page({ |
|||
data: { |
|||
hotTopics: [], |
|||
posts: [], |
|||
loading: false, |
|||
hasMore: true, |
|||
page: 1, |
|||
pageSize: 10 |
|||
}, |
|||
|
|||
onLoad() { |
|||
this.loadHotTopics(); |
|||
this.loadPosts(); |
|||
}, |
|||
|
|||
onShow() { |
|||
this.setData({ |
|||
page: 1, |
|||
hasMore: true, |
|||
posts: [] |
|||
}); |
|||
this.loadPosts(); |
|||
}, |
|||
|
|||
onReachBottom() { |
|||
if (this.data.hasMore && !this.data.loading) { |
|||
this.loadPosts(); |
|||
} |
|||
}, |
|||
|
|||
onPullDownRefresh() { |
|||
this.setData({ |
|||
page: 1, |
|||
hasMore: true, |
|||
posts: [] |
|||
}); |
|||
this.loadHotTopics(); |
|||
this.loadPosts(); |
|||
wx.stopPullDownRefresh(); |
|||
}, |
|||
|
|||
loadHotTopics() { |
|||
API.getHotTopics().then(res => { |
|||
if (res.data && res.data.length > 0) { |
|||
this.setData({ |
|||
hotTopics: res.data |
|||
}); |
|||
} else { |
|||
// 使用默认热门话题
|
|||
this.setData({ |
|||
hotTopics: [ |
|||
{ id: 1, name: '今天你吃蛋了么?', count: 123 }, |
|||
{ id: 2, name: '日常分享', count: 456 }, |
|||
{ id: 3, name: '鸡蛋行情', count: 789 }, |
|||
{ id: 4, name: '养殖经验', count: 321 }, |
|||
{ id: 5, name: '美食分享', count: 654 } |
|||
] |
|||
}); |
|||
} |
|||
}).catch(err => { |
|||
console.error('加载热门话题失败:', err); |
|||
// 出错时使用默认热门话题
|
|||
this.setData({ |
|||
hotTopics: [ |
|||
{ id: 1, name: '今天你吃蛋了么?', count: 123 }, |
|||
{ id: 2, name: '日常分享', count: 456 }, |
|||
{ id: 3, name: '鸡蛋行情', count: 789 }, |
|||
{ id: 4, name: '养殖经验', count: 321 }, |
|||
{ id: 5, name: '美食分享', count: 654 } |
|||
] |
|||
}); |
|||
}); |
|||
}, |
|||
|
|||
loadPosts() { |
|||
if (this.data.loading) { |
|||
return; |
|||
} |
|||
|
|||
this.setData({ |
|||
loading: true |
|||
}); |
|||
|
|||
// 获取用户信息
|
|||
const userInfo = wx.getStorageSync('userInfo'); |
|||
const phoneNumber = userInfo?.phoneNumber || wx.getStorageSync('phoneNumber'); |
|||
const userId = wx.getStorageSync('userId') || userInfo?.userId; |
|||
|
|||
// 构建请求参数
|
|||
const params = { |
|||
page: this.data.page, |
|||
pageSize: this.data.pageSize |
|||
}; |
|||
|
|||
// 只有当电话号码存在时才传递,避免传递空值
|
|||
if (phoneNumber) { |
|||
params.phone = phoneNumber; |
|||
} |
|||
|
|||
// 传递userId参数,用于后端过滤动态
|
|||
if (userId) { |
|||
params.userId = userId; |
|||
} |
|||
|
|||
API.getPosts(params).then(res => { |
|||
console.log('后端返回的完整响应:', res); |
|||
// 正确处理后端返回的响应格式
|
|||
let newPosts = res.data && res.data.posts ? res.data.posts : []; |
|||
console.log('后端返回的动态数量:', newPosts.length); |
|||
console.log('后端返回的动态数据:', newPosts); |
|||
|
|||
// 处理images字段,确保它是一个数组,并添加username字段
|
|||
newPosts = newPosts.map(post => { |
|||
if (post.images) { |
|||
// 如果images是字符串,尝试解析为JSON数组
|
|||
if (typeof post.images === 'string') { |
|||
try { |
|||
post.images = JSON.parse(post.images); |
|||
// 确保解析后是数组
|
|||
if (!Array.isArray(post.images)) { |
|||
post.images = []; |
|||
} |
|||
} catch (e) { |
|||
// 解析失败,设置为空数组
|
|||
post.images = []; |
|||
} |
|||
} else if (!Array.isArray(post.images)) { |
|||
// 如果不是字符串也不是数组,设置为空数组
|
|||
post.images = []; |
|||
} |
|||
} else { |
|||
// 如果images不存在,设置为空数组
|
|||
post.images = []; |
|||
} |
|||
|
|||
// 添加username字段(使用phone的前几位作为用户名)
|
|||
if (!post.username) { |
|||
if (post.phone) { |
|||
// 隐藏手机号中间部分
|
|||
post.username = post.phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2'); |
|||
} else { |
|||
post.username = '用户' + post.user_id?.substring(0, 6) || '未知用户'; |
|||
} |
|||
} |
|||
|
|||
// 添加time字段用于显示时间
|
|||
if (!post.time && post.created_at) { |
|||
const date = new Date(post.created_at); |
|||
post.time = date.toLocaleString('zh-CN', { |
|||
year: 'numeric', |
|||
month: '2-digit', |
|||
day: '2-digit', |
|||
hour: '2-digit', |
|||
minute: '2-digit' |
|||
}); |
|||
} |
|||
|
|||
// 确保topic字段存在
|
|||
if (post.topic) { |
|||
console.log('动态话题:', post.topic); |
|||
} |
|||
|
|||
// 为了兼容前端模板,添加user对象
|
|||
if (!post.user) { |
|||
post.user = { |
|||
avatar: post.avatar_url || '', |
|||
nickname: post.username |
|||
}; |
|||
} |
|||
|
|||
// 为了兼容前端模板,添加image字段(使用images数组的第一个元素)
|
|||
if (!post.image && post.images && post.images.length > 0) { |
|||
post.image = post.images[0]; |
|||
} |
|||
|
|||
// 为了兼容前端模板,添加like_count和comment_count字段
|
|||
if (post.likes !== undefined && post.like_count === undefined) { |
|||
post.like_count = post.likes; |
|||
} |
|||
if (post.comments !== undefined && post.comment_count === undefined) { |
|||
post.comment_count = post.comments; |
|||
} |
|||
|
|||
return post; |
|||
}); |
|||
|
|||
// 根据status字段过滤动态:0仅自己可见,1所有人可见,2仅自己可见
|
|||
newPosts = newPosts.filter(post => { |
|||
// 如果status为1,所有人可见
|
|||
if (post.status === 1) { |
|||
return true; |
|||
} |
|||
// 如果status为0或2,仅自己可见
|
|||
if (post.status === 0 || post.status === 2) { |
|||
// 检查是否是当前用户的动态
|
|||
return post.user_id === userId; |
|||
} |
|||
// 默认显示
|
|||
return true; |
|||
}); |
|||
|
|||
// 如果是第一页且没有数据,使用默认动态数据
|
|||
if (this.data.page === 1 && (!newPosts || newPosts.length === 0)) { |
|||
newPosts = [ |
|||
{ |
|||
id: 1, |
|||
user_id: '1', |
|||
content: '今天的鸡蛋质量真好,客户都很满意!', |
|||
images: [], |
|||
likes: 12, |
|||
comments: 3, |
|||
created_at: new Date().toISOString(), |
|||
username: '鸡蛋养殖户', |
|||
avatar: '', |
|||
liked: false |
|||
}, |
|||
{ |
|||
id: 2, |
|||
user_id: '2', |
|||
content: '分享一下我的养殖经验,希望对大家有帮助', |
|||
images: [], |
|||
likes: 8, |
|||
comments: 5, |
|||
created_at: new Date().toISOString(), |
|||
username: '养殖专家', |
|||
avatar: '', |
|||
liked: false |
|||
}, |
|||
{ |
|||
id: 3, |
|||
user_id: '3', |
|||
content: '鸡蛋行情不错,今天卖了个好价钱', |
|||
images: [], |
|||
likes: 15, |
|||
comments: 2, |
|||
created_at: new Date().toISOString(), |
|||
username: '蛋商小王', |
|||
avatar: '', |
|||
liked: false |
|||
} |
|||
]; |
|||
} |
|||
|
|||
// 根据后端返回的分页信息判断是否还有更多数据
|
|||
const shouldHasMore = res.data && res.data.pagination ? |
|||
this.data.page < res.data.pagination.totalPages : |
|||
this.data.page < 3; |
|||
|
|||
this.setData({ |
|||
posts: this.data.page === 1 ? newPosts : [...this.data.posts, ...newPosts], |
|||
loading: false, |
|||
hasMore: shouldHasMore, |
|||
page: this.data.page + 1 |
|||
}); |
|||
}).catch(err => { |
|||
console.error('加载动态失败:', err); |
|||
|
|||
// 出错时使用默认动态数据
|
|||
if (this.data.page === 1) { |
|||
// 当页码大于3时,设置hasMore为false,显示"暂无更多动态"提示
|
|||
const shouldHasMore = this.data.page < 3; |
|||
|
|||
this.setData({ |
|||
posts: [ |
|||
{ |
|||
id: 1, |
|||
user_id: '1', |
|||
content: '今天的鸡蛋质量真好,客户都很满意!', |
|||
images: [], |
|||
likes: 12, |
|||
comments: 3, |
|||
created_at: new Date().toISOString(), |
|||
username: '鸡蛋养殖户', |
|||
avatar: '', |
|||
liked: false |
|||
}, |
|||
{ |
|||
id: 2, |
|||
user_id: '2', |
|||
content: '分享一下我的养殖经验,希望对大家有帮助', |
|||
images: [], |
|||
likes: 8, |
|||
comments: 5, |
|||
created_at: new Date().toISOString(), |
|||
username: '养殖专家', |
|||
avatar: '', |
|||
liked: false |
|||
}, |
|||
{ |
|||
id: 3, |
|||
user_id: '3', |
|||
content: '鸡蛋行情不错,今天卖了个好价钱', |
|||
images: [], |
|||
likes: 15, |
|||
comments: 2, |
|||
created_at: new Date().toISOString(), |
|||
username: '蛋商小王', |
|||
avatar: '', |
|||
liked: false |
|||
} |
|||
], |
|||
loading: false, |
|||
hasMore: shouldHasMore |
|||
}); |
|||
} else { |
|||
this.setData({ |
|||
loading: false |
|||
}); |
|||
} |
|||
}); |
|||
}, |
|||
|
|||
viewTopic(e) { |
|||
const topic = e.currentTarget.dataset.topic; |
|||
wx.navigateTo({ |
|||
url: `/pages/eggbar/topic-detail?id=${topic.id}` |
|||
}); |
|||
}, |
|||
|
|||
viewPost(e) { |
|||
const post = e.currentTarget.dataset.post; |
|||
wx.navigateTo({ |
|||
url: `/pages/eggbar/post-detail?id=${post.id}` |
|||
}); |
|||
}, |
|||
|
|||
likePost(e) { |
|||
const postId = e.currentTarget.dataset.id; |
|||
|
|||
// 获取用户电话号码
|
|||
const userInfo = wx.getStorageSync('userInfo'); |
|||
const phoneNumber = userInfo?.phoneNumber || wx.getStorageSync('phoneNumber'); |
|||
|
|||
if (!phoneNumber) { |
|||
wx.showToast({ |
|||
title: '请先登录获取电话号码', |
|||
icon: 'none' |
|||
}); |
|||
return; |
|||
} |
|||
|
|||
// 前端临时更新状态
|
|||
const posts = this.data.posts.map(post => { |
|||
if (post.id === postId) { |
|||
return { |
|||
...post, |
|||
liked: !post.liked, |
|||
likes: post.liked ? post.likes - 1 : post.likes + 1 |
|||
}; |
|||
} |
|||
return post; |
|||
}); |
|||
|
|||
this.setData({ posts }); |
|||
|
|||
// 调用API
|
|||
API.likePost(postId, phoneNumber).then(res => { |
|||
console.log('点赞成功:', res); |
|||
wx.showToast({ |
|||
title: res.message || '操作成功', |
|||
icon: 'success' |
|||
}); |
|||
}).catch(err => { |
|||
console.error('点赞失败:', err); |
|||
|
|||
// 恢复原始状态
|
|||
const originalPosts = this.data.posts.map(post => { |
|||
if (post.id === postId) { |
|||
return { |
|||
...post, |
|||
liked: !post.liked, |
|||
likes: post.liked ? post.likes + 1 : post.likes - 1 |
|||
}; |
|||
} |
|||
return post; |
|||
}); |
|||
|
|||
this.setData({ posts: originalPosts }); |
|||
|
|||
wx.showToast({ |
|||
title: '操作失败,请重试', |
|||
icon: 'none' |
|||
}); |
|||
}); |
|||
}, |
|||
|
|||
commentPost(e) { |
|||
const postId = e.currentTarget.dataset.id; |
|||
wx.navigateTo({ |
|||
url: `/pages/eggbar/post-detail?id=${postId}&focusComment=true` |
|||
}); |
|||
}, |
|||
|
|||
sharePost(e) { |
|||
const postId = e.currentTarget.dataset.id; |
|||
wx.showShareMenu({ |
|||
withShareTicket: true, |
|||
success: () => { |
|||
console.log('分享成功'); |
|||
} |
|||
}); |
|||
}, |
|||
|
|||
// 预览图片
|
|||
previewImage(e) { |
|||
const images = e.currentTarget.dataset.images; |
|||
const currentIndex = parseInt(e.currentTarget.dataset.current); |
|||
|
|||
console.log('预览图片:', { |
|||
images, |
|||
currentIndex, |
|||
current: images[currentIndex] |
|||
}); |
|||
|
|||
wx.previewImage({ |
|||
current: images[currentIndex], |
|||
urls: images, |
|||
success: function(res) { |
|||
console.log('预览图片成功:', res); |
|||
}, |
|||
fail: function(err) { |
|||
console.error('预览图片失败:', err); |
|||
} |
|||
}); |
|||
}, |
|||
|
|||
createPost() { |
|||
wx.navigateTo({ |
|||
url: '/pages/eggbar/create-post' |
|||
}); |
|||
}, |
|||
|
|||
onShareAppMessage() { |
|||
return { |
|||
title: '蛋吧 - 鸡蛋行业交流社区', |
|||
path: '/pages/eggbar/eggbar', |
|||
imageUrl: '/images/eggbar-share.jpg' |
|||
}; |
|||
} |
|||
}); |
|||
@ -1,12 +0,0 @@ |
|||
{ |
|||
"usingComponents": { |
|||
"navigation-bar": "/components/navigation-bar/navigation-bar" |
|||
}, |
|||
"enablePullDownRefresh": false, |
|||
"backgroundTextStyle": "dark", |
|||
"backgroundColor": "#f8f8f8", |
|||
"navigationBarBackgroundColor": "#ffffff", |
|||
"navigationBarTitleText": "蛋吧", |
|||
"navigationBarTextStyle": "black", |
|||
"navigationStyle": "custom" |
|||
} |
|||
@ -1,82 +0,0 @@ |
|||
<view> |
|||
|
|||
|
|||
<view class="content"> |
|||
<view class="header"> |
|||
<text class="title">蛋吧</text> |
|||
<text class="subtitle">欢迎来到蛋吧社区</text> |
|||
</view> |
|||
|
|||
<view class="section"> |
|||
<view class="section-title"> |
|||
<text class="title-text">热门话题</text> |
|||
</view> |
|||
<view class="topic-list"> |
|||
<view class="topic-item" wx:for="{{hotTopics}}" wx:key="id" bindtap="viewTopic" data-topic="{{item}}"> |
|||
<view class="topic-rank">{{index + 1}}</view> |
|||
<view class="topic-info"> |
|||
<text class="topic-name">{{item.name}}</text> |
|||
<text class="topic-count">{{item.count}}人讨论</text> |
|||
</view> |
|||
</view> |
|||
</view> |
|||
</view> |
|||
|
|||
<view class="section"> |
|||
<view class="section-title"> |
|||
<text class="title-text">最新动态</text> |
|||
</view> |
|||
<view class="post-list"> |
|||
<view class="post-item" wx:for="{{posts}}" wx:key="id" bindtap="viewPost" data-post="{{item}}"> |
|||
<view class="post-header"> |
|||
<image class="post-avatar" src="{{item.avatar_url}}" mode="aspectFill"></image> |
|||
<view class="post-user"> |
|||
<text class="post-username">{{item.username}}</text> |
|||
<text class="post-time">{{item.time}}</text> |
|||
</view> |
|||
</view> |
|||
<view class="post-content"> |
|||
<text class="post-text">{{item.content}}</text> |
|||
<view class="post-images" wx:if="{{item.images && item.images.length > 0}}"> |
|||
<image class="post-image" wx:for="{{item.images}}" wx:for-item="img" wx:key="index" src="{{img}}" mode="aspectFill" bindtap="previewImage" data-images="{{item.images}}" data-current="{{index}}"></image> |
|||
</view> |
|||
<view class="post-topic" wx:if="{{item.topic}}"> |
|||
<text class="topic-text">#{{item.topic}}</text> |
|||
</view> |
|||
</view> |
|||
<view class="post-footer"> |
|||
<view class="post-action" bindtap="likePost" data-id="{{item.id}}"> |
|||
<text class="action-icon">{{item.liked ? '❤️' : '🤍'}}</text> |
|||
<text class="action-text">{{item.likes}}</text> |
|||
</view> |
|||
<view class="post-action" bindtap="commentPost" data-id="{{item.id}}"> |
|||
<text class="action-icon">💬</text> |
|||
<text class="action-text">{{item.comments}}</text> |
|||
</view> |
|||
<view class="post-action" bindtap="sharePost" data-id="{{item.id}}"> |
|||
<text class="action-icon">📤</text> |
|||
<text class="action-text">分享</text> |
|||
</view> |
|||
</view> |
|||
</view> |
|||
</view> |
|||
|
|||
<view class="loading" wx:if="{{loading}}"> |
|||
<text>加载中...</text> |
|||
</view> |
|||
|
|||
<view class="no-more" wx:if="{{!hasMore && posts.length > 0}}"> |
|||
<text>暂无更多动态</text> |
|||
</view> |
|||
|
|||
<view class="empty" wx:if="{{posts.length === 0 && !loading}}"> |
|||
<text class="empty-icon">📭</text> |
|||
<text class="empty-text">暂无动态</text> |
|||
</view> |
|||
</view> |
|||
</view> |
|||
|
|||
<view class="fab-button" bindtap="createPost"> |
|||
<text class="fab-icon">✏️</text> |
|||
</view> |
|||
</view> |
|||
@ -1,284 +0,0 @@ |
|||
page { |
|||
background-color: #f5f5f5; |
|||
height: 100vh; |
|||
} |
|||
|
|||
.container { |
|||
min-height: 100vh; |
|||
padding-bottom: 120rpx; |
|||
} |
|||
|
|||
.content { |
|||
padding: 20rpx; |
|||
} |
|||
|
|||
.header { |
|||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); |
|||
border-radius: 20rpx; |
|||
padding: 40rpx 30rpx; |
|||
margin-bottom: 20rpx; |
|||
box-shadow: 0 4rpx 20rpx rgba(102, 126, 234, 0.3); |
|||
} |
|||
|
|||
.title { |
|||
display: block; |
|||
font-size: 48rpx; |
|||
font-weight: bold; |
|||
color: #ffffff; |
|||
margin-bottom: 10rpx; |
|||
} |
|||
|
|||
.subtitle { |
|||
display: block; |
|||
font-size: 28rpx; |
|||
color: rgba(255, 255, 255, 0.9); |
|||
} |
|||
|
|||
.section { |
|||
background: #ffffff; |
|||
border-radius: 20rpx; |
|||
padding: 30rpx; |
|||
margin-bottom: 20rpx; |
|||
box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08); |
|||
} |
|||
|
|||
.section-title { |
|||
margin-bottom: 20rpx; |
|||
padding-bottom: 15rpx; |
|||
border-bottom: 2rpx solid #f0f0f0; |
|||
} |
|||
|
|||
.title-text { |
|||
font-size: 32rpx; |
|||
font-weight: bold; |
|||
color: #333333; |
|||
} |
|||
|
|||
.topic-list { |
|||
display: flex; |
|||
flex-direction: column; |
|||
gap: 15rpx; |
|||
} |
|||
|
|||
.topic-item { |
|||
display: flex; |
|||
align-items: center; |
|||
padding: 20rpx; |
|||
background: #f8f9fa; |
|||
border-radius: 12rpx; |
|||
transition: all 0.3s ease; |
|||
} |
|||
|
|||
.topic-item:active { |
|||
transform: scale(0.98); |
|||
background: #e9ecef; |
|||
} |
|||
|
|||
.topic-rank { |
|||
width: 50rpx; |
|||
height: 50rpx; |
|||
display: flex; |
|||
align-items: center; |
|||
justify-content: center; |
|||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); |
|||
border-radius: 50%; |
|||
font-size: 24rpx; |
|||
font-weight: bold; |
|||
color: #ffffff; |
|||
margin-right: 20rpx; |
|||
} |
|||
|
|||
.topic-info { |
|||
flex: 1; |
|||
display: flex; |
|||
flex-direction: column; |
|||
gap: 5rpx; |
|||
} |
|||
|
|||
.topic-name { |
|||
font-size: 28rpx; |
|||
font-weight: 600; |
|||
color: #333333; |
|||
} |
|||
|
|||
.topic-count { |
|||
font-size: 24rpx; |
|||
color: #999999; |
|||
} |
|||
|
|||
.post-list { |
|||
display: flex; |
|||
flex-direction: column; |
|||
gap: 20rpx; |
|||
} |
|||
|
|||
.post-item { |
|||
background: #ffffff; |
|||
border-radius: 16rpx; |
|||
padding: 25rpx; |
|||
box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.06); |
|||
transition: all 0.3s ease; |
|||
} |
|||
|
|||
.post-item:active { |
|||
transform: translateY(-2rpx); |
|||
box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1); |
|||
} |
|||
|
|||
.post-header { |
|||
display: flex; |
|||
align-items: center; |
|||
margin-bottom: 15rpx; |
|||
} |
|||
|
|||
.post-avatar { |
|||
width: 80rpx; |
|||
height: 80rpx; |
|||
border-radius: 50%; |
|||
margin-right: 15rpx; |
|||
background: #f0f0f0; |
|||
} |
|||
|
|||
.post-user { |
|||
flex: 1; |
|||
display: flex; |
|||
flex-direction: column; |
|||
gap: 5rpx; |
|||
} |
|||
|
|||
.post-username { |
|||
font-size: 28rpx; |
|||
font-weight: 600; |
|||
color: #333333; |
|||
} |
|||
|
|||
.post-time { |
|||
font-size: 24rpx; |
|||
color: #999999; |
|||
} |
|||
|
|||
.post-content { |
|||
margin-bottom: 15rpx; |
|||
} |
|||
|
|||
.post-text { |
|||
display: block; |
|||
font-size: 28rpx; |
|||
color: #333333; |
|||
line-height: 1.6; |
|||
margin-bottom: 15rpx; |
|||
} |
|||
|
|||
.post-images { |
|||
display: flex; |
|||
gap: 10rpx; |
|||
flex-wrap: wrap; |
|||
} |
|||
|
|||
.post-image { |
|||
width: 200rpx; |
|||
height: 200rpx; |
|||
border-radius: 12rpx; |
|||
background: #f0f0f0; |
|||
} |
|||
|
|||
.post-topic { |
|||
margin-top: 15rpx; |
|||
padding: 8rpx 16rpx; |
|||
background: #f0f8ff; |
|||
border-radius: 20rpx; |
|||
display: inline-block; |
|||
} |
|||
|
|||
.topic-text { |
|||
font-size: 24rpx; |
|||
color: #1890ff; |
|||
font-weight: 500; |
|||
} |
|||
|
|||
.post-footer { |
|||
display: flex; |
|||
justify-content: space-around; |
|||
padding-top: 15rpx; |
|||
border-top: 1rpx solid #f0f0f0; |
|||
} |
|||
|
|||
.post-action { |
|||
display: flex; |
|||
align-items: center; |
|||
gap: 8rpx; |
|||
padding: 10rpx 20rpx; |
|||
border-radius: 20rpx; |
|||
transition: all 0.3s ease; |
|||
} |
|||
|
|||
.post-action:active { |
|||
background: #f5f5f5; |
|||
} |
|||
|
|||
.action-icon { |
|||
font-size: 32rpx; |
|||
} |
|||
|
|||
.action-text { |
|||
font-size: 24rpx; |
|||
color: #666666; |
|||
} |
|||
|
|||
.loading { |
|||
text-align: center; |
|||
padding: 40rpx; |
|||
color: #999999; |
|||
font-size: 28rpx; |
|||
} |
|||
|
|||
.no-more { |
|||
text-align: center; |
|||
padding: 40rpx; |
|||
color: #999999; |
|||
font-size: 28rpx; |
|||
} |
|||
|
|||
.empty { |
|||
display: flex; |
|||
flex-direction: column; |
|||
align-items: center; |
|||
justify-content: center; |
|||
padding: 100rpx 20rpx; |
|||
} |
|||
|
|||
.empty-icon { |
|||
font-size: 120rpx; |
|||
margin-bottom: 20rpx; |
|||
opacity: 0.5; |
|||
} |
|||
|
|||
.empty-text { |
|||
font-size: 28rpx; |
|||
color: #999999; |
|||
} |
|||
|
|||
.fab-button { |
|||
position: fixed; |
|||
right: 30rpx; |
|||
bottom: 150rpx; |
|||
width: 120rpx; |
|||
height: 120rpx; |
|||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); |
|||
border-radius: 50%; |
|||
display: flex; |
|||
align-items: center; |
|||
justify-content: center; |
|||
box-shadow: 0 6rpx 20rpx rgba(102, 126, 234, 0.4); |
|||
z-index: 1000; |
|||
transition: all 0.3s ease; |
|||
} |
|||
|
|||
.fab-button:active { |
|||
transform: scale(0.95); |
|||
box-shadow: 0 4rpx 15rpx rgba(102, 126, 234, 0.3); |
|||
} |
|||
|
|||
.fab-icon { |
|||
font-size: 48rpx; |
|||
} |
|||
Loading…
Reference in new issue