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); } } } }); }); }); } });