Browse Source

修复eggbar发布功能:更新验证逻辑并避免端点冲突

pull/18/head
徐飞洋 1 month ago
parent
commit
21d50f31f0
  1. 39
      pages/eggbar/create-post.js
  2. 2
      pages/eggbar/create-post.wxml
  3. 48
      server-example/server-mysql.js
  4. 47
      server-example/test-post-api.js

39
pages/eggbar/create-post.js

@ -15,11 +15,28 @@ Page({
loadHotTopics() { loadHotTopics() {
API.getHotTopics().then(res => { API.getHotTopics().then(res => {
this.setData({ if (res.data && res.data.length > 0) {
hotTopics: res.data || [] this.setData({
}); hotTopics: res.data
});
} else {
// 使用默认热门话题
this.setData({
hotTopics: [
{ id: 1, name: '今天你吃蛋了么?', count: 123 },
{ id: 2, name: '日常分享', count: 456 }
]
});
}
}).catch(err => { }).catch(err => {
console.error('加载热门话题失败:', err); console.error('加载热门话题失败:', err);
// 出错时使用默认热门话题
this.setData({
hotTopics: [
{ id: 1, name: '今天你吃蛋了么?', count: 123 },
{ id: 2, name: '日常分享', count: 456 }
]
});
}); });
}, },
@ -87,10 +104,19 @@ Page({
}, },
submit() { 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; return;
} }
console.log('验证通过,开始发布');
wx.showLoading({ wx.showLoading({
title: '发布中...' title: '发布中...'
}); });
@ -98,15 +124,18 @@ Page({
// 先上传图片,获取永久 URL // 先上传图片,获取永久 URL
this.uploadImages(this.data.images) this.uploadImages(this.data.images)
.then(uploadedImages => { .then(uploadedImages => {
console.log('图片上传完成,上传的图片数量:', uploadedImages.length);
const postData = { const postData = {
content: this.data.content, content: this.data.content,
images: uploadedImages, images: uploadedImages,
topic: this.data.selectedTopic topic: this.data.selectedTopic
}; };
console.log('准备发送的发布数据:', postData);
return API.createPost(postData); return API.createPost(postData);
}) })
.then(res => { .then(res => {
console.log('发布成功,服务器返回:', res);
wx.hideLoading(); wx.hideLoading();
wx.showToast({ wx.showToast({
title: '发布成功', title: '发布成功',
@ -117,12 +146,12 @@ Page({
}, 1000); }, 1000);
}) })
.catch(err => { .catch(err => {
console.error('发布动态失败:', err);
wx.hideLoading(); wx.hideLoading();
wx.showToast({ wx.showToast({
title: '发布失败,请重试', title: '发布失败,请重试',
icon: 'none' icon: 'none'
}); });
console.error('发布动态失败:', err);
}); });
}, },

2
pages/eggbar/create-post.wxml

@ -45,7 +45,7 @@
<view class="footer"> <view class="footer">
<button class="cancel-btn" bindtap="cancel">取消</button> <button class="cancel-btn" bindtap="cancel">取消</button>
<button class="submit-btn" bindtap="submit" disabled="{{!content.trim()}}"> <button class="submit-btn" bindtap="submit" disabled="{{!content.trim() && !selectedTopic}}">
发布 发布
</button> </button>
</view> </view>

48
server-example/server-mysql.js

@ -134,12 +134,12 @@ app.post('/api/eggbar/posts', async (req, res) => {
const { content, images, topic } = req.body; const { content, images, topic } = req.body;
// 数据验证 // 数据验证 - 允许只填写内容或话题中的一项
if (!content || content.trim() === '') { if ((!content || content.trim() === '') && !topic) {
return res.status(400).json({ return res.status(400).json({
success: false, success: false,
code: 400, 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'); const uploadTempDir = path.join(__dirname, 'temp-uploads');
if (!fs.existsSync(uploadTempDir)) { if (!fs.existsSync(uploadTempDir)) {

47
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);
Loading…
Cancel
Save