You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
217 lines
5.2 KiB
217 lines
5.2 KiB
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 }
|
|
]
|
|
});
|
|
}
|
|
}).catch(err => {
|
|
console.error('加载热门话题失败:', err);
|
|
// 出错时使用默认热门话题
|
|
this.setData({
|
|
hotTopics: [
|
|
{ id: 1, name: '今天你吃蛋了么?', count: 123 },
|
|
{ id: 2, name: '日常分享', count: 456 }
|
|
]
|
|
});
|
|
});
|
|
},
|
|
|
|
onContentChange(e) {
|
|
this.setData({
|
|
content: e.detail.value
|
|
});
|
|
},
|
|
|
|
chooseImage() {
|
|
if (this.data.images.length >= 9) {
|
|
wx.showToast({
|
|
title: '最多只能上传9张图片',
|
|
icon: 'none'
|
|
});
|
|
return;
|
|
}
|
|
|
|
wx.chooseImage({
|
|
count: 9 - 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
|
|
const userId = wx.getStorageSync('userId');
|
|
if (!userId) {
|
|
throw new Error('用户未登录');
|
|
}
|
|
|
|
// 获取用户电话号码
|
|
const userInfo = wx.getStorageSync('userInfo');
|
|
const phoneNumber = userInfo?.phoneNumber || wx.getStorageSync('phoneNumber');
|
|
console.log('获取到的用户电话号码:', phoneNumber);
|
|
|
|
const postData = {
|
|
user_id: userId,
|
|
phone: phoneNumber,
|
|
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: '发布失败,请重试',
|
|
icon: 'none'
|
|
});
|
|
});
|
|
},
|
|
|
|
// 上传图片获取永久 URL
|
|
uploadImages(tempImagePaths) {
|
|
return new Promise((resolve, reject) => {
|
|
if (!tempImagePaths || tempImagePaths.length === 0) {
|
|
resolve([]);
|
|
return;
|
|
}
|
|
|
|
const uploadedImages = [];
|
|
let uploadedCount = 0;
|
|
|
|
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);
|
|
}
|
|
} catch (e) {
|
|
console.error('解析上传响应失败:', e);
|
|
}
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
console.error('上传图片失败:', err);
|
|
},
|
|
complete: () => {
|
|
uploadedCount++;
|
|
if (uploadedCount === tempImagePaths.length) {
|
|
resolve(uploadedImages);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
});
|
|
}
|
|
});
|
|
|