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.
174 lines
3.7 KiB
174 lines
3.7 KiB
const API = require('../../utils/api.js');
|
|
|
|
Page({
|
|
data: {
|
|
content: '',
|
|
images: [],
|
|
selectedTopic: null,
|
|
showTopicModal: false,
|
|
hotTopics: []
|
|
},
|
|
|
|
onLoad() {
|
|
this.loadHotTopics();
|
|
},
|
|
|
|
loadHotTopics() {
|
|
API.getHotTopics().then(res => {
|
|
this.setData({
|
|
hotTopics: res.data || []
|
|
});
|
|
}).catch(err => {
|
|
console.error('加载热门话题失败:', err);
|
|
});
|
|
},
|
|
|
|
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() {
|
|
if (!this.data.content.trim()) {
|
|
return;
|
|
}
|
|
|
|
wx.showLoading({
|
|
title: '发布中...'
|
|
});
|
|
|
|
// 先上传图片,获取永久 URL
|
|
this.uploadImages(this.data.images)
|
|
.then(uploadedImages => {
|
|
const postData = {
|
|
content: this.data.content,
|
|
images: uploadedImages,
|
|
topic: this.data.selectedTopic
|
|
};
|
|
|
|
return API.createPost(postData);
|
|
})
|
|
.then(res => {
|
|
wx.hideLoading();
|
|
wx.showToast({
|
|
title: '发布成功',
|
|
icon: 'success'
|
|
});
|
|
setTimeout(() => {
|
|
wx.navigateBack();
|
|
}, 1000);
|
|
})
|
|
.catch(err => {
|
|
wx.hideLoading();
|
|
wx.showToast({
|
|
title: '发布失败,请重试',
|
|
icon: 'none'
|
|
});
|
|
console.error('发布动态失败:', err);
|
|
});
|
|
},
|
|
|
|
// 上传图片获取永久 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);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
});
|
|
}
|
|
});
|
|
|