对话逻辑修复
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
// 聊天记录修复验证脚本
|
||||
// 在浏览器控制台中运行此脚本来验证修复效果
|
||||
|
||||
(function() {
|
||||
console.log('=== 聊天记录修复验证脚本 ===');
|
||||
|
||||
// 检查基础环境
|
||||
function checkEnvironment() {
|
||||
console.log('\n1. 检查基础环境:');
|
||||
|
||||
// 检查token
|
||||
const token = localStorage.getItem('token');
|
||||
console.log('- Token存在:', !!token);
|
||||
if (token) {
|
||||
console.log('- Token长度:', token.length);
|
||||
console.log('- Token前缀:', token.substring(0, 20) + '...');
|
||||
}
|
||||
|
||||
// 检查当前页面
|
||||
console.log('- 当前页面:', window.location.pathname);
|
||||
|
||||
// 检查API基础URL
|
||||
const apiBase = 'http://localhost:8080'; // 根据实际情况修改
|
||||
console.log('- API基础URL:', apiBase);
|
||||
|
||||
return { token, apiBase };
|
||||
}
|
||||
|
||||
// 测试API调用
|
||||
async function testAPI(url, token, apiBase) {
|
||||
try {
|
||||
console.log(`\n测试API: ${url}`);
|
||||
|
||||
const response = await fetch(`${apiBase}${url}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
console.log('- 响应状态:', response.status);
|
||||
console.log('- 响应状态文本:', response.statusText);
|
||||
|
||||
const data = await response.json();
|
||||
console.log('- 响应数据:', data);
|
||||
|
||||
if (response.ok && data.code === 200) {
|
||||
console.log('✅ API调用成功');
|
||||
return { success: true, data: data.data };
|
||||
} else {
|
||||
console.log('❌ API调用失败:', data.message || '未知错误');
|
||||
return { success: false, error: data.message || '未知错误' };
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.log('❌ 网络错误:', error.message);
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
}
|
||||
|
||||
// 测试所有聊天记录相关API
|
||||
async function testChatHistoryAPIs() {
|
||||
console.log('\n2. 测试聊天记录API:');
|
||||
|
||||
const { token, apiBase } = checkEnvironment();
|
||||
|
||||
if (!token) {
|
||||
console.log('❌ 没有找到token,请先登录');
|
||||
return;
|
||||
}
|
||||
|
||||
// 测试获取用户消息分页
|
||||
const pageResult = await testAPI('/message/user/page?current=1&size=5', token, apiBase);
|
||||
|
||||
// 测试搜索消息
|
||||
const searchResult = await testAPI('/message/user/search?keyword=测试&limit=5', token, apiBase);
|
||||
|
||||
// 测试获取最近消息
|
||||
const recentResult = await testAPI('/message/user/recent?limit=5', token, apiBase);
|
||||
|
||||
// 测试获取当前用户信息
|
||||
const userResult = await testAPI('/user/current', token, apiBase);
|
||||
|
||||
// 汇总结果
|
||||
console.log('\n3. 测试结果汇总:');
|
||||
console.log('- 分页查询:', pageResult.success ? '✅ 成功' : '❌ 失败');
|
||||
console.log('- 搜索功能:', searchResult.success ? '✅ 成功' : '❌ 失败');
|
||||
console.log('- 最近消息:', recentResult.success ? '✅ 成功' : '❌ 失败');
|
||||
console.log('- 用户信息:', userResult.success ? '✅ 成功' : '❌ 失败');
|
||||
|
||||
// 如果有成功的结果,显示数据统计
|
||||
if (pageResult.success && pageResult.data) {
|
||||
console.log('\n4. 数据统计:');
|
||||
console.log('- 总消息数:', pageResult.data.total || 0);
|
||||
console.log('- 当前页消息数:', pageResult.data.records ? pageResult.data.records.length : 0);
|
||||
|
||||
if (pageResult.data.records && pageResult.data.records.length > 0) {
|
||||
const firstMessage = pageResult.data.records[0];
|
||||
console.log('- 最新消息预览:', {
|
||||
id: firstMessage.id,
|
||||
content: firstMessage.content ? firstMessage.content.substring(0, 50) + '...' : '',
|
||||
sender: firstMessage.sender,
|
||||
createTime: firstMessage.createTime
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
pageResult,
|
||||
searchResult,
|
||||
recentResult,
|
||||
userResult
|
||||
};
|
||||
}
|
||||
|
||||
// 测试前端聊天记录功能
|
||||
function testFrontendChatHistory() {
|
||||
console.log('\n5. 测试前端聊天记录功能:');
|
||||
|
||||
// 检查是否在聊天页面
|
||||
if (!window.location.pathname.includes('/chat')) {
|
||||
console.log('❌ 当前不在聊天页面,请先进入聊天页面');
|
||||
return;
|
||||
}
|
||||
|
||||
// 查找聊天记录按钮
|
||||
const historyButton = document.querySelector('.header-right .action-btn');
|
||||
if (historyButton) {
|
||||
console.log('✅ 找到聊天记录按钮');
|
||||
|
||||
// 模拟点击
|
||||
console.log('- 模拟点击聊天记录按钮...');
|
||||
historyButton.click();
|
||||
|
||||
// 检查抽屉是否打开
|
||||
setTimeout(() => {
|
||||
const drawer = document.querySelector('.history-drawer');
|
||||
if (drawer && drawer.style.display !== 'none') {
|
||||
console.log('✅ 聊天记录抽屉已打开');
|
||||
} else {
|
||||
console.log('❌ 聊天记录抽屉未打开');
|
||||
}
|
||||
}, 1000);
|
||||
|
||||
} else {
|
||||
console.log('❌ 未找到聊天记录按钮');
|
||||
}
|
||||
}
|
||||
|
||||
// 主函数
|
||||
async function main() {
|
||||
try {
|
||||
// 测试API
|
||||
const apiResults = await testChatHistoryAPIs();
|
||||
|
||||
// 测试前端功能
|
||||
testFrontendChatHistory();
|
||||
|
||||
console.log('\n=== 验证完成 ===');
|
||||
console.log('如果API测试都成功,但前端仍有问题,请检查:');
|
||||
console.log('1. 浏览器控制台是否有JavaScript错误');
|
||||
console.log('2. 网络请求是否正常发送');
|
||||
console.log('3. 前端代码是否正确处理API响应');
|
||||
|
||||
} catch (error) {
|
||||
console.error('验证过程中发生错误:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 运行验证
|
||||
main();
|
||||
|
||||
})();
|
||||
Reference in New Issue
Block a user