Browse Source

完成合并操作

蛋吧eggbar
Trae AI 1 month ago
parent
commit
3403edb589
  1. 39
      pages/eggbar/create-post.js
  2. 101
      pages/eggbar/eggbar.js
  3. 7
      pages/eggbar/eggbar.wxml
  4. 14
      pages/eggbar/eggbar.wxss
  5. 32
      server-example/server-mysql.js
  6. 4
      utils/api.js

39
pages/eggbar/create-post.js

@ -132,20 +132,47 @@ Page({
.then(uploadedImages => {
console.log('图片上传完成,上传的图片数量:', uploadedImages.length);
// 获取用户ID
const userId = wx.getStorageSync('userId');
// 获取用户ID - 从多个来源获取,确保可靠性
let userId = wx.getStorageSync('userId');
// 如果没有userId,尝试从userInfo中获取
if (!userId) {
throw new Error('用户未登录');
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');
const phoneNumber = userInfo?.phoneNumber || wx.getStorageSync('phoneNumber');
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
@ -169,7 +196,7 @@ Page({
console.error('发布动态失败:', err);
wx.hideLoading();
wx.showToast({
title: '发布失败,请重试',
title: err.message || '发布失败,请重试',
icon: 'none'
});
});

101
pages/eggbar/eggbar.js

@ -83,19 +83,27 @@ Page({
loading: true
});
// 获取用户电话号码
// 获取用户信息
const userInfo = wx.getStorageSync('userInfo');
const phoneNumber = userInfo?.phoneNumber || wx.getStorageSync('phoneNumber');
const userId = wx.getStorageSync('userId') || userInfo?.userId;
// 只有当电话号码存在时才传递,避免传递空值
// 构建请求参数
const params = {
page: this.data.page,
pageSize: this.data.pageSize
};
// 只有当电话号码存在时才传递,避免传递空值
if (phoneNumber) {
params.phone = phoneNumber;
}
// 传递userId参数,用于后端过滤动态
if (userId) {
params.userId = userId;
}
API.getPosts(params).then(res => {
console.log('后端返回的完整响应:', res);
// 正确处理后端返回的响应格式
@ -103,7 +111,7 @@ Page({
console.log('后端返回的动态数量:', newPosts.length);
console.log('后端返回的动态数据:', newPosts);
// 处理images字段,确保它是一个数组
// 处理images字段,确保它是一个数组,并添加username字段
newPosts = newPosts.map(post => {
if (post.images) {
// 如果images是字符串,尝试解析为JSON数组
@ -126,9 +134,73 @@ Page({
// 如果images不存在,设置为空数组
post.images = [];
}
// 添加username字段(使用phone的前几位作为用户名)
if (!post.username) {
if (post.phone) {
// 隐藏手机号中间部分
post.username = post.phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
} else {
post.username = '用户' + post.user_id?.substring(0, 6) || '未知用户';
}
}
// 添加time字段用于显示时间
if (!post.time && post.created_at) {
const date = new Date(post.created_at);
post.time = date.toLocaleString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit'
});
}
// 确保topic字段存在
if (post.topic) {
console.log('动态话题:', post.topic);
}
// 为了兼容前端模板,添加user对象
if (!post.user) {
post.user = {
avatar: post.avatar_url || '',
nickname: post.username
};
}
// 为了兼容前端模板,添加image字段(使用images数组的第一个元素)
if (!post.image && post.images && post.images.length > 0) {
post.image = post.images[0];
}
// 为了兼容前端模板,添加like_count和comment_count字段
if (post.likes !== undefined && post.like_count === undefined) {
post.like_count = post.likes;
}
if (post.comments !== undefined && post.comment_count === undefined) {
post.comment_count = post.comments;
}
return post;
});
// 根据status字段过滤动态:0仅自己可见,1所有人可见,2仅自己可见
newPosts = newPosts.filter(post => {
// 如果status为1,所有人可见
if (post.status === 1) {
return true;
}
// 如果status为0或2,仅自己可见
if (post.status === 0 || post.status === 2) {
// 检查是否是当前用户的动态
return post.user_id === userId;
}
// 默认显示
return true;
});
// 如果是第一页且没有数据,使用默认动态数据
if (this.data.page === 1 && (!newPosts || newPosts.length === 0)) {
newPosts = [
@ -331,6 +403,29 @@ Page({
});
},
// 预览图片
previewImage(e) {
const images = e.currentTarget.dataset.images;
const currentIndex = parseInt(e.currentTarget.dataset.current);
console.log('预览图片:', {
images,
currentIndex,
current: images[currentIndex]
});
wx.previewImage({
current: images[currentIndex],
urls: images,
success: function(res) {
console.log('预览图片成功:', res);
},
fail: function(err) {
console.error('预览图片失败:', err);
}
});
},
createPost() {
wx.navigateTo({
url: '/pages/eggbar/create-post'

7
pages/eggbar/eggbar.wxml

@ -29,7 +29,7 @@
<view class="post-list">
<view class="post-item" wx:for="{{posts}}" wx:key="id" bindtap="viewPost" data-post="{{item}}">
<view class="post-header">
<image class="post-avatar" src="{{item.avatar}}" mode="aspectFill"></image>
<image class="post-avatar" src="{{item.avatar_url}}" mode="aspectFill"></image>
<view class="post-user">
<text class="post-username">{{item.username}}</text>
<text class="post-time">{{item.time}}</text>
@ -38,7 +38,10 @@
<view class="post-content">
<text class="post-text">{{item.content}}</text>
<view class="post-images" wx:if="{{item.images && item.images.length > 0}}">
<image class="post-image" wx:for="{{item.images}}" wx:for-item="img" wx:key="index" src="{{img}}" mode="aspectFill"></image>
<image class="post-image" wx:for="{{item.images}}" wx:for-item="img" wx:key="index" src="{{img}}" mode="aspectFill" bindtap="previewImage" data-images="{{item.images}}" data-current="{{index}}"></image>
</view>
<view class="post-topic" wx:if="{{item.topic}}">
<text class="topic-text">#{{item.topic}}</text>
</view>
</view>
<view class="post-footer">

14
pages/eggbar/eggbar.wxss

@ -182,6 +182,20 @@ page {
background: #f0f0f0;
}
.post-topic {
margin-top: 15rpx;
padding: 8rpx 16rpx;
background: #f0f8ff;
border-radius: 20rpx;
display: inline-block;
}
.topic-text {
font-size: 24rpx;
color: #1890ff;
font-weight: 500;
}
.post-footer {
display: flex;
justify-content: space-around;

32
server-example/server-mysql.js

@ -132,7 +132,7 @@ app.post('/api/eggbar/posts', async (req, res) => {
console.log('===== 收到帖子创建请求 =====');
console.log('1. 收到请求体:', JSON.stringify(req.body, null, 2));
const { user_id, content, images, topic, phone } = req.body;
const { user_id, content, images, topic, phone, avatar_url } = req.body;
// 数据验证
if (!user_id) {
@ -153,6 +153,7 @@ app.post('/api/eggbar/posts', async (req, res) => {
const newPost = await EggbarPost.create({
user_id: user_id,
phone: phone || null,
avatar_url: avatar_url || null,
content: content || null,
images: imagesData,
topic: topic || null
@ -260,6 +261,7 @@ app.get('/api/eggbar/posts', async (req, res) => {
id: post.id,
user_id: post.user_id,
phone: post.phone,
avatar_url: post.avatar_url,
content: post.content,
images: images,
topic: post.topic,
@ -272,8 +274,29 @@ app.get('/api/eggbar/posts', async (req, res) => {
};
});
// 检查用户是否已点赞
// 获取当前用户信息
const phone = req.query.phone || req.headers['x-phone'];
const userId = req.query.userId || req.headers['x-user-id'];
// 根据status字段和用户信息过滤动态
formattedPosts = formattedPosts.filter(post => {
// 获取帖子状态,默认为0
const postStatus = post.status || 0;
// status=1:审核通过,所有用户都可见
if (postStatus === 1) {
return true;
}
// status=0(待审核)或2(拒绝):只有发布者可见
// 检查是否是发布者(通过phone或userId)
const isPublisherByPhone = phone && post.phone === phone;
const isPublisherByUserId = userId && post.user_id === userId;
return isPublisherByPhone || isPublisherByUserId;
});
// 检查用户是否已点赞
if (phone) {
try {
// 批量检查点赞状态
@ -1743,6 +1766,11 @@ EggbarPost.init({
allowNull: true,
comment: '用户电话号码'
},
avatar_url: {
type: DataTypes.TEXT,
allowNull: true,
comment: '用户头像URL'
},
content: {
type: DataTypes.TEXT,
allowNull: true,

4
utils/api.js

@ -2266,6 +2266,10 @@ module.exports = {
getApp().globalData.userType = userType;
}
// 将用户信息存储到本地存储
wx.setStorageSync('userInfo', userInfo);
console.log('用户信息已存储到本地存储:', userInfo);
// 将用户类型更新到服务器数据库
if (isCustomerService) {
console.log('将客服身份更新到服务器数据库');

Loading…
Cancel
Save