Browse Source

修复导航问题,完善聊天列表功能,更新聊天详情页标题显示

pull/1/head
徐飞洋 3 months ago
parent
commit
4e7b17a76c
  1. 69
      pages/chat-detail/index.js
  2. 2
      pages/chat-detail/index.wxml
  3. 3
      pages/chat-detail/index.wxss
  4. 65
      pages/chat/index.js
  5. 102
      server-example/server-mysql.js

69
pages/chat-detail/index.js

@ -1,10 +1,14 @@
// pages/chat-detail/index.js // pages/chat-detail/index.js
const API = require('../../utils/api.js');
Page({ Page({
data: { data: {
chatId: null, chatId: null,
messages: [], messages: [],
inputValue: '', inputValue: '',
loading: false loading: false,
chatTitle: '聊天对象',
managerPhone: null
}, },
onLoad: function (options) { onLoad: function (options) {
@ -12,10 +16,73 @@ Page({
this.setData({ this.setData({
chatId: options.id chatId: options.id
}); });
// 获取聊天标题
this.loadChatTitle();
} }
this.loadMessages(); this.loadMessages();
}, },
// 加载聊天标题
loadChatTitle: function () {
// 从本地存储获取聊天列表
const users = wx.getStorageSync('users') || {};
const userId = wx.getStorageSync('userId');
let userPhone = null;
// 尝试从users中获取手机号
if (userId && users[userId]) {
if (users[userId].phoneNumber) {
userPhone = users[userId].phoneNumber;
} else if (users[userId].phone) {
userPhone = users[userId].phone;
}
}
// 如果还没有获取到,尝试从全局用户信息获取
if (!userPhone) {
const userInfo = wx.getStorageSync('userInfo');
if (userInfo) {
if (userInfo.phoneNumber) {
userPhone = userInfo.phoneNumber;
} else if (userInfo.phone) {
userPhone = userInfo.phone;
}
}
}
// 如果还没有获取到,尝试从直接存储获取
if (!userPhone) {
if (wx.getStorageSync('phoneNumber')) {
userPhone = wx.getStorageSync('phoneNumber');
} else if (wx.getStorageSync('phone')) {
userPhone = wx.getStorageSync('phone');
}
}
if (userPhone) {
// 获取聊天列表
API.getChatList(userPhone).then(chatList => {
if (Array.isArray(chatList)) {
// 找到当前聊天项
const currentChat = chatList.find(item => item.id === this.data.chatId);
if (currentChat && currentChat.manager_phone) {
this.setData({ managerPhone: currentChat.manager_phone });
// 获取业务员信息
API.getSalesPersonnelInfo(currentChat.manager_phone).then(personnelInfo => {
if (personnelInfo && personnelInfo.alias) {
this.setData({ chatTitle: personnelInfo.alias });
}
}).catch(error => {
console.error('获取业务员信息失败:', error);
});
}
}
}).catch(error => {
console.error('获取聊天列表失败:', error);
});
}
},
onBack: function () { onBack: function () {
wx.navigateBack(); wx.navigateBack();
}, },

2
pages/chat-detail/index.wxml

@ -4,7 +4,7 @@
<text>←</text> <text>←</text>
</view> </view>
<view class="header-info"> <view class="header-info">
<text class="header-name">聊天对象</text> <text class="header-name">{{chatTitle}}</text>
</view> </view>
<view class="header-more"> <view class="header-more">
<text>⋮</text> <text>⋮</text>

3
pages/chat-detail/index.wxss

@ -10,11 +10,12 @@
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: space-between; justify-content: space-between;
padding: 16rpx 24rpx; padding: 16rpx 0;
border-bottom: 1rpx solid #e8e8e8; border-bottom: 1rpx solid #e8e8e8;
position: sticky; position: sticky;
top: 0; top: 0;
z-index: 100; z-index: 100;
width: 100%;
} }
.header-back, .header-more { .header-back, .header-more {

65
pages/chat/index.js

@ -18,22 +18,67 @@ Page({
title: '加载中...', title: '加载中...',
}); });
// 获取用户手机号 // 获取用户手机号 - 增强版,增加更多获取途径和调试信息
const users = wx.getStorageSync('users') || {}; const users = wx.getStorageSync('users') || {};
const userId = wx.getStorageSync('userId'); const userId = wx.getStorageSync('userId');
let userPhone = null; let userPhone = null;
// 尝试从users中获取手机号 // 添加调试信息,显示当前存储的用户信息
if (userId && users[userId] && users[userId].phoneNumber) { console.log('调试信息 - 登录用户:', {
userPhone = users[userId].phoneNumber; userId: userId,
} else { users: users,
// 尝试从全局用户信息获取 userInfo: wx.getStorageSync('userInfo'),
phoneNumber: wx.getStorageSync('phoneNumber'),
phone: wx.getStorageSync('phone')
});
// 尝试从users中获取手机号(支持不同的键名)
if (userId && users[userId]) {
if (users[userId].phoneNumber) {
userPhone = users[userId].phoneNumber;
console.log('从users[userId].phoneNumber获取到手机号:', userPhone);
} else if (users[userId].phone) {
userPhone = users[userId].phone;
console.log('从users[userId].phone获取到手机号:', userPhone);
}
}
// 如果还没有获取到,尝试从全局用户信息获取
if (!userPhone) {
const userInfo = wx.getStorageSync('userInfo'); const userInfo = wx.getStorageSync('userInfo');
if (userInfo && userInfo.phoneNumber) { if (userInfo) {
userPhone = userInfo.phoneNumber; if (userInfo.phoneNumber) {
} else { userPhone = userInfo.phoneNumber;
// 尝试从直接存储的phoneNumber获取 console.log('从userInfo.phoneNumber获取到手机号:', userPhone);
} else if (userInfo.phone) {
userPhone = userInfo.phone;
console.log('从userInfo.phone获取到手机号:', userPhone);
}
}
}
// 如果还没有获取到,尝试从直接存储获取(支持不同的键名)
if (!userPhone) {
if (wx.getStorageSync('phoneNumber')) {
userPhone = wx.getStorageSync('phoneNumber'); userPhone = wx.getStorageSync('phoneNumber');
console.log('从storage.phoneNumber获取到手机号:', userPhone);
} else if (wx.getStorageSync('phone')) {
userPhone = wx.getStorageSync('phone');
console.log('从storage.phone获取到手机号:', userPhone);
}
}
// 如果还没有获取到,尝试从更多可能的存储位置获取
if (!userPhone) {
const loginInfo = wx.getStorageSync('loginInfo');
if (loginInfo) {
if (loginInfo.phoneNumber) {
userPhone = loginInfo.phoneNumber;
console.log('从loginInfo.phoneNumber获取到手机号:', userPhone);
} else if (loginInfo.phone) {
userPhone = loginInfo.phone;
console.log('从loginInfo.phone获取到手机号:', userPhone);
}
} }
} }

102
server-example/server-mysql.js

@ -7826,6 +7826,108 @@ async function handleSessionMessage(ws, data) {
} }
} }
// 新增:获取聊天列表数据接口 - 根据用户手机号查询
app.post('/api/chat/list', async (req, res) => {
try {
const { user_phone } = req.body;
console.log('获取聊天列表 - user_phone:', user_phone);
if (!user_phone) {
return res.status(400).json({
success: false,
code: 400,
message: '用户手机号不能为空'
});
}
// 从wechat_app数据库的chat_list表中查询用户的聊天记录
const chatList = await sequelize.query(
'SELECT * FROM chat_list WHERE user_phone = ?',
{
replacements: [user_phone],
type: Sequelize.QueryTypes.SELECT
}
);
console.log('找到聊天记录数量:', chatList.length);
// 如果没有聊天记录,返回空数组
if (chatList.length === 0) {
return res.status(200).json({
success: true,
code: 200,
message: '暂无聊天记录',
data: []
});
}
// 返回聊天列表数据
res.status(200).json({
success: true,
code: 200,
message: '获取聊天列表成功',
data: chatList
});
} catch (error) {
console.error('获取聊天列表失败:', error);
res.status(500).json({
success: false,
code: 500,
message: '获取聊天列表失败: ' + error.message
});
}
});
// 新增:获取业务员信息接口 - 根据手机号查询
app.post('/api/personnel/get', async (req, res) => {
try {
const { phone } = req.body;
console.log('获取业务员信息 - phone:', phone);
if (!phone) {
return res.status(400).json({
success: false,
code: 400,
message: '手机号不能为空'
});
}
// 从userlogin数据库的personnel表中查询业务员信息
const personnel = await userLoginSequelize.query(
'SELECT * FROM personnel WHERE phoneNumber = ?',
{
replacements: [phone],
type: Sequelize.QueryTypes.SELECT
}
);
console.log('找到业务员信息数量:', personnel.length);
if (personnel.length === 0) {
return res.status(200).json({
success: false,
code: 404,
message: '未找到该手机号对应的业务员信息'
});
}
// 返回业务员信息
res.status(200).json({
success: true,
code: 200,
message: '获取业务员信息成功',
data: personnel[0]
});
} catch (error) {
console.error('获取业务员信息失败:', error);
res.status(500).json({
success: false,
code: 500,
message: '获取业务员信息失败: ' + error.message
});
}
});
// 在服务器启动前执行商品联系人更新 // 在服务器启动前执行商品联系人更新
updateProductContacts().then(() => { updateProductContacts().then(() => {
console.log('\n📦 商品联系人信息更新完成!'); console.log('\n📦 商品联系人信息更新完成!');

Loading…
Cancel
Save