diff --git a/pages/eggbar/create-post.js b/pages/eggbar/create-post.js index c713daf..d2c2a77 100644 --- a/pages/eggbar/create-post.js +++ b/pages/eggbar/create-post.js @@ -15,11 +15,28 @@ Page({ loadHotTopics() { API.getHotTopics().then(res => { - this.setData({ - hotTopics: res.data || [] - }); + 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 } + ] + }); }); }, @@ -87,10 +104,19 @@ Page({ }, submit() { - if (!this.data.content.trim()) { + 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: '发布中...' }); @@ -98,15 +124,18 @@ Page({ // 先上传图片,获取永久 URL this.uploadImages(this.data.images) .then(uploadedImages => { + console.log('图片上传完成,上传的图片数量:', uploadedImages.length); const postData = { content: this.data.content, images: uploadedImages, topic: this.data.selectedTopic }; + console.log('准备发送的发布数据:', postData); return API.createPost(postData); }) .then(res => { + console.log('发布成功,服务器返回:', res); wx.hideLoading(); wx.showToast({ title: '发布成功', @@ -117,12 +146,12 @@ Page({ }, 1000); }) .catch(err => { + console.error('发布动态失败:', err); wx.hideLoading(); wx.showToast({ title: '发布失败,请重试', icon: 'none' }); - console.error('发布动态失败:', err); }); }, diff --git a/pages/eggbar/create-post.wxml b/pages/eggbar/create-post.wxml index ba07cad..986cc3d 100644 --- a/pages/eggbar/create-post.wxml +++ b/pages/eggbar/create-post.wxml @@ -45,7 +45,7 @@ - diff --git a/server-example/server-mysql.js b/server-example/server-mysql.js index 15c5141..28d6306 100644 --- a/server-example/server-mysql.js +++ b/server-example/server-mysql.js @@ -134,12 +134,12 @@ app.post('/api/eggbar/posts', async (req, res) => { const { content, images, topic } = req.body; - // 数据验证 - if (!content || content.trim() === '') { + // 数据验证 - 允许只填写内容或话题中的一项 + if ((!content || content.trim() === '') && !topic) { return res.status(400).json({ success: false, code: 400, - message: '帖子内容不能为空' + message: '文本内容和话题至少需要填写一项' }); } @@ -201,6 +201,48 @@ app.get('/api/cover', async (req, res) => { } }); +// 创建动态接口(已废弃 - 使用上面的实际实现) +app.post('/api/eggbar/posts/deprecated', async (req, res) => { + try { + console.log('===== 创建动态接口被调用 ====='); + console.log('收到的请求数据:', req.body); + + const { content, images, topic } = req.body; + + // 验证参数 + if (!content && !topic) { + return res.json({ + success: false, + message: '文本内容和话题至少需要填写一项' + }); + } + + // 模拟创建动态 + console.log('创建动态成功:', { + content, + images, + topic + }); + + res.json({ + success: true, + message: '发布成功', + data: { + content, + images, + topic + } + }); + } catch (error) { + console.error('创建动态失败:', error); + res.json({ + success: false, + message: '发布失败,请重试', + error: error.message + }); + } +}); + // 创建临时文件夹用于存储上传的文件 const uploadTempDir = path.join(__dirname, 'temp-uploads'); if (!fs.existsSync(uploadTempDir)) { diff --git a/server-example/test-post-api.js b/server-example/test-post-api.js new file mode 100644 index 0000000..3b738f4 --- /dev/null +++ b/server-example/test-post-api.js @@ -0,0 +1,47 @@ +// 测试创建动态API +const http = require('http'); + +// 测试数据 +const testData = { + content: '测试发布帖子', + images: ['https://example.com/image1.jpg', 'https://example.com/image2.jpg'], + topic: '今天你吃蛋了么?' +}; + +// 发送POST请求到主服务器 +const options = { + hostname: 'localhost', + port: 3003, + path: '/api/eggbar/posts', + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Content-Length': Buffer.byteLength(JSON.stringify(testData)) + } +}; + +const req = http.request(options, (res) => { + console.log(`状态码: ${res.statusCode}`); + console.log(`响应头: ${JSON.stringify(res.headers)}`); + + let data = ''; + res.on('data', (chunk) => { + data += chunk; + }); + + res.on('end', () => { + console.log('响应体:', data); + console.log('测试完成'); + }); +}); + +req.on('error', (e) => { + console.error(`请求错误: ${e.message}`); +}); + +// 发送请求体 +req.write(JSON.stringify(testData)); +req.end(); + +console.log('正在发送测试请求到 http://localhost:3003/api/eggbar/posts...'); +console.log('测试数据:', testData);