fix(vip-search): 修复VIP客户搜索功能的数据加载时序问题
增强搜索函数的数据加载检查,改进异步处理机制,优化搜索匹配算法
This commit is contained in:
@@ -534,8 +534,16 @@ $packages_json = json_encode(array_map(function($package) {
|
||||
// 检查数据库连接
|
||||
checkDatabaseConnection();
|
||||
|
||||
// 加载VIP客户(必须在页面加载时完成)
|
||||
loadVIPCustomers();
|
||||
// 加载VIP客户(使用异步加载确保数据完整性)
|
||||
console.log('开始初始化VIP客户数据...');
|
||||
loadVIPCustomers()
|
||||
.then(data => {
|
||||
console.log('VIP客户数据初始化成功,共', data.length, '条记录');
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('VIP客户数据初始化失败:', error);
|
||||
// 即使VIP数据加载失败,也要继续页面初始化
|
||||
});
|
||||
|
||||
// 调试信息
|
||||
console.log('页面初始化完成');
|
||||
@@ -543,42 +551,65 @@ $packages_json = json_encode(array_map(function($package) {
|
||||
|
||||
let allVIPCustomers = []; // 全局变量存储所有VIP客户数据
|
||||
|
||||
// 加载VIP客户列表
|
||||
// 加载VIP客户列表 - 返回Promise以支持异步调用
|
||||
function loadVIPCustomers() {
|
||||
console.log('开始加载VIP客户列表...');
|
||||
|
||||
fetch('get_vip_customers.php')
|
||||
.then(response => {
|
||||
console.log('VIP客户API响应状态:', response.status);
|
||||
console.log('VIP客户API响应头:', response.headers);
|
||||
return response.text();
|
||||
})
|
||||
.then(text => {
|
||||
console.log('VIP客户API原始响应:', text);
|
||||
|
||||
try {
|
||||
const data = JSON.parse(text);
|
||||
console.log('VIP客户数据解析成功:', data);
|
||||
return new Promise((resolve, reject) => {
|
||||
fetch('get_vip_customers.php')
|
||||
.then(response => {
|
||||
console.log('VIP客户API响应状态:', response.status);
|
||||
console.log('VIP客户API响应头:', response.headers);
|
||||
|
||||
allVIPCustomers = data; // 存储所有VIP客户数据
|
||||
updateVIPSelect(data); // 更新下拉列表
|
||||
|
||||
console.log('VIP客户列表加载完成,共', data.length, '条记录');
|
||||
|
||||
// 测试搜索功能
|
||||
if (data.length > 0) {
|
||||
console.log('测试搜索功能...');
|
||||
testSearchFunction();
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
||||
}
|
||||
} catch(e) {
|
||||
console.error('VIP客户数据解析失败:', e);
|
||||
|
||||
return response.text();
|
||||
})
|
||||
.then(text => {
|
||||
console.log('VIP客户API原始响应:', text);
|
||||
|
||||
try {
|
||||
const data = JSON.parse(text);
|
||||
|
||||
// 检查是否是错误响应
|
||||
if (data.error) {
|
||||
throw new Error(data.message || 'API返回错误');
|
||||
}
|
||||
|
||||
console.log('VIP客户数据解析成功:', data);
|
||||
|
||||
// 确保数据是数组格式
|
||||
if (!Array.isArray(data)) {
|
||||
throw new Error('API返回数据不是数组格式');
|
||||
}
|
||||
|
||||
allVIPCustomers = data; // 存储所有VIP客户数据
|
||||
updateVIPSelect(data); // 更新下拉列表
|
||||
|
||||
console.log('VIP客户列表加载完成,共', data.length, '条记录');
|
||||
|
||||
// 测试搜索功能
|
||||
if (data.length > 0) {
|
||||
console.log('测试搜索功能...');
|
||||
testSearchFunction();
|
||||
}
|
||||
|
||||
resolve(data); // 成功完成Promise
|
||||
|
||||
} catch(e) {
|
||||
console.error('VIP客户数据解析失败:', e);
|
||||
allVIPCustomers = [];
|
||||
reject(e); // 拒绝Promise
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('加载VIP客户列表失败:', error);
|
||||
allVIPCustomers = [];
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('加载VIP客户列表失败:', error);
|
||||
allVIPCustomers = [];
|
||||
});
|
||||
reject(error); // 拒绝Promise
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 测试搜索功能
|
||||
@@ -609,7 +640,7 @@ $packages_json = json_encode(array_map(function($package) {
|
||||
});
|
||||
}
|
||||
|
||||
// VIP客户搜索功能
|
||||
// VIP客户搜索功能 - 修复数据加载时序问题
|
||||
function searchVIPCustomers() {
|
||||
console.log('开始搜索VIP客户...');
|
||||
|
||||
@@ -628,7 +659,21 @@ $packages_json = json_encode(array_map(function($package) {
|
||||
|
||||
const searchTerm = searchInput.value.trim();
|
||||
console.log('搜索关键词:', searchTerm);
|
||||
|
||||
// 检查数据加载状态
|
||||
console.log('VIP客户总数:', allVIPCustomers.length);
|
||||
console.log('VIP客户数据:', allVIPCustomers);
|
||||
|
||||
// 如果数据还没加载完成,先加载数据
|
||||
if (allVIPCustomers.length === 0) {
|
||||
console.log('VIP客户数据尚未加载,先加载数据...');
|
||||
loadVIPCustomers().then(() => {
|
||||
console.log('数据加载完成,重新执行搜索');
|
||||
// 重新调用搜索函数
|
||||
setTimeout(() => searchVIPCustomers(), 100);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (searchTerm === '') {
|
||||
clearSearchResults();
|
||||
@@ -637,16 +682,27 @@ $packages_json = json_encode(array_map(function($package) {
|
||||
}
|
||||
|
||||
// 模糊搜索:支持姓名和手机号搜索
|
||||
const filteredCustomers = allVIPCustomers.filter(vip =>
|
||||
(vip.customer_name && vip.customer_name.includes(searchTerm)) ||
|
||||
(vip.phone && vip.phone.includes(searchTerm))
|
||||
);
|
||||
const filteredCustomers = allVIPCustomers.filter(vip => {
|
||||
const name = (vip.customer_name || '').toLowerCase();
|
||||
const phone = (vip.phone || '').toLowerCase();
|
||||
const term = searchTerm.toLowerCase();
|
||||
|
||||
const nameMatch = name.includes(term);
|
||||
const phoneMatch = phone.includes(term);
|
||||
|
||||
console.log(`检查VIP客户: ${vip.customer_name} (${vip.phone}) - 匹配结果: 姓名=${nameMatch}, 手机=${phoneMatch}`);
|
||||
|
||||
return nameMatch || phoneMatch;
|
||||
});
|
||||
|
||||
console.log('搜索结果:', filteredCustomers);
|
||||
displaySearchResults(filteredCustomers, searchTerm);
|
||||
|
||||
// 隐藏下拉列表
|
||||
document.getElementById('vip_id').style.display = 'none';
|
||||
const vipSelect = document.getElementById('vip_id');
|
||||
if (vipSelect) {
|
||||
vipSelect.style.display = 'none';
|
||||
}
|
||||
}
|
||||
|
||||
// 显示搜索结果
|
||||
|
||||
Reference in New Issue
Block a user